HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
📝
프론트엔드 스쿨 교안(1기)
/
📝
Node
/
📝
18. 암호화
📝

18. 암호화

암호화

  • 평문 : 암호화 되지 않은 문서
  • 암호화 : 평문을 일반인들이 알지 못하는 암호로 만드는 과정
  • 복호화 : 암호화된 문서를 다시 평문으로 바꾸는 과정
  • 해쉬 : 복호화가 안되는 단방향 암호화(MD5(X), sha1(X), sha256(X), sha512(O) 등)
  • crypto는 양방향, 단방향 모두 지원합니다.
 

 
  • 패스워드 등 다양한 곳에서 sha256, sha512 방식을 사용합니다.
    • SHA256
      SHA256 online hash function
      https://emn178.github.io/online-tools/sha256.html
  • python에서는 hashlib, javascript에서는 pbkdf2, scrypt, bcrypt을 사용하여 암복호화를 수행할 수 있습니다. bcrypt를 실무에서 많이 사용합니다.
우선 간단하게 crypto-js로 원리를 알아본 다음 bcrypt를 사용해보도록 하겠습니다.
GitHub - brix/crypto-js: JavaScript library of crypto standards.
JavaScript library of crypto standards. Requirements: Node.js npm (Node.js package manager) ES6 import for typical API call signing use case: Modular include: Including all libraries, for access to extra methods: Requirements: Node.js Bower (package manager for frontend) Modular include: Including all libraries, for access to extra methods: See: https://cryptojs.gitbook.io/docs/ Plain text encryption Object encryption Fix module order in bundled release.
GitHub - brix/crypto-js: JavaScript library of crypto standards.
https://github.com/brix/crypto-js
GitHub - brix/crypto-js: JavaScript library of crypto standards.
CryptoJS
NOTE: I made a mistake when I named this implementation SHA-3. It should be named Keccak[c=2d]. Each of the SHA-3 functions is based on an instance of the Keccak algorithm, which NIST selected as the winner of the SHA-3 competition, but those SHA-3 functions won't produce hashes identical to Keccak.
CryptoJS
https://cryptojs.gitbook.io/docs/
CryptoJS
GitHub - kelektiv/node.bcrypt.js: bcrypt for NodeJs
A library to help you hash passwords. You can read about bcrypt in Wikipedia as well as in the following article: How To Safely Store A Password Verify that the node version you are using is a stable version; it has an even major release number.
GitHub - kelektiv/node.bcrypt.js: bcrypt for NodeJs
https://github.com/kelektiv/node.bcrypt.js
GitHub - kelektiv/node.bcrypt.js: bcrypt for NodeJs
// npm init --yes // npm i crypto-js bcrypt // npm i nodemon --save-dev // -> nodemon app1 으로 실행 가능 const crypto = require('crypto'); // 내장 모듈 const cryptojs = require('crypto-js'); // 외장 모듈(나름 괜찮아요.) const sha256 = require('crypto-js/sha256'); const Base64 = require('crypto-js/enc-base64'); const bcrypt = require('bcrypt') // 외장 모듈 // createHash : 해쉬 알고리즘 // update : 변환 문자열 // digest : 인코딩 // pbkdf2, scrypt, bcrypt(추천!!) console.log(crypto.createHash('sha256').update('leehojun').digest('base64')); console.log(crypto.createHash('sha256').update('leehojun').digest('hex')); console.log(Base64.stringify(sha256('leehojun'))); let hash = sha256('leehojun') console.log(hash.toString()); // 일반적인 sha256 console.log(hash.toString(cryptojs.enc.Hex)) console.log(hash.toString(cryptojs.enc.Base64)) console.log('------'); const password = 'leehojun'; // 사용자에게 입력된 패스워드 console.log(bcrypt.hashSync(password, 1)); console.log(bcrypt.hashSync(password, 10)); // $2b$10$juQd.2hc..ad1QLON.KQN.634A6RD20mxP.itVHU7/VxUocRkImgu // $2b$10 : 알고리즘과 복잡도 // $juQd.2hc..ad1QLON.KQN.634A6RD20mxP.itVHU7/VxUocRkImgu : salt와 hash hash = bcrypt.hashSync(password, 10); //10을 추천, 시간이 너무 많이 걸려요. console.log(bcrypt.compareSync(password, hash)); console.log('------'); // salt관리는 다른 파일로 하고 .git ignore합니다. // 절대 key나 salt는 github에 올라가면 안됩니다. crypto.randomBytes(64, (err, buf) => { const salt = buf.toString('base64'); console.log(`salt : ${salt}`); // 100번 반복에 64 바이트를 출력합니다. crypto.pbkdf2(password, salt, 100, 64, 'sha512', (err, key) =>{ console.log(key.toString('base64')); }); });