Search

crypto

crypto

: μ•”ν˜Έν™”, ν•΄μ‹œ, HMAC λ“±μ˜ λ³΄μ•ˆ κ΄€λ ¨ μž‘μ—…μ— λŒ€ν•œ κΈ°λŠ₯을 μ œκ³΅ν•΄μ£ΌλŠ” λ‚΄μž₯ λͺ¨λ“ˆ

μ •μ˜

crypto λͺ¨λ“ˆμ€ Node.jsμ—μ„œ μ œκ³΅ν•˜λŠ” λ‚΄μž₯ λͺ¨λ“ˆ 쀑 ν•˜λ‚˜λ‘œ, μ•”ν˜Έν™” 및 ν•΄μ‹œ κ΄€λ ¨ μž‘μ—…μ„ μˆ˜ν–‰ν•˜κΈ° μœ„ν•œ κΈ°λŠ₯을 μ œκ³΅ν•©λ‹ˆλ‹€. 이 λͺ¨λ“ˆμ„ μ‚¬μš©ν•˜λ©΄ λ‹€μ–‘ν•œ ν•΄μ‹œ μ•Œκ³ λ¦¬μ¦˜, μ•”ν˜Έν™” 및 λ³΅ν˜Έν™” κΈ°λŠ₯을 ν™œμš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

문법:

crypto λͺ¨λ“ˆμ„ μ‚¬μš©ν•˜λ €λ©΄ λ¨Όμ € λͺ¨λ“ˆμ„ 가져와야 ν•©λ‹ˆλ‹€.
const crypto = require('crypto');
JavaScript
볡사

μ£Όμš” 속성 및 λ©”μ†Œλ“œ

ν•΄μ‹œ 생성

β€’
crypto.createHash(algorithm) : μ§€μ •λœ ν•΄μ‹œ μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•˜μ—¬ ν•΄μ‹œ 객체λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
κ°€λŠ₯ν•œ ν•΄μ‹œ μ•Œκ³ λ¦¬μ¦˜ : md5, sha1, sha256, sha512, ripemd160

HMAC (Hash-based Message Authentication Code) 생성

β€’
crypto.createHmac(algorithm, key) : μ§€μ •λœ ν•΄μ‹œ μ•Œκ³ λ¦¬μ¦˜κ³Ό ν‚€λ₯Ό μ‚¬μš©ν•˜μ—¬ HMAC 객체λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.

μ•”ν˜Έν™” 및 λ³΅ν˜Έν™”

λ©”μ†Œλ“œ
μ„€λͺ…
crypto.createCipher(algorithm, password)
μ§€μ •λœ μ•Œκ³ λ¦¬μ¦˜κ³Ό λΉ„λ°€λ²ˆν˜Έλ₯Ό μ‚¬μš©ν•˜μ—¬ μ•”ν˜Έν™” 객체λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
crypto.createDecipher(algorithm, password)
μ§€μ •λœ μ•Œκ³ λ¦¬μ¦˜κ³Ό λΉ„λ°€λ²ˆν˜Έλ₯Ό μ‚¬μš©ν•˜μ—¬ λ³΅ν˜Έν™” 객체λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
crypto.createCipheriv(algorithm, key, iv)
μ§€μ •λœ μ•Œκ³ λ¦¬μ¦˜, ν‚€, μ΄ˆκΈ°ν™” 벑터(iv)λ₯Ό μ‚¬μš©ν•˜μ—¬ μ•”ν˜Έν™” 객체λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
crypto.createDecipheriv(algorithm, key, iv)
μ§€μ •λœ μ•Œκ³ λ¦¬μ¦˜, ν‚€, μ΄ˆκΈ°ν™” 벑터(iv)λ₯Ό μ‚¬μš©ν•˜μ—¬ λ³΅ν˜Έν™” 객체λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.

기타

λ©”μ†Œλ“œ
μ„€λͺ…
crypto.randomBytes(size, callback)
μ§€μ •λœ 크기의 λ¬΄μž‘μœ„ λ°”μ΄νŠΈλ₯Ό μƒμ„±ν•˜μ—¬ 콜백 ν•¨μˆ˜μ— μ „λ‹¬ν•©λ‹ˆλ‹€.
crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)
λΉ„λ°€λ²ˆν˜Έ 기반 ν‚€ νŒŒμƒ ν•¨μˆ˜(PBKDF2)λ₯Ό μˆ˜ν–‰ν•˜μ—¬ 콜백 ν•¨μˆ˜μ— ν‚€ 값을 μ „λ‹¬ν•©λ‹ˆλ‹€.

μ˜ˆμ‹œ μ½”λ“œ

const crypto = require('crypto'); // ν•΄μ‹œ 생성 μ˜ˆμ‹œ const hash = crypto.createHash('sha256'); hash.update('Hello, World!'); const hashedData = hash.digest('hex'); console.log('Hashed Data:', hashedData); // HMAC 생성 μ˜ˆμ‹œ const hmac = crypto.createHmac('sha256', 'secret-key'); hmac.update('Hello, World!'); const hmacResult = hmac.digest('hex'); console.log('HMAC Result:', hmacResult); // μ•”ν˜Έν™” 및 λ³΅ν˜Έν™” μ˜ˆμ‹œ (AES μ•Œκ³ λ¦¬μ¦˜ μ‚¬μš©) const algorithm = 'aes-256-cbc'; const key = 'encryption-key'; const iv = crypto.randomBytes(16); const cipher = crypto.createCipheriv(algorithm, key, iv); let encryptedData = cipher.update('Sensitive Data', 'utf-8', 'hex'); encryptedData += cipher.final('hex'); console.log('Encrypted Data:', encryptedData); const decipher = crypto.createDecipheriv(algorithm, key, iv); let decryptedData = decipher.update(encryptedData, 'hex', 'utf-8'); decryptedData += decipher.final('utf-8'); console.log('Decrypted Data:', decryptedData);
JavaScript
볡사

sha256 λ°©μ‹μœΌλ‘œ λΉ„λ°€λ²ˆν˜Έ μ•”ν˜Έν™”

const crypto = require('crypto'); const password = '123456'; const salt = 'random_salt'; // μž„μ˜μ˜ μ†”νŠΈ κ°’ // μ†”νŠΈμ™€ λΉ„λ°€λ²ˆν˜Έλ₯Ό κ²°ν•©ν•˜μ—¬ ν•΄μ‹œν™” const hashedPassword = crypto.createHash('sha256').update(salt + password).digest('hex'); console.log('Hashed Password:', hashedPassword); // readline : μ‚¬μš©μž μž…λ ₯을 λ°›κΈ° μœ„ν•œ λͺ¨λ“ˆ const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // λΉ„λ°€λ²ˆν˜Έ μž…λ ₯ rl.question('Enter your password: ', (userInputPassword) => { // μž…λ ₯된 λΉ„λ°€λ²ˆν˜Έ console.log('You entered:', userInputPassword); // λΉ„λ°€λ²ˆν˜Έ 검증 const hashedInputPassword = crypto.createHash('sha256').update(salt + userInputPassword).digest('hex'); // μ‚¬μš©μžκ°€ μž…λ ₯ν•œ λΉ„λ°€λ²ˆν˜Έμ™€ μ €μž₯된 ν•΄μ‹œλœ λΉ„λ°€λ²ˆν˜Έλ₯Ό 비ꡐ if (hashedInputPassword === hashedPassword) { console.log('Password Match: User is authenticated.'); } else { console.log('Password Mismatch: User authentication failed.'); } // μ‚¬μš©μ΄ λλ‚˜λ©΄ μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ‹«μŒ rl.close(); }); // μΈν„°νŽ˜μ΄μŠ€κ°€ λ‹«νž λ•Œμ˜ 이벀트 λ¦¬μŠ€λ„ˆ rl.on('close', () => { console.log('Input interface closed.'); });
JavaScript
볡사

μ‚¬μš© λͺ©μ 

β€’
μ‚¬μš©μž μ•”ν˜Έμ˜ μ•ˆμ „ν•œ μ €μž₯
β€’
데이터 무결성 검증을 μœ„ν•œ ν•΄μ‹œ 생성
β€’
λ°μ΄ν„°μ˜ 기밀성을 μœ„ν•œ μ•”ν˜Έν™” 및 λ³΅ν˜Έν™”
β€’
HMAC을 μ‚¬μš©ν•œ λ©”μ‹œμ§€ 인증
β€’
λ¬΄μž‘μœ„ λ°”μ΄νŠΈ 생성 λ“± λ‹€μ–‘ν•œ λ³΄μ•ˆ κ΄€λ ¨ μž‘μ—…μ— ν™œμš©λ©λ‹ˆλ‹€.

μ°Έκ³ 

bcrypt λ°©μ‹μœΌλ‘œ μ•”ν˜Έν™”

β€’
bcrypt λͺ¨λ“ˆ μ„€μΉ˜
npm install bcrypt
JavaScript
볡사
β€’
bcrypt μ•”ν˜Έν™”
const bcrypt = require('bcrypt'); const readline = require('readline'); const saltRounds = 10; // μ†”νŠΈλ₯Ό μƒμ„±ν•˜λŠ” λΌμš΄λ“œ 수 const salt = bcrypt.genSaltSync(saltRounds); // μ†”νŠΈ 생성 const password = '123456'; const hashedPassword = bcrypt.hashSync(password, salt); console.log('Hashed Password:', hashedPassword); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // λΉ„λ°€λ²ˆν˜Έ μž…λ ₯ rl.question('Enter your password: ', (userInputPassword) => { // μž…λ ₯된 λΉ„λ°€λ²ˆν˜Έ console.log('You entered:', userInputPassword); // λΉ„λ°€λ²ˆν˜Έ 검증 bcrypt.compare(userInputPassword, hashedPassword, (err, result) => { if (err) { console.error('Comparison Error:', err); } else { if (result) { console.log('Password Match: User is authenticated.'); } else { console.log('Password Mismatch: User authentication failed.'); } } // μ‚¬μš©μ΄ λλ‚˜λ©΄ μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ‹«μŒ rl.close(); }); }); // μΈν„°νŽ˜μ΄μŠ€κ°€ λ‹«νž λ•Œμ˜ 이벀트 λ¦¬μŠ€λ„ˆ rl.on('close', () => { console.log('Input interface closed.'); });
JavaScript
볡사