11import { beforeAll , describe , expect , it } from 'vitest'
2- import { hxDecrypt , hxEncrypt } from './aes-sealed'
2+ import { fromBase64 , toBase64 } from '../data/bin'
3+ import { decryptAesGcm , encryptAesGcm } from './aes-sealed'
4+ import { deriveKeyPbkdf2 } from './crypto'
35
46describe ( 'aes Encryption and Decryption' , ( ) => {
57 let key : CryptoKey
@@ -17,21 +19,21 @@ describe('aes Encryption and Decryption', () => {
1719
1820 it ( 'should encrypt and decrypt data correctly' , async ( ) => {
1921 const data = new TextEncoder ( ) . encode ( 'Hello, World!' )
20- const encryptedData = await hxEncrypt ( data , key )
21- const decryptedData = await hxDecrypt ( encryptedData , key )
22+ const encryptedData = await encryptAesGcm ( data , key )
23+ const decryptedData = await decryptAesGcm ( encryptedData , key )
2224 expect ( new TextDecoder ( ) . decode ( decryptedData ) ) . toBe ( 'Hello, World!' )
2325 } )
2426
2527 it ( 'should produce different ciphertexts for the same plaintext' , async ( ) => {
2628 const data = new TextEncoder ( ) . encode ( 'Hello, World!' )
27- const encryptedData1 = await hxEncrypt ( data , key )
28- const encryptedData2 = await hxEncrypt ( data , key )
29+ const encryptedData1 = await encryptAesGcm ( data , key )
30+ const encryptedData2 = await encryptAesGcm ( data , key )
2931 expect ( encryptedData1 ) . not . toEqual ( encryptedData2 )
3032 } )
3133
3234 it ( 'should fail to decrypt with a different key' , async ( ) => {
3335 const data = new TextEncoder ( ) . encode ( 'Hello, World!' )
34- const encryptedData = await hxEncrypt ( data , key )
36+ const encryptedData = await encryptAesGcm ( data , key )
3537 const differentKey = await crypto . subtle . generateKey (
3638 {
3739 name : 'AES-GCM' ,
@@ -40,17 +42,38 @@ describe('aes Encryption and Decryption', () => {
4042 true ,
4143 [ 'encrypt' , 'decrypt' ] ,
4244 )
43- await expect ( hxDecrypt ( encryptedData , differentKey ) ) . rejects . toThrow ( )
45+ await expect ( decryptAesGcm ( encryptedData , differentKey ) ) . rejects . toThrow ( )
4446 } )
4547
46- // it('should decrypt a sample that was generated by Swift code', async () => {
47- // const key = await deriveKeyPbkdf2CBC(new Uint8Array([1, 2, 3]), {
48- // salt: new Uint8Array([1, 2, 3]),
49- // })
50- // // expect(toBase64(key)).toMatchInlineSnapshot()
51- // const sample = new Uint8Array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
52- // const encryptedData = fromBase64('br6sc+pnZaIXcV1fTygAs/UJlDZIIBY50i56MMGNampZTcSakt0=')
53- // const decryptedData = await hxDecrypt(encryptedData, key)
54- // expect(decryptedData).toMatchInlineSnapshot()
55- // })
48+ it ( 'should decrypt a sample that was generated by Swift code' , async ( ) => {
49+ const key = await deriveKeyPbkdf2 ( new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ] ) , {
50+ salt : new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ] ) ,
51+ iterations : 100000 ,
52+ } )
53+ const dataFromKey = await crypto . subtle . exportKey ( 'raw' , key )
54+ expect ( toBase64 ( dataFromKey ) ) . toMatchInlineSnapshot ( `"UDl7buu/Zn/UxCIEp55MOTOKcDHvb959P+eyozok7BA="` )
55+
56+ // Create for Swift sample
57+ // const sample = new Uint8Array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
58+ // const encryptedData2 = await hxEncrypt(sample, key)
59+ // expect(toBase64(encryptedData2)).toMatchInlineSnapshot(`"Akjv9nbmkMUsbYg3TuJIXjafK9kUkZMdJ49XdapndkFtiuLVEyg="`)
60+
61+ // From Swift sample
62+ const encryptedData = fromBase64 ( 'pVzIX88DZNjLw1VLLEnsIaqt4/jq4ZGJc8qDU2YruZJr6N9V0i0=' )
63+ const decryptedData = await decryptAesGcm ( encryptedData , key )
64+ expect ( decryptedData ) . toMatchInlineSnapshot ( `
65+ Uint8Array [
66+ 9,
67+ 8,
68+ 7,
69+ 6,
70+ 5,
71+ 4,
72+ 3,
73+ 2,
74+ 1,
75+ 0,
76+ ]
77+ ` )
78+ } )
5679} )
0 commit comments