|
1 | 1 | const shutter = require("../dist/shutter-crypto");
|
| 2 | +const ethers = require("ethers"); |
2 | 3 |
|
3 |
| -describe("Sum numbers", () => { |
4 |
| - test("it should sum two numbers correctly", () => { |
5 |
| - const sum = 1 + 2; |
6 |
| - const expectedResult = 3; |
7 |
| - expect(sum).toEqual(expectedResult); |
8 |
| - }); |
9 |
| -}); |
10 |
| - |
| 4 | +var encrypted; |
11 | 5 | describe("Test shutter crypto", () => {
|
12 | 6 | test("init the wasm library", async () => {
|
13 | 7 | try {
|
14 | 8 | await shutter.init();
|
15 | 9 | } catch (error) {
|
16 | 10 | expect(error).toBeNone();
|
17 | 11 | }
|
18 |
| - const msg = new Uint8Array(32); |
| 12 | + }); |
| 13 | + test("encrypt a message", async () => { |
| 14 | + const msg = ethers.getBytes(Buffer.from("a message")); |
19 | 15 | const hexString =
|
20 |
| - "0x0b4e86e0ed51ef774210d1c0fe0be6f1b4f0695d5d396b3b003547f752ac82e316375aa37b1739c9c8472b1b5ae09477565bf9d2c0d7db0c39576f4615d32703262d5854bfbac8a60eb6d227f397289e6e51f979b56476b7f7f32a45ede7a61f21d893a54ab6e65b283342adc41d53df5432569c6a8c2304921bce3ea148efb4"; |
| 16 | + "0b4e86e0ed51ef774210d1c0fe0be6f1b4f0695d5d396b3b003547f752ac82e316375aa37b1739c9c8472b1b5ae09477565bf9d2c0d7db0c39576f4615d32703262d5854bfbac8a60eb6d227f397289e6e51f979b56476b7f7f32a45ede7a61f21d893a54ab6e65b283342adc41d53df5432569c6a8c2304921bce3ea148efb4"; |
21 | 17 | const eonkey = Uint8Array.from(Buffer.from(hexString, "hex"));
|
22 |
| - const epochId = 0x46; |
| 18 | + const epochId = ethers.getBytes( |
| 19 | + ethers.zeroPadValue(Buffer.from("46", "hex"), 32) |
| 20 | + ); |
| 21 | + |
23 | 22 | const sigma = new Uint8Array(32);
|
| 23 | + crypto.getRandomValues(sigma); |
| 24 | + try { |
| 25 | + encrypted = await shutter.encrypt(msg, eonkey, epochId, sigma); |
| 26 | + } catch (error) { |
| 27 | + expect(error).toBeNull(); |
| 28 | + } |
| 29 | + expect(encrypted).toBeDefined(); |
| 30 | + }); |
| 31 | + test("decrypt a message", async () => { |
| 32 | + const decryptionKey = Uint8Array.from( |
| 33 | + Buffer.from( |
| 34 | + "009bb51574d6a6790faa4724dfad416ca059a286ccfee20be732cac9a81e05dc2f47905cbaa0fb043ff849b0c41e99208d98d27cba3fffb43d63ba50c35259d3", |
| 35 | + "hex" |
| 36 | + ) |
| 37 | + ); |
| 38 | + var decrypted; |
| 39 | + try { |
| 40 | + decrypted = await shutter.decrypt(encrypted, decryptionKey); |
| 41 | + } catch (error) { |
| 42 | + expect(error).toBeNull(); |
| 43 | + } |
| 44 | + }); |
| 45 | +}); |
24 | 46 |
|
25 |
| - const encrypted = shutter.encrypt(msg, eonkey, sigma); |
26 |
| - // expect(encrypted).toBeDefined(); |
27 |
| - const decryptionKey = |
28 |
| - "0x009bb51574d6a6790faa4724dfad416ca059a286ccfee20be732cac9a81e05dc2f47905cbaa0fb043ff849b0c41e99208d98d27cba3fffb43d63ba50c35259d3"; |
| 47 | +describe("Test known values (values obtained from 'rolling-shutter crypto encrypt/decrypt')", () => { |
| 48 | + test("encrypt a message with zero sigma", async () => { |
| 49 | + const epochId = ethers.getBytes( |
| 50 | + ethers.zeroPadValue(Buffer.from("46", "hex"), 32) |
| 51 | + ); |
| 52 | + const eonKey = ethers.getBytes( |
| 53 | + Buffer.from( |
| 54 | + "0b4e86e0ed51ef774210d1c0fe0be6f1b4f0695d5d396b3b003547f752ac82e316375aa37b1739c9c8472b1b5ae09477565bf9d2c0d7db0c39576f4615d32703262d5854bfbac8a60eb6d227f397289e6e51f979b56476b7f7f32a45ede7a61f21d893a54ab6e65b283342adc41d53df5432569c6a8c2304921bce3ea148efb4", |
| 55 | + "hex" |
| 56 | + ) |
| 57 | + ); |
| 58 | + const sigma = ethers.getBytes( |
| 59 | + Buffer.from( |
| 60 | + "0000000000000000000000000000000000000000000000000000000000000000", |
| 61 | + "hex" |
| 62 | + ) |
| 63 | + ); |
| 64 | + const message = Uint8Array.from( |
| 65 | + "a message".split("").map((c) => c.charCodeAt()) |
| 66 | + ); |
| 67 | + const expected_encrypted = |
| 68 | + "0x01f2490511e502db0ea9940cfc16ae5ee4435e70b8f9a5567c6230ee41026d5710cdeeaef09dbed3d461592995150eaafba0ed3eeef5be914172677fa9a095261cc049a326159a7ad35b80ba3c08296bef0c19fc6e605e0c21d542a46e83eb611ee04582022da7715b417c91502f07e26eb642f20918830a08f584afb04cb3b0b88d48b4aa8d6e5cf3e6066f4b765774d0ff84d046ef6c92f6c3ad1a2711aecf92baebfa11943d9295a886519fb1a59edde5138322cff19b4b497a12f35e04e4"; |
| 69 | + const result = await shutter.encrypt(message, eonKey, epochId, sigma); |
| 70 | + const result_hex = ethers.hexlify(result); |
| 71 | + expect(result_hex).toEqual(expected_encrypted); |
| 72 | + }); |
| 73 | + test("decrypt a known encrypted message", async () => { |
| 74 | + const known_encrypted = ethers.getBytes( |
| 75 | + Buffer.from( |
| 76 | + "01f2490511e502db0ea9940cfc16ae5ee4435e70b8f9a5567c6230ee41026d5710cdeeaef09dbed3d461592995150eaafba0ed3eeef5be914172677fa9a095261cc049a326159a7ad35b80ba3c08296bef0c19fc6e605e0c21d542a46e83eb611ee04582022da7715b417c91502f07e26eb642f20918830a08f584afb04cb3b0b88d48b4aa8d6e5cf3e6066f4b765774d0ff84d046ef6c92f6c3ad1a2711aecf92baebfa11943d9295a886519fb1a59edde5138322cff19b4b497a12f35e04e4", |
| 77 | + "hex" |
| 78 | + ) |
| 79 | + ); |
| 80 | + const decryptionKey = Uint8Array.from( |
| 81 | + Buffer.from( |
| 82 | + "009bb51574d6a6790faa4724dfad416ca059a286ccfee20be732cac9a81e05dc2f47905cbaa0fb043ff849b0c41e99208d98d27cba3fffb43d63ba50c35259d3", |
| 83 | + "hex" |
| 84 | + ) |
| 85 | + ); |
| 86 | + var decrypted; |
| 87 | + try { |
| 88 | + decrypted = await shutter.decrypt(known_encrypted, decryptionKey); |
| 89 | + } catch (error) { |
| 90 | + expect(error).toBeNull(); |
| 91 | + } |
| 92 | + const expected = Uint8Array.from( |
| 93 | + "a message".split("").map((c) => c.charCodeAt()) |
| 94 | + ); |
| 95 | + expect(decrypted).toEqual(expected); |
29 | 96 | });
|
30 | 97 | });
|
0 commit comments