mirror of
https://github.com/cupcakearmy/occulto.git
synced 2024-12-22 09:06:27 +00:00
add hash as buffer
This commit is contained in:
parent
7a6a24d631
commit
0c08dcc678
56
README.md
Normal file
56
README.md
Normal file
@ -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)
|
||||
```
|
@ -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++) {
|
||||
|
@ -8,8 +8,12 @@ export enum Hashes {
|
||||
SHA_512 = 'SHA-512',
|
||||
}
|
||||
|
||||
export async function hash(data: string, hash: Hashes): Promise<string> {
|
||||
export async function hash(data: string, hash: Hashes): Promise<string>
|
||||
export async function hash(data: Uint8Array, hash: Hashes): Promise<Uint8Array>
|
||||
export async function hash(data: string | Uint8Array, hash: Hashes): Promise<string | Uint8Array> {
|
||||
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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user