-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding asymmetric encryption primitives and basic testing
- Loading branch information
Showing
3 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters