added utils and hashing

This commit is contained in:
cupcakearmy 2019-04-10 10:42:00 +02:00
parent df89829327
commit 6096036063
3 changed files with 45 additions and 5 deletions

18
src/Hash.ts Normal file
View File

@ -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')
}

View File

@ -1,4 +1,20 @@
import { randomBytes } from 'crypto'
export class Base64 { export class Base64 {
static encode = (s: string): string => Buffer.from(s).toString('base64') static encode = (s: string): string => Buffer.from(s).toString('base64')
static decode = (s: string): string => Buffer.from(s, 'base64').toString() 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
} }

View File

@ -1,14 +1,20 @@
import RSA_Internal from './RSA' import H from './Hash'
import Symmetric_Internal from './Symmetric' import R from './RSA'
import S from './Symmetric'
import U from './Util'
export const RSA = RSA_Internal export const RSA = R
export const Symmetric = Symmetric_Internal export const Symmetric = S
export const Hash = H
export const Util = U
export default { export default {
RSA, RSA,
Symmetric, Symmetric,
Hash,
Util,
} }
// Require node 11 // Require node 11
if (parseInt(process.versions.node.split('.')[0]) < 10) throw new Error('Node 10 or higher is required') if (parseInt(process.versions.node.split('.')[0]) < 11) throw new Error('Node 11 or higher is required')