반응형
CryptoUtils.js
import crypto from "crypto" ;
/**
* sha512 해싱 알고리즘
* @description 단방향
* @param {String} text 암호화할 평문.
*/
export const sha512 = (text) => crypto.createHash('sha512').update(text).digest('base64')
let data = sha512("가나다라마바사")
console.log(` sha512 : ${data}`)
AES256Utils.js
import crypto from "crypto" ;
const aesKey = crypto.randomBytes(32) // 32 byte
const iv = Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
console.log('iv = ',iv)
/**
*
* @param {String} text
*/
export const aes256Encrypt = (text) => {
let cipher = crypto.createCipheriv('aes-256-cbc', aesKey, iv);
let result = cipher.update(text, 'utf8', 'base64');
result += cipher.final('base64');
return result
}
export const aes256Decrypt = (cryptogram) => {
const decipher = crypto.createDecipheriv('aes-256-cbc', aesKey, iv);
let result = decipher.update(cryptogram, 'base64', 'utf8');
result += decipher.final('utf8');
return result
}
let plainTextData = "id=kdy&ts=1597802688"
let encryptStr = aes256Encrypt(plainTextData)
let decryptStr = aes256Decrypt(encryptStr)
console.log(" encryptStr : ", encryptStr)
console.log(" decryptStr : ", decryptStr)
index.js
import * as AESCrypto from "./crypto/Aes256Utils.js"
import {sha512} from "./crypto/CryptoUtils.js"
// promise test
const Application = async () => {
console.log(" Application Running")
return {key:"key", value :"value"}
}
Application().then(
r=> {
console.log(`application run code = ${r}`)
let plainTextData = "id=kdy&ts=1597802688"
let encryptStr = AESCrypto.aes256Encrypt(plainTextData)
let decryptStr = AESCrypto.aes256Decrypt(encryptStr)
console.log(" encryptStr : ", encryptStr)
console.log(" decryptStr : ", decryptStr)
let data = sha512("가나다라마바사")
console.log(` sha512 : ${data}`)
}
).catch((error=>{
console.log('error', error)
}))
반응형
'JavaScript' 카테고리의 다른 글
javascript module , import 와 requires (0) | 2021.12.10 |
---|---|
쓸만한 위지윅 에디터 (웹에디터) 추천 (0) | 2020.10.16 |
javascript cryptojs 예제 AES 256 (1) | 2020.08.28 |