added chacha20 & hashes

This commit is contained in:
cupcakearmy 2019-07-07 21:20:05 +02:00
parent e9ed8f0e73
commit a3a4b9ca18
4 changed files with 56 additions and 14 deletions

View File

@ -1,5 +1,6 @@
import { createHash } from 'crypto' import { createHash } from 'crypto'
enum Hashes { enum Hashes {
MD5 = 'md5', MD5 = 'md5',
SHA1_1 = 'sha1', SHA1_1 = 'sha1',
@ -14,5 +15,11 @@ export default class Hash {
static Hashes = Hashes static Hashes = Hashes
static digest = (s: string, type: Hashes = Hashes.SHA3_256): string => createHash(type).update(s).digest().toString('hex') /**
*
* @param {string} plain Input string
* @param {Hashes} type Type of hash to be used to digest
* @returns {string} Hash of the plain text
*/
static digest = (plain: string, type: Hashes = Hashes.SHA3_256): string => createHash(type).update(plain).digest().toString('hex')
} }

View File

@ -11,6 +11,11 @@ export type KeyPair = {
export default class RSA { export default class RSA {
/**
*
* @param {number} size Number of bits for the key
* @returns {Promise<KeyPair>} Object that contains the key pair
*/
static gen = (size: number = 2 ** 12): Promise<KeyPair> => new Promise<KeyPair>((resolve, reject) => { static gen = (size: number = 2 ** 12): Promise<KeyPair> => new Promise<KeyPair>((resolve, reject) => {
// @ts-ignore // @ts-ignore
generateKeyPair('rsa', { generateKeyPair('rsa', {
@ -26,8 +31,20 @@ export default class RSA {
}) })
}) })
static encrypt = (data: string, key: PublicKey): string => publicEncrypt(key, Buffer.from(data)).toString('base64') /**
*
* @param {string} plain
* @param {PublicKey} key
* @returns {string} Encrypted string
*/
static encrypt = (plain: string, key: PublicKey): string => publicEncrypt(key, Buffer.from(plain)).toString('base64')
static decrypt = (data: string, key: PrivateKey): string => privateDecrypt(key, Buffer.from(data, 'base64')).toString() /**
*
* @param {string} encrypted
* @param {PrivateKey} key
* @returns {string} Decrypted string
*/
static decrypt = (encrypted: string, key: PrivateKey): string => privateDecrypt(key, Buffer.from(encrypted, 'base64')).toString()
} }

View File

@ -46,18 +46,25 @@ export default class Symmetric {
static Ciphers = Ciphers static Ciphers = Ciphers
static Encoding: BufferEncoding = 'base64' static Encoding: BufferEncoding = 'base64'
static encrypt(data: string, pass: string, type: Ciphers = Ciphers.AES_256_CTR): string { /**
*
* @param plain {string} data The data to be encrypted
* @param key {string} The encryption key
* @param type {Ciphers} [type=Ciphers.AES_256_CTR] The cipher that will be used
* @returns {string} Encrypted data as string
*/
static encrypt(plain: string, key: string, type: Ciphers = Ciphers.AES_256_CTR): string {
const { alg, ivSize, mac, keySize } = Symmetric.getCipherConfig(type) const { alg, ivSize, mac, keySize } = Symmetric.getCipherConfig(type)
const iv = randomBytes(ivSize) const iv = randomBytes(ivSize)
const salt = randomBytes(keySize) const salt = randomBytes(keySize)
const key = scryptSync(pass, salt, keySize) const keyBuffered = scryptSync(Buffer.from(key), salt, keySize)
// @ts-ignore
const options: TransformOptions = mac ? { authTagLength: mac } : undefined const options: TransformOptions | undefined = mac ? { authTagLength: mac } as TransformOptions : undefined
const cipher: CipherGCM | CipherCCM | Cipher = createCipheriv(alg, key, iv, options) const cipher: CipherGCM | CipherCCM | Cipher = createCipheriv(alg, keyBuffered, iv, options)
let content: Buffer = Buffer.concat([ let content: Buffer = Buffer.concat([
cipher.update(data), cipher.update(Buffer.from(plain)),
cipher.final(), cipher.final(),
]) ])
@ -78,13 +85,19 @@ export default class Symmetric {
return Base64.encode(JSON.stringify(encrypted)) return Base64.encode(JSON.stringify(encrypted))
} }
static decrypt(e: string, pass: string): string { /**
const { alg, data, iv, tag, salt, keySize, tagSize }: EncryptedItem = JSON.parse(Base64.decode(e)) *
const key = scryptSync(pass, Buffer.from(salt, Symmetric.Encoding), keySize) * @param {string} encrypted The encrypted string
* @param {string} key The key used for encrypting
* @returns {string} The data as string
*/
static decrypt(encrypted: string, key: string): string {
const { alg, data, iv, tag, salt, keySize, tagSize }: EncryptedItem = JSON.parse(Base64.decode(encrypted))
const keyBuffered = scryptSync(Buffer.from(key), Buffer.from(salt, Symmetric.Encoding), keySize)
// @ts-ignore // @ts-ignore
const options: TransformOptions = tag ? { authTagLength: tagSize } : undefined const options: TransformOptions = tag ? { authTagLength: tagSize } : undefined
const decipher: DecipherGCM | DecipherCCM | Decipher = createDecipheriv(alg, key, Buffer.from(iv, Symmetric.Encoding), options) const decipher: DecipherGCM | DecipherCCM | Decipher = createDecipheriv(alg, keyBuffered, Buffer.from(iv, Symmetric.Encoding), options)
// @ts-ignore // @ts-ignore
if (tag) decipher.setAuthTag(Buffer.from(tag, Symmetric.Encoding)) if (tag) decipher.setAuthTag(Buffer.from(tag, Symmetric.Encoding))

View File

@ -5,7 +5,12 @@ export class Base64 {
static decode = (s: string): string => Buffer.from(s, 'base64').toString() static decode = (s: string): string => Buffer.from(s, 'base64').toString()
} }
/**
*
* @param {number} length Length of the generated string
* @param {boolean} string Whether to return a string or bytes
* @returns {Buffer|String} The random string or buffer
*/
function Rand(length: number, string: false): Buffer function Rand(length: number, string: false): Buffer
function Rand(length: number, string: true): string function Rand(length: number, string: true): string
function Rand(length: number, string: boolean = false): Buffer | string { function Rand(length: number, string: boolean = false): Buffer | string {