const ps = process.env;
console.log(ps);
npm install dotenv
// .env 생성
NAME = sesac
NODE = dev
////////////
require("dotenv").config(); // .env 파일의 환경변수를 읽어옴
app.get("/", (req, res) => {
res.send("log");
console.log(process.env.NAME);
console.log(process.env.NODE);
});
npm install cross-env
"scripts": {
"start": "cross-env NODE_ENV=develeopment node index.js",
"start:prod": "cross-env NODE_ENV=production node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
해시 함수에 의해 얻어지는 값
충돌 저항성이 약하기 때문에 사용하지 않는 알고리즘 : MD5, SHA-1
대칭키 사용 : 암호화와 복호화에 동일한 키를 사용하므로 키 관리가 중요
두 개의 키 : 공개키와 개인키라는 두 개의 키 쌍을 사용
암호화 알고리즘이 모여 있는 패키지
const crypto = require("crypto");
const crypto = require("crypto");
const createHasedPassword = (pw) => {
return crypto.createHash("sha512").update(pw).digest("base64");
};
해시함수 한계! 레인보우 테이블
- 레인보우 테이블(Rainbow Table) : 해시함수를 사용해 만들어낼 수 있는 값들을 대량으로 저장해놓은 표
- 해시 함수를 사용하여 암호화된 비밀번호를 빠르게 역추적하여 원본 비밀번호를 찾는 공격 기법
const salt = crypto.randomBytes(16).toString('base64'); // 솔트 생성
const iterations = 100000; // 반복 횟수
const keylen = 64; // 생성할 키의 길이
const digest = 'sha512'; // 해시 알고리즘
const createPbkdf = (pw) => {
return crypto.pbkd2Sync(pw, salt, iterations, keylen, digest).toString('base64');
};
const verifyPassword = (password, salt, dbPassword) => {
const compare = crypto.pbkdf2Sync(password, salt, iterations, keylen, digest).toString('base64');
if (compare === dbPassword) return true;
return false;
};
const algorithm = "aes-256-chc"; // 알고리즘
const key = crypto.randomBytes(32); // 256비트 키
const iv = crypto.randomBytes(16); // 초기화 벡터
const cipherEncrypt = (word) => {
const cipher = crypto.createCipheriv(algorithm, key, iv); // 암호화 객체 생성
let encryptedData = cipher.update(word, "utf-8", "base64"); // 암호화할 데이터 처리
// word: 암호화 원본 데이터
// "utf-8": 입력 인코딩
// "base64": 출력 인코딩
encryptedData += cipher.final("base64");
return encryptedData;
};
const decipher = (encryptedData) => {
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decryptedData = decipher.update(encryptedData, "base64", "utf-8"); // 암호화할 데이터 처리
// encryptedData: 암호화 데이터
// "base64": 입력 인코딩
// "utf-8": 출력 인코딩
decryptedData += decipher.final("utf-8");
console.log("Decrypted:", decryptedData);
return encryptedData;
};
비밀번호를 암호화하는 알고리즘 중 하나
npm install bcrypt
const bcrypt = require("bcrypt");
const salt = 10; // 암호화에 사용할 salt의 수준 설정. 정수 사용
// 비밀번호 해싱 함수
const bcryptPassword = (password) => {
return bcrypt.hashSync(password, salt);
};
// 원본 비밀번호와 해시된 비밀번호가 일치하는 확인하는 함수
const comparePassword = (password, dbPassword) => {
return bcrypt.compareSync(password, dbPassword);
};