diff --git a/src/Hash.ts b/src/Hash.ts index 40bbb61..e6a10dd 100644 --- a/src/Hash.ts +++ b/src/Hash.ts @@ -1,5 +1,6 @@ import { createHash } from 'crypto' + enum Hashes { MD5 = 'md5', SHA1_1 = 'sha1', @@ -14,5 +15,11 @@ export default class Hash { 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') } \ No newline at end of file diff --git a/src/RSA.ts b/src/RSA.ts index 34e51d0..6b4dc9d 100644 --- a/src/RSA.ts +++ b/src/RSA.ts @@ -11,6 +11,11 @@ export type KeyPair = { export default class RSA { + /** + * + * @param {number} size Number of bits for the key + * @returns {Promise} Object that contains the key pair + */ static gen = (size: number = 2 ** 12): Promise => new Promise((resolve, reject) => { // @ts-ignore 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() } \ No newline at end of file diff --git a/src/Symmetric.ts b/src/Symmetric.ts index ccda35c..4d40512 100644 --- a/src/Symmetric.ts +++ b/src/Symmetric.ts @@ -46,18 +46,25 @@ export default class Symmetric { static Ciphers = Ciphers 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 iv = randomBytes(ivSize) 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 cipher: CipherGCM | CipherCCM | Cipher = createCipheriv(alg, key, iv, options) + + const options: TransformOptions | undefined = mac ? { authTagLength: mac } as TransformOptions : undefined + const cipher: CipherGCM | CipherCCM | Cipher = createCipheriv(alg, keyBuffered, iv, options) let content: Buffer = Buffer.concat([ - cipher.update(data), + cipher.update(Buffer.from(plain)), cipher.final(), ]) @@ -78,13 +85,19 @@ export default class Symmetric { 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 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 if (tag) decipher.setAuthTag(Buffer.from(tag, Symmetric.Encoding)) diff --git a/src/Util.ts b/src/Util.ts index a1c46cf..65105c5 100644 --- a/src/Util.ts +++ b/src/Util.ts @@ -5,7 +5,12 @@ export class Base64 { 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: true): string function Rand(length: number, string: boolean = false): Buffer | string {