@@ -155,4 +155,54 @@ test.group('Encryption', () => {
155155 const encrypted = encryption . encrypt ( { username : 'virk' } , { expiresIn : '1h' , purpose : 'test' } )
156156 assert . deepEqual ( encryption . decrypt ( encrypted , 'test' ) , { username : 'virk' } )
157157 } )
158+
159+ test ( 'compute blind index using first key' , ( { assert } ) => {
160+ const encryption = new Encryption ( {
161+ driver : ( key ) => new ChaCha20Poly1305 ( { id : 'test' , key } ) ,
162+ keys : [ SECRET , SECRET_2 ] ,
163+ } )
164+
165+ const blindIndex = encryption . blindIndex ( 'foo@example.com' , 'users.email' )
166+
167+ const singleKeyEncryption = new Encryption ( {
168+ driver : ( key ) => new ChaCha20Poly1305 ( { id : 'test' , key } ) ,
169+ keys : [ SECRET ] ,
170+ } )
171+
172+ assert . equal ( blindIndex , singleKeyEncryption . blindIndex ( 'foo@example.com' , 'users.email' ) )
173+ } )
174+
175+ test ( 'compute blind indexes for all keys' , ( { assert } ) => {
176+ const encryption = new Encryption ( {
177+ driver : ( key ) => new ChaCha20Poly1305 ( { id : 'test' , key } ) ,
178+ keys : [ SECRET , SECRET_2 ] ,
179+ } )
180+
181+ const indexes = encryption . blindIndexes ( 'foo@example.com' , 'users.email' )
182+ assert . lengthOf ( indexes , 2 )
183+
184+ const singleKeyEncryption1 = new Encryption ( {
185+ driver : ( key ) => new ChaCha20Poly1305 ( { id : 'test' , key } ) ,
186+ keys : [ SECRET ] ,
187+ } )
188+ const singleKeyEncryption2 = new Encryption ( {
189+ driver : ( key ) => new ChaCha20Poly1305 ( { id : 'test' , key } ) ,
190+ keys : [ SECRET_2 ] ,
191+ } )
192+
193+ assert . include ( indexes , singleKeyEncryption1 . blindIndex ( 'foo@example.com' , 'users.email' ) )
194+ assert . include ( indexes , singleKeyEncryption2 . blindIndex ( 'foo@example.com' , 'users.email' ) )
195+ } )
196+
197+ test ( 'fail when blind index purpose is missing' , ( { assert } ) => {
198+ const encryption = new Encryption ( {
199+ driver : ( key ) => new ChaCha20Poly1305 ( { id : 'test' , key } ) ,
200+ keys : [ SECRET ] ,
201+ } )
202+
203+ assert . throws (
204+ ( ) => encryption . blindIndex ( 'foo@example.com' , '' ) ,
205+ 'Blind index requires a non-empty purpose'
206+ )
207+ } )
158208} )
0 commit comments