Skip to content

Instantly share code, notes, and snippets.

@flxxyz
Created October 23, 2023 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flxxyz/7cbd91ab6a20deef229f97f04863c3e0 to your computer and use it in GitHub Desktop.
Save flxxyz/7cbd91ab6a20deef229f97f04863c3e0 to your computer and use it in GitHub Desktop.
import crypto from 'crypto';
export default class AESCipher {
#key: Buffer;
constructor(key: string) {
const hash = crypto.createHash('sha256');
hash.update(key);
this.#key = hash.digest();
}
decrypt(encrypt: string) {
const encryptBuffer = Buffer.from(encrypt, 'base64');
const decipher = crypto.createDecipheriv('aes-256-cbc', this.#key, encryptBuffer.slice(0, 16));
let decrypted = decipher.update(encryptBuffer.slice(16).toString('hex'), 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment