Skip to content

Instantly share code, notes, and snippets.

@samsoft00
Last active September 13, 2019 00:21
Show Gist options
  • Save samsoft00/337da2694691289b54b6c185a69281ec to your computer and use it in GitHub Desktop.
Save samsoft00/337da2694691289b54b6c185a69281ec to your computer and use it in GitHub Desktop.
Word Encryption using crypto
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