const bcrypt = require("bcrypt"); // (1)
const password = 'password'; // (2)
const saltRounds = 12; // (3)
const makeHash = async (password, saltRounds) => {
return await bcrypt.hash(password, saltRounds); // (4)
}
const main = async () => {
const hashedPassword = await makeHash(password, saltRounds);
console.log(hashedPassword);
}
main()
=> b'$2b$12$76taFAFPE9ydE0ZsuWkIZexWVjLBbTTHWc509/OLI5nM9d5r3fkRG'
첫번째 인자 - 암호화하고 싶은 평문
두번째 인자 - Cost Factor
const checkHash = async (password, hashedPassword) => {
return await bcrypt.compare(password, hashedPassword) // (1)
}
const main = async () => {
const hashedPassword = await makeHash("password", 12);
const result = await checkHash("password", hashedPassword);
console.log(result);
};
main()
=> true(or false) // (2)
첫번째 인자 - 평문
두번쨰 인자 - 암호화 된 값
const jwt = require('jsonwebtoken'); // (1)
const payLoad = { foo: 'bar' }; // (2)
const secretKey = 'mySecretKey'; // (3)
const jwtToken = jwt.sign(payLoad, secretKey); // (4)
console.log(jwtToken)
=> 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE2NTA1NTYxMzZ9.YAMgUMLhiVUwkRTr2rpOrIyWN0cTGLxsxZBqLAaKWUU'
첫번째 인자 - payload
두번째 인자 - secretKey
세번째 인자 - 선택 가능, 존재하지 않으면 HS256 알고리즘으로 JWT 발급
const decoded = jwt.verify(jwtToken, secretKey); // (1)
console.log(decoded)
=> { foo: 'bar', iat: 1650555667 } // (2)
첫번째 인자 - JWT
두번째 인자 - secretKey