Skip to content

Commit

Permalink
Adding asymmetric encryption primitives and basic testing
Browse files Browse the repository at this point in the history
  • Loading branch information
brickpop committed Apr 3, 2024
1 parent 41e254c commit 5cfcf50
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
70 changes: 70 additions & 0 deletions tests/asymmetric.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { expect, test, describe, beforeAll } from "bun:test";
import {
decryptBytes,
decryptString,
encrypt,
generateKeyPair,
getSeededKeyPair,
computePublicKey,
} from "../utils/encryption/asymmetric";
import libsodium from "libsodium-wrappers";

describe("Symmetric encryption", () => {
beforeAll(async () => {
await libsodium.ready;
});

test("Generates a random key pair", () => {
const alice = generateKeyPair();
const bob = generateKeyPair();

expect(libsodium.to_hex(alice.keyType)).toBe(libsodium.to_hex(bob.keyType));
expect(libsodium.to_hex(alice.privateKey)).not.toBe(
libsodium.to_hex(bob.privateKey)
);
expect(libsodium.to_hex(alice.publicKey)).not.toBe(
libsodium.to_hex(bob.publicKey)
);
});

test("Computes the public key given the secret one", () => {
const alice = generateKeyPair();
const computedPubKey = computePublicKey(alice.privateKey);

expect(libsodium.to_hex(alice.publicKey)).toBe(
libsodium.to_hex(computedPubKey)
);
});

test("Generates a seeded key pair", () => {
const alice = getSeededKeyPair("00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");
const bob = getSeededKeyPair("00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");

expect(libsodium.to_hex(alice.keyType)).toBe(libsodium.to_hex(bob.keyType));
expect(libsodium.to_hex(alice.privateKey)).toBe(
libsodium.to_hex(bob.privateKey)
);
expect(libsodium.to_hex(alice.publicKey)).toBe(
libsodium.to_hex(bob.publicKey)
);
});

test("Encrypts and decrypts a string", () => {
const bob = generateKeyPair();
const ciphertext = encrypt("Hello world", bob.publicKey);
expect(ciphertext.length).toBe(59);
const decrypted = decryptString(ciphertext, bob);
expect(decrypted).toBe("Hello world");
const decryptedHex = decryptBytes(ciphertext, bob);
expect(libsodium.to_hex(decryptedHex)).toBe("48656c6c6f20776f726c64");
});

test("Encrypts and decrypts a buffer", () => {
const bob = generateKeyPair();
const bytes = new Uint8Array([10, 15, 50, 55, 80, 85]);
const ciphertext = encrypt(bytes, bob.publicKey);
expect(ciphertext.length).toBe(54);
const decryptedHex = decryptBytes(ciphertext, bob);
expect(libsodium.to_hex(decryptedHex)).toBe("0a0f32375055");
});
});
1 change: 1 addition & 0 deletions tests/symmetric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("Symmetric encryption", () => {
beforeAll(async () => {
await libsodium.ready;
});

test("Generates a random symmetric key", () => {
const key1 = generateSymmetricKey();
const key2 = generateSymmetricKey();
Expand Down
4 changes: 2 additions & 2 deletions utils/encryption/asymmetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export function generateKeyPair() {
export function getSeededKeyPair(hexSeed: string) {
if (!hexSeed.match(/^[0-9a-fA-F]+$/)) {
throw new Error("Invalid hexadecimal seed");
} else if (hexSeed.length % 2 !== 0) {
throw new Error("The hexadecimal seed cannot have an odd length");
} else if (hexSeed.length != 64) {
throw new Error("The hexadecimal seed should be 32 bytes long");
}
const bytesSeed = sodium.from_hex(hexSeed);
return sodium.crypto_box_seed_keypair(bytesSeed);
Expand Down

0 comments on commit 5cfcf50

Please sign in to comment.