@@ -5,7 +5,7 @@ const secret = 'Secret0123456789Secret0123456789';
55const data = '🦊 secret stuff ~ !@#$%^&*()_+' ;
66
77describe ( 'Encryption' , ( ) => {
8- test ( 'Encrypt Data' , async ( ) => {
8+ test ( 'Encrypt Data Default ' , async ( ) => {
99 // biome-ignore lint/style/useConst: ignore
1010 let nodeSecretKey : SecretKey < 'node' > ;
1111 const nodeKey = nodeKit . tryCreateSecretKey ( secret ) ;
@@ -74,6 +74,89 @@ describe('Encryption', () => {
7474 expect ( decryptedObjWeb . success ) . toBe ( true ) ;
7575 expect ( decryptedObjWeb . result ) . toEqual ( largeObj ) ;
7676 } ) ;
77+
78+ test ( 'Encrypt Data AES128GCM' , async ( ) => {
79+ // biome-ignore lint/style/useConst: ignore
80+ let nodeSecretKey : SecretKey < 'node' > ;
81+ const nodeKey = nodeKit . tryCreateSecretKey ( secret , { algorithm : 'aes128gcm' } ) ;
82+ expect ( nodeKey . success ) . toBe ( true ) ;
83+ expect ( nodeKey . result ) . toBeDefined ( ) ;
84+ expect ( isSecretKey ( nodeKey . result , 'node' ) ) . toBe ( true ) ;
85+ nodeSecretKey = nodeKey . result as SecretKey < 'node' > ;
86+
87+ // biome-ignore lint/style/useConst: ignore
88+ let webSecretKey : SecretKey < 'web' > ;
89+ const webKey = await webKit . tryCreateSecretKey ( secret , { algorithm : 'aes128gcm' } ) ;
90+ expect ( webKey . success ) . toBe ( true ) ;
91+ expect ( webKey . result ) . toBeDefined ( ) ;
92+ expect ( isSecretKey ( webKey . result , 'web' ) ) . toBe ( true ) ;
93+ webSecretKey = webKey . result as SecretKey < 'web' > ;
94+
95+ const encryptedNode = nodeKit . tryEncrypt ( data , nodeSecretKey , { outputEncoding : 'hex' } ) ;
96+ expect ( encryptedNode . success ) . toBe ( true ) ;
97+ expect ( encryptedNode . result ) . toBeDefined ( ) ;
98+ expect ( matchPattern ( encryptedNode . result as string , 'node' ) ) . toBe ( true ) ;
99+
100+ const encryptedWeb = await webKit . tryEncrypt ( data , webSecretKey , { outputEncoding : 'hex' } ) ;
101+ expect ( encryptedWeb . success ) . toBe ( true ) ;
102+ expect ( encryptedWeb . result ) . toBeDefined ( ) ;
103+ expect ( matchPattern ( encryptedWeb . result as string , 'web' ) ) . toBe ( true ) ;
104+
105+ expect ( encryptedNode . result ) . not . toBe ( encryptedWeb . result ) ;
106+
107+ const decryptedNode = nodeKit . tryDecrypt ( encryptedNode . result as string , nodeSecretKey , { inputEncoding : 'hex' } ) ;
108+ expect ( decryptedNode . success ) . toBe ( true ) ;
109+ expect ( decryptedNode . result ) . toBe ( data ) ;
110+
111+ const decryptedWeb = await webKit . tryDecrypt ( encryptedWeb . result as string , webSecretKey , { inputEncoding : 'hex' } ) ;
112+ expect ( decryptedWeb . success ) . toBe ( true ) ;
113+ expect ( decryptedWeb . result ) . toBe ( data ) ;
114+
115+ expect ( decryptedNode . result ) . toBe ( decryptedWeb . result ) ;
116+
117+ const encryptedNode2 = nodeKit . tryEncrypt ( data , nodeSecretKey , { outputEncoding : 'hex' } ) ;
118+ expect ( encryptedNode2 . success ) . toBe ( true ) ;
119+ expect ( encryptedNode2 . result ) . toBeDefined ( ) ;
120+ expect ( encryptedNode2 . result ) . not . toBe ( encryptedNode . result ) ;
121+
122+ const encryptedWeb2 = await webKit . tryEncrypt ( data , webSecretKey , { outputEncoding : 'hex' } ) ;
123+ expect ( encryptedWeb2 . success ) . toBe ( true ) ;
124+ expect ( encryptedWeb2 . result ) . toBeDefined ( ) ;
125+ expect ( encryptedWeb2 . result ) . not . toBe ( encryptedWeb . result ) ;
126+
127+ const encryptedObjNode = nodeKit . tryEncryptObj ( largeObj , nodeSecretKey , { outputEncoding : 'base64' } ) ;
128+ expect ( encryptedObjNode . success ) . toBe ( true ) ;
129+ expect ( encryptedObjNode . result ) . toBeDefined ( ) ;
130+ expect ( matchPattern ( encryptedObjNode . result as string , 'node' ) ) . toBe ( true ) ;
131+
132+ const encryptedObjWeb = await webKit . tryEncryptObj ( largeObj , webSecretKey , { outputEncoding : 'base64' } ) ;
133+ expect ( encryptedObjWeb . success ) . toBe ( true ) ;
134+ expect ( encryptedObjWeb . result ) . toBeDefined ( ) ;
135+ expect ( matchPattern ( encryptedObjWeb . result as string , 'web' ) ) . toBe ( true ) ;
136+
137+ expect ( encryptedObjNode . result ) . not . toBe ( encryptedObjWeb . result ) ;
138+
139+ const decryptedObjNode = nodeKit . tryDecryptObj < typeof largeObj > ( encryptedObjNode . result as string , nodeSecretKey , {
140+ inputEncoding : 'base64' ,
141+ } ) ;
142+ expect ( decryptedObjNode . success ) . toBe ( true ) ;
143+ expect ( decryptedObjNode . result ) . toEqual ( largeObj ) ;
144+
145+ const decryptedObjWeb = await webKit . tryDecryptObj < typeof largeObj > (
146+ encryptedObjWeb . result as string ,
147+ webSecretKey ,
148+ { inputEncoding : 'base64' } ,
149+ ) ;
150+ expect ( decryptedObjWeb . success ) . toBe ( true ) ;
151+ expect ( decryptedObjWeb . result ) . toEqual ( largeObj ) ;
152+ } ) ;
153+
154+ test ( 'Encoding Test' , ( ) => {
155+ expect ( nodeKit . convertEncoding ( data , 'utf8' , 'base64' ) ) . toBe ( webKit . convertEncoding ( data , 'utf8' , 'base64' ) ) ;
156+ expect ( nodeKit . convertEncoding ( data , 'utf8' , 'hex' ) ) . toBe ( webKit . convertEncoding ( data , 'utf8' , 'hex' ) ) ;
157+ expect ( nodeKit . convertEncoding ( data , 'utf8' , 'base64url' ) ) . toBe ( webKit . convertEncoding ( data , 'utf8' , 'base64url' ) ) ;
158+ expect ( nodeKit . convertEncoding ( data , 'utf8' , 'latin1' ) ) . toBe ( webKit . convertEncoding ( data , 'utf8' , 'latin1' ) ) ;
159+ } ) ;
77160} ) ;
78161
79162const largeObj = {
0 commit comments