All files / src Utils.ts

100% Statements 16/16
100% Branches 0/0
100% Functions 3/3
100% Lines 16/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49  1x 1x           1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x                             1x     1x                  
import * as crypto from 'crypto';
 
/**
 * Utility methods
 */
export class Utils {
	public static encrypt(key: string, data: any) {
		const iv = crypto.randomBytes(16);
		const cipher = crypto.createCipheriv('aes-256-cbc', new Buffer(key), iv);
		let encrypted = cipher.update(data);
 
		encrypted = Buffer.concat([encrypted, cipher.final()]);
 
		return `${iv.toString('hex')}:${encrypted.toString('hex')}`;
	}
 
	public static decrypt(key: string, data: string) {
		const parts = data.split(':');
		const iv = new Buffer(parts.shift(), 'hex');
		const encryptedText = new Buffer(parts.join(':'), 'hex');
		const decipher = crypto.createDecipheriv('aes-256-cbc', new Buffer(key), iv);
		let decrypted = decipher.update(encryptedText);
 
		decrypted = Buffer.concat([decrypted, decipher.final()]);
 
		return decrypted.toString();
	}
 
	// public static encrypt(key: string, data: string): string {
	// 	const cipher = crypto.createCipher('aes256', key);
	// 	let result  = cipher.update(`${key}`, 'utf8', 'hex');
 
	// 	result += cipher.final();
	// 	return result;
	// }
 
	// public static decrypt(key: string, data: string): string {
	// 	const decipher = crypto.createDecipher('aes256', key);
	// 	let result = decipher.update(data, 'hex', 'utf8');
 
	// 	result += decipher.final('utf8');
	// 	return result;
	// }
 
	public static sha256(data: any): string {
		return crypto.createHash('sha256').update(data, 'utf8').digest('hex');
	}
}