* rewrite

* testing worklflow

* sprcify version

* config stuff

* add hash as buffer

* delete docs

* use typedarray everywhere and docs

* readme

* aes

* cleanup

* rsa

* testing with playwright

* fix playwright

* readme

* docs

* update deps

* use headless

* add prepublish

* build pipeline

* move types up

* move types up

* add types legacy

* packaging

* versions bump

* cleanup

* maybe this time

* drop support for commonjs

* version bump

* cleanup
This commit is contained in:
2022-10-18 15:53:43 +02:00
committed by GitHub
parent f1f5e44b54
commit 56a8103582
58 changed files with 2701 additions and 4705 deletions

28
test/aes.spec.js Normal file
View File

@@ -0,0 +1,28 @@
import { AES, Bytes, Hashes, Hex } from '../dist/index.js'
import { Precomputed } from './values.js'
describe('AES', () => {
it('Basic API', async () => {
const message = Precomputed.Crypto.Messages.nietzscheIpsum
const data = Bytes.encode(message)
const [key] = await AES.derive('foo', {
name: 'PBKDF2',
hash: Hashes.SHA_512,
iterations: 1000,
length: 256,
salt: Hex.decode(Precomputed.Crypto.Bytes[16]),
})
const ciphertext = await AES.encrypt(data, key, AES.Modes.GCM)
const plaintext = await AES.decrypt(ciphertext, key)
chai.expect(data).to.be.deep.equal(plaintext)
chai.expect(message).to.be.equal(Bytes.decode(plaintext))
})
it('Easy API', async () => {
const message = Precomputed.Crypto.Messages.nietzscheIpsum
const password = 'foobar'
const encrypted = await AES.encryptEasy(message, password)
const decrypted = await AES.decryptEasy(encrypted, password)
chai.expect(message).to.be.equal(decrypted)
})
})

16
test/encoding.spec.js Normal file
View File

@@ -0,0 +1,16 @@
import { Base64, Bytes } from '../dist/index.js'
import { Precomputed } from './values.js'
describe('Encoding', () => {
describe('Base64', () => {
for (const [input, output] of Object.entries(Precomputed.Encoding.Base64)) {
const buffer = Bytes.encode(input)
it(`Should encode ${input} to ${output}`, async () => {
chai.expect(await Base64.encode(buffer)).to.equal(output)
})
it(`Should decode ${output} to ${input}`, async () => {
chai.expect(await Base64.decode(output)).to.deep.equal(buffer)
})
}
})
})

23
test/hash.spec.js Normal file
View File

@@ -0,0 +1,23 @@
import { Bytes, Hash, Hashes, Hex } from '../dist/index.js'
import { Precomputed } from './values.js'
describe('Hash', () => {
for (const type of Object.keys(Hashes)) {
describe(type, () => {
const values = Precomputed.Hash[type]
for (const [input, output] of Object.entries(values)) {
it(`Should hash "${input}" to "${output.slice(0, 8)}..."`, async () => {
const hashed = await Hash.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.hash(inputBuffer, Hashes[type])
chai.expect(hashed).to.deep.equal(outputBuffer)
})
}
})
}
})

12
test/hooks.js Normal file
View File

@@ -0,0 +1,12 @@
/**
* Hook for mocha tests in node
* Initialises chai and chai-as-promised as global variables
*/
export const mochaHooks = {
async beforeEach() {
if (typeof chai === 'undefined') {
global.chai = await import('chai')
}
},
}

View File

@@ -1,89 +0,0 @@
const { describe, it } = require('mocha')
const a = require('assert')
describe('Check imports', () => {
it('default', () => {
const Occulto = require('../').default
a.notStrictEqual(undefined, Occulto.RSA)
a.notStrictEqual(undefined, Occulto.Symmetric)
})
it('normal', () => {
const { RSA, Symmetric } = require('../')
a.notStrictEqual(undefined, RSA)
a.notStrictEqual(undefined, Symmetric)
})
})
const { RSA, Symmetric, Hash } = require('../')
describe('Asymmetric', () => {
describe('RSA', () => {
it('Encrypt and Decrypt small string', async () => {
const pair = await RSA.gen(2 ** 10)
const text = `some small string`
const e = RSA.encrypt(text, pair.pub)
const d = RSA.decrypt(e, pair.prv)
a.strictEqual(text, d)
})
})
})
describe('Symmetric', () => {
const keyLong = `yruyLaCAbcfJD9DKk3Ef6zuSFMPatwbg63ayPHVDSSxAePqtYxmd7N5BX2ShCgaG`
const keyShort = `simple`
const dataShort = `a sentence`
const dataLong = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`
describe('Symmetric', () => {
describe('Default', () => {
it('Short', () => {
const e = Symmetric.encrypt(dataShort, keyShort)
const d = Symmetric.decrypt(e, keyShort)
a.strictEqual(dataShort, d)
})
it('Long', () => {
const e = Symmetric.encrypt(dataLong, keyLong)
const d = Symmetric.decrypt(e, keyLong)
a.strictEqual(dataLong, d)
})
})
describe('All Ciphers', () => {
for (const [key, value] of Object.entries(Symmetric.Ciphers)) {
if (!isNaN(key)) continue
it(key, () => {
const e = Symmetric.encrypt(dataShort, keyShort, value)
const d = Symmetric.decrypt(e, keyShort)
a.strictEqual(dataShort, d)
})
}
})
})
})
describe('Hashes', () => {
const dataShort = `a sentence`
const dataLong = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`
describe('All Hashes', () => {
for (const [key, value] of Object.entries(Hash.Hashes)) {
if (!isNaN(key)) continue
it(key, () => {
const short = Hash.digest(dataShort, value)
a.notStrictEqual(undefined, short)
const long = Hash.digest(dataLong, value)
a.notStrictEqual(undefined, long)
})
}
})
})

18
test/random.spec.js Normal file
View File

@@ -0,0 +1,18 @@
import { getRandomBytes } from '../dist/index.js'
import { Promises } from './utils.js'
describe('Random', () => {
it('Should be able to create random values', async () => {
const buffer = await getRandomBytes(8)
chai.expect(buffer).to.be.instanceOf(Uint8Array)
chai.expect(buffer.byteLength).to.equal(8)
})
it('Should throw error on empty array', async () => {
await Promises.reject(() => getRandomBytes(0))
})
it('Should throw error on negative bytes', async () => {
await Promises.reject(() => getRandomBytes(-1))
})
})

41
test/rsa.spec.js Normal file
View File

@@ -0,0 +1,41 @@
import { Bytes, RSA } from '../dist/index.js'
import { Promises } from './utils.js'
import { Precomputed } from './values.js'
describe('RSA', () => {
describe('Generate keys', function () {
this.timeout(5_000)
it('Should be able to generate a keypair', async () => {
await RSA.generateKeyPair()
})
it('Should be able to generate a keypair with 2048bit', async () => {
await RSA.generateKeyPair(2048)
})
it('Should be able to generate a keypair with 4096bit', async () => {
await RSA.generateKeyPair(4096)
})
it('Should not be able to generate a key below 2048bit', async () => {
await Promises.reject(() => RSA.generateKeyPair(1024))
})
it('Should not be able to generate a key below 2048bit', async () => {
await Promises.reject(() => RSA.generateKeyPair(-1))
})
})
describe('Encryption', () => {
for (const message of Object.values(Precomputed.Crypto.Messages)) {
it(`Should be able to encrypt and decrypt "${message.slice(0, 8)}..."`, async () => {
const pair = await RSA.generateKeyPair(2 ** 11)
const bytes = Bytes.encode(message)
try {
const encrypted = await RSA.encrypt(bytes, pair.public)
chai.expect.fail('Should have thrown error')
const decrypted = await RSA.decrypt(encrypted, pair.private)
chai.expect(decrypted).to.be.deep.equal(bytes)
chai.expect(message).to.be.equal(Bytes.decode(decrypted))
} catch {}
})
}
})
})

8
test/utils.js Normal file
View File

@@ -0,0 +1,8 @@
export class Promises {
static async reject(fn) {
try {
await fn()
chai.expect.fail('Should have thrown error')
} catch {}
}
}

46
test/values.js Normal file
View File

@@ -0,0 +1,46 @@
export const Precomputed = {
Encoding: {
Base64: {
occulto: 'b2NjdWx0bw==',
test: 'dGVzdA==',
'hello world': 'aGVsbG8gd29ybGQ=',
},
},
Hash: {
SHA_1: {
test: 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3',
'hello world': '2aae6c35c94fcfb415dbe95f408b9ce91ee846ed',
occulto: 'f4b27cfb9e01492f409295fbbc339753fa839c0f',
},
SHA_256: {
test: '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
'hello world': 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9',
occulto: 'df2b97515886051821e4375a33df10486ce55cb3d14acd05dd7465f820ef2481',
},
SHA_384: {
test: '768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9',
'hello world': 'fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd',
occulto: '133c1968e937462ed66732409fa305c63335ee62b1114dd2d0ae98b4dd8fa6aca4656c919b295e41efa2d63f0d3c9951',
},
SHA_512: {
test: 'ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff',
'hello world':
'309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f',
occulto:
'9f7ff06148d415b12290ab7c21f021964ed627574f94f66c994aad4a8e319aa3168a9871edace3e736096cbd957cafa42dbf3feb6efd7763bf936ddc933c9470',
},
},
Crypto: {
Bytes: {
14: 'e0b28a24252963ff30dd2bb3ec9c',
16: '65eeb2044e9eb115956dbf4d0d70cd8f',
24: '9fa9e0aace3b0bdcbc871aa3ee3ddb1bece759b811fa4603',
32: '848ca08f01f82e28bfa91c85d55ef2a98afd8b32c707c9c790e86b1c53a177e4',
},
Messages: {
test: 'test',
nietzscheIpsum:
'Marvelous intentions joy deceptions overcome sexuality spirit against. Selfish of marvelous play dead war snare eternal-return ultimate. Reason aversion suicide.',
},
},
}