From 0c08dcc67883ece6710dea9a4b715190d3c19af6 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Fri, 14 Oct 2022 13:31:52 +0200 Subject: [PATCH] add hash as buffer --- README.md | 56 ++++++++++++++++++++++++++++++++++++++++++ src/crypto/encoding.ts | 4 +-- src/crypto/hash.ts | 10 +++++--- src/index.ts | 9 +------ test/hash.spec.js | 11 +++++++-- 5 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..f94714b --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +# occulto 🔒 + +High level wrapper around the [node native crypto API](https://nodejs.org/api/crypto.html). + +**No Deps & Typescript typings included** + +Supports Hashes, Symmetric AES & ChaCha20 ciphers and Asymmetric RSA. + +[**📒 DOCS HERE 📒**](https://cupcakearmy.github.io/occulto/index.html) + +## Quickstart 🚀 + +###### Requirements + +- Node >= 16 required + +###### Install + +``` +npm i occulto +``` + +### Examples + +## [RSA](https://cupcakearmy.github.io/occulto/modules/_rsa_.html) + +```typescript +import { RSA } from 'occulto' + +const pair = await RSA.gen() +const encrypted = RSA.encrypt('some text', pair.pub) +const decrypted = RSA.decrypt(encrypted, pair.prv) +``` + +## [Symmetric](https://cupcakearmy.github.io/occulto/modules/_symmetric_.html) + +[Available Ciphers](https://cupcakearmy.github.io/occulto/enums/_symmetric_.ciphers.html) + +```javascript +import { Symmetric } from 'occulto' + +const encrypted = Symmetric.encrypt('some string', 'myPass', Symmetric.Ciphers.AES_128_GCM) +const decrypted = Symmetric.decrypt(encrypted, 'myPadd') +``` + +## [Hash](https://cupcakearmy.github.io/occulto/modules/_hash_.html) + +[Available hashes](https://cupcakearmy.github.io/occulto/enums/_hash_.hashes.html) + +```typescript +import { Hash } from 'occulto' + +const hash = Hash.digest('something') + +const h = Hash.digest('something', Hash.Hashes.MD5) +``` diff --git a/src/crypto/encoding.ts b/src/crypto/encoding.ts index 4492bdf..a0b00e1 100644 --- a/src/crypto/encoding.ts +++ b/src/crypto/encoding.ts @@ -25,7 +25,7 @@ export class Base64 { } export class Hex { - static encode(buffer: ArrayBuffer): string { + static encode(buffer: Uint8Array): string { let s = '' for (const i of new Uint8Array(buffer)) { s += i.toString(16).padStart(2, '0') @@ -33,7 +33,7 @@ export class Hex { return s } - static decode(s: string): ArrayBuffer { + static decode(s: string): Uint8Array { const size = s.length / 2 const buffer = new Uint8Array(size) for (let i = 0; i < size; i++) { diff --git a/src/crypto/hash.ts b/src/crypto/hash.ts index 411b5cc..fd2f76d 100644 --- a/src/crypto/hash.ts +++ b/src/crypto/hash.ts @@ -8,8 +8,12 @@ export enum Hashes { SHA_512 = 'SHA-512', } -export async function hash(data: string, hash: Hashes): Promise { +export async function hash(data: string, hash: Hashes): Promise +export async function hash(data: Uint8Array, hash: Hashes): Promise +export async function hash(data: string | Uint8Array, hash: Hashes): Promise { + const isString = typeof data === 'string' const c = await getCrypto() - const result = await c.subtle.digest(hash, Bytes.encode(data)) - return Hex.encode(result) + const result = await c.subtle.digest(hash, isString ? Bytes.encode(data) : data) + const buf = new Uint8Array(result) + return isString ? Hex.encode(buf) : buf } diff --git a/src/index.ts b/src/index.ts index 6f48d14..543086d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,3 @@ -export { Base64, Bytes } from './crypto/encoding.js' +export { Base64, Bytes, Hex } from './crypto/encoding.js' export { hash, Hashes } from './crypto/hash.js' export { getRandomBytes } from './crypto/random.js' - -import { isBrowser } from './utils/base.js' - -export function sum(a: number, b: number): number { - console.log(`Executing from: ${isBrowser ? 'browser' : 'server'}`) - return a + b -} diff --git a/test/hash.spec.js b/test/hash.spec.js index 16a618f..433e0d2 100644 --- a/test/hash.spec.js +++ b/test/hash.spec.js @@ -1,4 +1,4 @@ -import { hash, Hashes } from '../dist/esm/index.js' +import { Bytes, hash, Hashes, Hex } from '../dist/esm/index.js' import { Precomputed } from './values.js' describe('Hash', () => { @@ -6,10 +6,17 @@ describe('Hash', () => { describe(type, () => { const values = Precomputed.Hash[type] for (const [input, output] of Object.entries(values)) { - it(`Should hash ${input} to ${output}`, async () => { + it(`Should hash "${input}" to "${output.slice(0, 8)}..."`, async () => { const hashed = await hash(input, Hashes[type]) chai.expect(hashed).to.equal(output) }) + + it(`Should hash "${input}" to "${output.slice(0, 8)}..." as buffer`, async () => { + const outputBuffer = Hex.decode(output) + const inputBuffer = Bytes.encode(input) + const hashed = await hash(inputBuffer, Hashes[type]) + chai.expect(hashed).to.deep.equal(outputBuffer) + }) } }) }