Created
November 27, 2019 11:30
-
-
Save nyawach/b656d6e94c98ea59bb40fd37d4a8ca8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* References: | |
* - https://qiita.com/Ishidall/items/bb0e0db86a2f56fb1022 | |
*/ | |
import * as crypto from "crypto" | |
const ENCRYPT_METHOD = "aes-256-cbc" // 暗号化方式 | |
const ENCRYPTION_KEY = "" // 32byte | |
const IV = Buffer.from("") // 16byte | |
const ENCODING = "hex" // 最終的な文字列のエンコード方式 | |
/** | |
* 暗号化 | |
* @param data 暗号化したいデータ | |
*/ | |
export const encrypt = (data: any) => { | |
const cipher = crypto.createCipheriv(ENCRYPT_METHOD, Buffer.from(ENCRYPTION_KEY), IV) | |
const encrypted = cipher.update(data) | |
const encryptedBuffer = Buffer.concat([encrypted, cipher.final()]) | |
return encryptedBuffer.toString(ENCODING) | |
} | |
/** | |
* 複合する | |
* @param encrypted 暗号化されたテキスト | |
*/ | |
export const decrypt = (encrypted: string) => { | |
const encryptedText = Buffer.from(encrypted, ENCODING) | |
const decipher = crypto.createDecipheriv(ENCRYPT_METHOD, Buffer.from(ENCRYPTION_KEY), IV) | |
const decrypted = decipher.update(encryptedText) | |
const decryptedBuffer = Buffer.concat([decrypted, decipher.final()]) | |
return decryptedBuffer.toString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment