-
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.
- Loading branch information
Showing
5 changed files
with
67 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { expect, test, describe, beforeAll } from "bun:test"; | ||
import { | ||
decryptBytes, | ||
decryptString, | ||
encrypt, | ||
generateSymmetricKey, | ||
} from "../utils/encryption/symmetric"; | ||
import libsodium from "libsodium-wrappers"; | ||
|
||
describe("Symmetric encryption", () => { | ||
beforeAll(async () => { | ||
await libsodium.ready; | ||
}); | ||
test("Generates a random symmetric key", () => { | ||
const key1 = generateSymmetricKey(); | ||
const key2 = generateSymmetricKey(); | ||
|
||
expect(libsodium.to_hex(key1)).not.toBe(libsodium.to_hex(key2)); | ||
}); | ||
|
||
test("Encrypts and decrypts a string", () => { | ||
const symKey = generateSymmetricKey(); | ||
const encryptedPayload = encrypt("Hello world", symKey); | ||
|
||
expect(libsodium.to_hex(encryptedPayload)).toMatch(/^[0-9a-fA-F]+$/); | ||
expect(encryptedPayload.length).toBe(51); | ||
|
||
const decrypted = decryptString(encryptedPayload, symKey); | ||
expect(decrypted).toBe("Hello world"); | ||
|
||
const decryptedHex = decryptBytes(encryptedPayload, symKey); | ||
expect(libsodium.to_hex(decryptedHex)).toBe("48656c6c6f20776f726c64"); | ||
}); | ||
|
||
test("Encrypts and decrypts a buffer", () => { | ||
const symKey = generateSymmetricKey(); | ||
const bytes = new Uint8Array([10, 15, 50, 55, 80, 85]); | ||
const encryptedPayload = encrypt(bytes, symKey); | ||
|
||
expect(libsodium.to_hex(encryptedPayload)).toMatch(/^[0-9a-fA-F]+$/); | ||
expect(encryptedPayload.length).toBe(46); | ||
|
||
const decryptedHex = decryptBytes(encryptedPayload, symKey); | ||
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