Last active
September 13, 2019 00:21
-
-
Save samsoft00/337da2694691289b54b6c185a69281ec to your computer and use it in GitHub Desktop.
Word Encryption using crypto
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
import * as crypto from 'crypto'; | |
export default class WordEncryptor{ | |
salt: Buffer; | |
algorithm: string; | |
key:Buffer; | |
iv: Buffer; | |
constructor(password: string) { | |
this.salt = crypto.randomBytes(32); | |
this.algorithm = 'aes-256-cbc'; | |
this.iv = crypto.randomBytes(16); | |
this.key = crypto.scryptSync(password, this.salt, 32); | |
} | |
encrypt(textToEncrypt: string){ | |
let ciper = crypto.createCipheriv(this.algorithm, this.key, this.iv); | |
ciper.update(textToEncrypt, 'utf8', 'hex'); | |
return ciper.final('hex'); | |
} | |
decrypt(encriptedWord: string){ | |
const deciper = crypto.createDecipheriv(this.algorithm, this.key, this.iv); | |
deciper.update(encriptedWord, 'hex', 'utf8'); | |
return deciper.final('utf8'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment