mirror of
https://github.com/cupcakearmy/occulto.git
synced 2024-12-21 17:56:27 +00:00
added chacha20 & hashes
This commit is contained in:
parent
e9ed8f0e73
commit
a3a4b9ca18
@ -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')
|
||||
}
|
21
src/RSA.ts
21
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<KeyPair>} Object that contains the key pair
|
||||
*/
|
||||
static gen = (size: number = 2 ** 12): Promise<KeyPair> => new Promise<KeyPair>((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()
|
||||
|
||||
}
|
@ -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))
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user