update deps, cleanup

This commit is contained in:
2026-06-01 01:05:14 +02:00
parent 4c61c067e6
commit 51c3e3d05f
22 changed files with 1590 additions and 2542 deletions
+32 -32
View File
@@ -1,44 +1,44 @@
import { describe, expect, it } from 'vitest'
import { AES, Bytes, Hashes, Hex } from '../dist/index.js'
import { Precomputed } from './values.js'
import { describe, expect, it } from "vitest";
import { AES, Bytes, Hashes, Hex } from "../src/index.js";
import { Precomputed } from "./values.js";
describe('AES', () => {
describe("AES", () => {
for (const message of Object.values(Precomputed.Crypto.Messages)) {
describe(`Message: ${message.slice(0, 8)}...`, () => {
describe('Basic API', () => {
describe("Basic API", () => {
for (const keySize of [128, 256]) {
it('Key Size: ' + keySize, async () => {
const data = Bytes.encode(message)
const [key] = await AES.derive('foo', {
name: 'PBKDF2',
it("Key Size: " + keySize, async () => {
const data = Bytes.encode(message);
const [key] = await AES.derive("foo", {
name: "PBKDF2",
hash: Hashes.SHA_512,
iterations: 1000,
length: keySize,
salt: Hex.decode(Precomputed.Crypto.Bytes[16]),
})
const ciphertext = await AES.encrypt(data, key, AES.Modes.AES_GCM)
const plaintext = await AES.decrypt(ciphertext, key)
expect(data.buffer).toEqual(plaintext.buffer)
expect(message).toEqual(Bytes.decode(plaintext))
})
});
const ciphertext = await AES.encrypt(data, key, AES.Modes.AES_GCM);
const plaintext = await AES.decrypt(ciphertext, key);
expect(data.buffer).toEqual(plaintext.buffer);
expect(message).toEqual(Bytes.decode(plaintext));
});
}
})
});
it('Generated Key', async () => {
const key = await AES.generateKey()
const data = Bytes.encode(message)
const ciphertext = await AES.encrypt(data, key)
const plaintext = await AES.decrypt(ciphertext, key)
expect(data.buffer).toEqual(plaintext.buffer)
expect(message).toEqual(Bytes.decode(plaintext))
})
it("Generated Key", async () => {
const key = await AES.generateKey();
const data = Bytes.encode(message);
const ciphertext = await AES.encrypt(data, key);
const plaintext = await AES.decrypt(ciphertext, key);
expect(data.buffer).toEqual(plaintext.buffer);
expect(message).toEqual(Bytes.decode(plaintext));
});
it('Easy API', async () => {
const password = 'foobar'
const encrypted = await AES.encryptEasy(message, password)
const decrypted = await AES.decryptEasy(encrypted, password)
expect(message).toEqual(decrypted)
})
})
it("Easy API", async () => {
const password = "foobar";
const encrypted = await AES.encryptEasy(message, password);
const decrypted = await AES.decryptEasy(encrypted, password);
expect(message).toEqual(decrypted);
});
});
}
})
});
+25 -25
View File
@@ -1,38 +1,38 @@
import { describe, expect, it } from 'vitest'
import { Base64, Bytes, Hex } from '../dist/index.js'
import { Precomputed } from './values.js'
import { describe, expect, it } from "vitest";
import { Base64, Bytes, Hex } from "../src/index.js";
import { Precomputed } from "./values.js";
describe('Encoding', () => {
describe('Bytes', () => {
describe("Encoding", () => {
describe("Bytes", () => {
for (const [input, output] of Object.entries(Precomputed.Encoding.Bytes)) {
it(`Should encode ${input} to ${output}`, async () => {
expect(Bytes.encode(input).buffer).toEqual(output.buffer)
})
expect(Bytes.encode(input).buffer).toEqual(output.buffer);
});
it(`Should decode ${output} to ${input}`, async () => {
expect(Bytes.decode(output)).toEqual(input)
})
expect(Bytes.decode(output)).toEqual(input);
});
}
})
describe('Hex', () => {
});
describe("Hex", () => {
for (const [input, output] of Object.entries(Precomputed.Encoding.Hex)) {
const buffer = Bytes.encode(input)
const buffer = Bytes.encode(input);
it(`Should encode ${input} to ${output}`, async () => {
expect(Hex.encode(buffer)).toEqual(output)
})
expect(Hex.encode(buffer)).toEqual(output);
});
it(`Should decode ${output} to ${input}`, async () => {
expect(Hex.decode(output).buffer).toEqual(buffer.buffer)
})
expect(Hex.decode(output).buffer).toEqual(buffer.buffer);
});
}
})
describe('Base64', () => {
});
describe("Base64", () => {
for (const [input, output] of Object.entries(Precomputed.Encoding.Base64)) {
const buffer = Bytes.encode(input)
const buffer = Bytes.encode(input);
it(`Should encode ${input} to ${output}`, async () => {
expect(await Base64.encode(buffer)).toEqual(output)
})
expect(await Base64.encode(buffer)).toEqual(output);
});
it(`Should decode ${output} to ${input}`, async () => {
expect((await Base64.decode(output)).buffer).toEqual(buffer.buffer)
})
expect((await Base64.decode(output)).buffer).toEqual(buffer.buffer);
});
}
})
})
});
});
+16 -16
View File
@@ -1,26 +1,26 @@
import { describe, expect, it } from 'vitest'
import { Bytes, Hash, Hashes, Hex } from '../dist/index.js'
import { Precomputed } from './values.js'
import { describe, expect, it } from "vitest";
import { Bytes, Hash, Hashes, Hex } from "../src/index.js";
import { Precomputed } from "./values.js";
describe('Hash', () => {
describe("Hash", () => {
for (const type of Object.keys(Hashes)) {
describe(type, () => {
const values = Precomputed.Hash[type]
const values = Precomputed.Hash[type];
for (const [input, output] of Object.entries(values)) {
if (typeof output !== 'string') throw new Error('Bad test data')
if (typeof output !== "string") throw new Error("Bad test data");
it(`Should hash "${input}" to "${output.slice(0, 8)}..."`, async () => {
const hashed = await Hash.hash(input, Hashes[type])
expect(hashed).toEqual(output)
})
const hashed = await Hash.hash(input, Hashes[type]);
expect(hashed).toEqual(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])
expect(hashed).toEqual(outputBuffer)
})
const outputBuffer = Hex.decode(output);
const inputBuffer = Bytes.encode(input);
const hashed = await Hash.hash(inputBuffer, Hashes[type]);
expect(hashed).toEqual(outputBuffer);
});
}
})
});
}
})
});
+19 -15
View File
@@ -1,18 +1,22 @@
import { describe, expect, it } from 'vitest'
import { getRandomBytes } from '../dist/index.js'
import { describe, expect, it } from "vitest";
import { getRandomBytes } from "../src/index.js";
describe('Random', () => {
it('Should be able to create random values', async () => {
const buffer = await getRandomBytes(8)
expect(buffer).instanceOf(Uint8Array)
expect(buffer.byteLength).toEqual(8)
})
describe("Random", () => {
it("Should be able to create random values", async () => {
const buffer = await getRandomBytes(8);
expect(buffer).instanceOf(Uint8Array);
expect(buffer.byteLength).toEqual(8);
});
it('Should throw error on empty array', async () => {
await expect(() => getRandomBytes(0)).rejects.toThrowErrorMatchingSnapshot()
})
it("Should throw error on empty array", async () => {
await expect(() =>
getRandomBytes(0),
).rejects.toThrowErrorMatchingSnapshot();
});
it('Should throw error on negative bytes', async () => {
await expect(() => getRandomBytes(-1)).rejects.toThrowErrorMatchingSnapshot()
})
})
it("Should throw error on negative bytes", async () => {
await expect(() =>
getRandomBytes(-1),
).rejects.toThrowErrorMatchingSnapshot();
});
});
+37 -33
View File
@@ -1,40 +1,44 @@
import { describe } from 'vitest'
import { Bytes, RSA } from '../dist/index.js'
import { Precomputed } from './values.js'
import { it } from 'vitest'
import { expect } from 'vitest'
import { describe } from "vitest";
import { Bytes, RSA } from "../src/index.js";
import { Precomputed } from "./values.js";
import { it } from "vitest";
import { expect } from "vitest";
describe('RSA', () => {
describe('Generate keys', function () {
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 expect(() => RSA.generateKeyPair(1024)).rejects.toThrowErrorMatchingSnapshot()
})
it('Should not be able to generate a key below 2048bit', async () => {
await expect(() => RSA.generateKeyPair(-1)).rejects.toThrowErrorMatchingSnapshot()
})
})
describe("RSA", () => {
describe("Generate keys", function () {
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 expect(() =>
RSA.generateKeyPair(1024),
).rejects.toThrowErrorMatchingSnapshot();
});
it("Should not be able to generate a key below 2048bit", async () => {
await expect(() =>
RSA.generateKeyPair(-1),
).rejects.toThrowErrorMatchingSnapshot();
});
});
describe('Encryption', () => {
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)
const pair = await RSA.generateKeyPair(2 ** 11);
const bytes = Bytes.encode(message);
try {
const encrypted = await RSA.encrypt(bytes, pair.public)
const decrypted = await RSA.decrypt(encrypted, pair.private)
expect(decrypted).toEqual(bytes)
expect(message).toEqual(Bytes.decode(decrypted))
const encrypted = await RSA.encrypt(bytes, pair.public);
const decrypted = await RSA.decrypt(encrypted, pair.private);
expect(decrypted).toEqual(bytes);
expect(message).toEqual(Bytes.decode(decrypted));
} catch {}
})
});
}
})
})
});
});
+35 -30
View File
@@ -1,60 +1,65 @@
export const Precomputed = {
Encoding: {
Base64: {
occulto: 'b2NjdWx0bw==',
test: 'dGVzdA==',
'hello world': 'aGVsbG8gd29ybGQ=',
occulto: "b2NjdWx0bw==",
test: "dGVzdA==",
"hello world": "aGVsbG8gd29ybGQ=",
},
Hex: {
test: '74657374',
occulto: '6f6363756c746f',
'hello world': '68656c6c6f20776f726c64',
test: "74657374",
occulto: "6f6363756c746f",
"hello world": "68656c6c6f20776f726c64",
},
Bytes: {
test: new Uint8Array([0x74, 0x65, 0x73, 0x74]),
occulto: new Uint8Array([0x6f, 0x63, 0x63, 0x75, 0x6c, 0x74, 0x6f]),
'entropy is king': new Uint8Array([
0x65, 0x6e, 0x74, 0x72, 0x6f, 0x70, 0x79, 0x20, 0x69, 0x73, 0x20, 0x6b, 0x69, 0x6e, 0x67,
"entropy is king": new Uint8Array([
0x65, 0x6e, 0x74, 0x72, 0x6f, 0x70, 0x79, 0x20, 0x69, 0x73, 0x20, 0x6b,
0x69, 0x6e, 0x67,
]),
},
},
Hash: {
SHA_1: {
test: 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3',
'hello world': '2aae6c35c94fcfb415dbe95f408b9ce91ee846ed',
occulto: 'f4b27cfb9e01492f409295fbbc339753fa839c0f',
test: "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3",
"hello world": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed",
occulto: "f4b27cfb9e01492f409295fbbc339753fa839c0f",
},
SHA_256: {
test: '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
'hello world': 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9',
occulto: 'df2b97515886051821e4375a33df10486ce55cb3d14acd05dd7465f820ef2481',
test: "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
"hello world":
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
occulto:
"df2b97515886051821e4375a33df10486ce55cb3d14acd05dd7465f820ef2481",
},
SHA_384: {
test: '768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9',
'hello world': 'fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd',
occulto: '133c1968e937462ed66732409fa305c63335ee62b1114dd2d0ae98b4dd8fa6aca4656c919b295e41efa2d63f0d3c9951',
test: "768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9",
"hello world":
"fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd",
occulto:
"133c1968e937462ed66732409fa305c63335ee62b1114dd2d0ae98b4dd8fa6aca4656c919b295e41efa2d63f0d3c9951",
},
SHA_512: {
test: 'ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff',
'hello world':
'309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f',
test: "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff",
"hello world":
"309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f",
occulto:
'9f7ff06148d415b12290ab7c21f021964ed627574f94f66c994aad4a8e319aa3168a9871edace3e736096cbd957cafa42dbf3feb6efd7763bf936ddc933c9470',
"9f7ff06148d415b12290ab7c21f021964ed627574f94f66c994aad4a8e319aa3168a9871edace3e736096cbd957cafa42dbf3feb6efd7763bf936ddc933c9470",
},
},
Crypto: {
Bytes: {
14: 'e0b28a24252963ff30dd2bb3ec9c',
16: '65eeb2044e9eb115956dbf4d0d70cd8f',
24: '9fa9e0aace3b0bdcbc871aa3ee3ddb1bece759b811fa4603',
32: '848ca08f01f82e28bfa91c85d55ef2a98afd8b32c707c9c790e86b1c53a177e4',
14: "e0b28a24252963ff30dd2bb3ec9c",
16: "65eeb2044e9eb115956dbf4d0d70cd8f",
24: "9fa9e0aace3b0bdcbc871aa3ee3ddb1bece759b811fa4603",
32: "848ca08f01f82e28bfa91c85d55ef2a98afd8b32c707c9c790e86b1c53a177e4",
},
Messages: {
test: 'test',
occulto: 'occulto',
weird: 'Some 🃏 weird 🃏 text',
test: "test",
occulto: "occulto",
weird: "Some 🃏 weird 🃏 text",
nietzscheIpsum:
'Marvelous intentions joy deceptions overcome sexuality spirit against. Selfish of marvelous play dead war snare eternal-return ultimate. Reason aversion suicide.',
"Marvelous intentions joy deceptions overcome sexuality spirit against. Selfish of marvelous play dead war snare eternal-return ultimate. Reason aversion suicide.",
},
},
} as const
} as const;