Created
October 23, 2023 15:58
-
-
Save flxxyz/7cbd91ab6a20deef229f97f04863c3e0 to your computer and use it in GitHub Desktop.
This file contains 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
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