From 6096036063aa4c1056dcb0747e4ee2a4f9850970 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Wed, 10 Apr 2019 10:42:00 +0200 Subject: [PATCH] added utils and hashing --- src/Hash.ts | 18 ++++++++++++++++++ src/Util.ts | 16 ++++++++++++++++ src/index.ts | 16 +++++++++++----- 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 src/Hash.ts diff --git a/src/Hash.ts b/src/Hash.ts new file mode 100644 index 0000000..40bbb61 --- /dev/null +++ b/src/Hash.ts @@ -0,0 +1,18 @@ +import { createHash } from 'crypto' + +enum Hashes { + MD5 = 'md5', + SHA1_1 = 'sha1', + SHA1_256 = 'sha256', + SHA1_512 = 'sha512', + SHA3_256 = 'sha3-256', + SHA3_384 = 'sha3-384', + SHA3_512 = 'sha3-512', +} + +export default class Hash { + + static Hashes = Hashes + + static digest = (s: string, type: Hashes = Hashes.SHA3_256): string => createHash(type).update(s).digest().toString('hex') +} \ No newline at end of file diff --git a/src/Util.ts b/src/Util.ts index 5396741..a1c46cf 100644 --- a/src/Util.ts +++ b/src/Util.ts @@ -1,4 +1,20 @@ +import { randomBytes } from 'crypto' + export class Base64 { static encode = (s: string): string => Buffer.from(s).toString('base64') static decode = (s: string): string => Buffer.from(s, 'base64').toString() +} + + +function Rand(length: number, string: false): Buffer +function Rand(length: number, string: true): string +function Rand(length: number, string: boolean = false): Buffer | string { + const r = randomBytes(length) + return string + ? r.toString('ascii') + : r +} + +export default { + Rand } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index daae3ce..fda4425 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,14 +1,20 @@ -import RSA_Internal from './RSA' -import Symmetric_Internal from './Symmetric' +import H from './Hash' +import R from './RSA' +import S from './Symmetric' +import U from './Util' -export const RSA = RSA_Internal -export const Symmetric = Symmetric_Internal +export const RSA = R +export const Symmetric = S +export const Hash = H +export const Util = U export default { RSA, Symmetric, + Hash, + Util, } // Require node 11 -if (parseInt(process.versions.node.split('.')[0]) < 10) throw new Error('Node 10 or higher is required') \ No newline at end of file +if (parseInt(process.versions.node.split('.')[0]) < 11) throw new Error('Node 11 or higher is required') \ No newline at end of file