This commit is contained in:
m5r
2021-08-01 22:03:49 +08:00
parent 7d34fcd48f
commit 1489f97c14
33 changed files with 147 additions and 313 deletions

View File

@ -7,9 +7,7 @@ const IV_LENGTH = 16;
const ALGORITHM = "aes-256-cbc";
export function encrypt(text: string, encryptionKey: Buffer | string) {
const encryptionKeyAsBuffer = Buffer.isBuffer(encryptionKey)
? encryptionKey
: Buffer.from(encryptionKey, "hex");
const encryptionKeyAsBuffer = Buffer.isBuffer(encryptionKey) ? encryptionKey : Buffer.from(encryptionKey, "hex");
const iv = crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv(ALGORITHM, encryptionKeyAsBuffer, iv);
const encrypted = cipher.update(text);
@ -19,9 +17,7 @@ export function encrypt(text: string, encryptionKey: Buffer | string) {
}
export function decrypt(encryptedHexText: string, encryptionKey: Buffer | string) {
const encryptionKeyAsBuffer = Buffer.isBuffer(encryptionKey)
? encryptionKey
: Buffer.from(encryptionKey, "hex");
const encryptionKeyAsBuffer = Buffer.isBuffer(encryptionKey) ? encryptionKey : Buffer.from(encryptionKey, "hex");
const [hexIv, hexText] = encryptedHexText.split(":");
const iv = Buffer.from(hexIv!, "hex");
const encryptedText = Buffer.from(hexText!, "hex");