You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
it('should throw error when initializing without key',async()=>{
37
+
awaitexpect(crypto.initialize()).rejects.toThrow('Hash key must be passed to initialize function')
38
+
awaitexpect(crypto.initialize(null)).rejects.toThrow('Hash key must be passed to initialize function')
39
+
awaitexpect(crypto.initialize('')).rejects.toThrow('Hash key must be passed to initialize function')
40
+
})
41
+
42
+
it('should throw error when initializing with non-hex key',async()=>{
43
+
awaitexpect(crypto.initialize('not-hex')).rejects.toThrow('Hash key must be a 32-byte string')
44
+
awaitexpect(crypto.initialize('zzz')).rejects.toThrow('Hash key must be a 32-byte string')
45
+
awaitexpect(crypto.initialize('12345g')).rejects.toThrow('Hash key must be a 32-byte string')
46
+
})
47
+
48
+
it('should throw error when initializing with wrong length key',async()=>{
49
+
// Too short (31 bytes = 62 hex chars)
50
+
awaitexpect(crypto.initialize('a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1')).rejects.toThrow('Hash key must be a 32-byte string')
51
+
// Too long (33 bytes = 66 hex chars)
52
+
awaitexpect(crypto.initialize('a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3')).rejects.toThrow('Hash key must be a 32-byte string')
it('should throw error when encrypting with invalid message type',()=>{
423
+
constkeypairA=crypto.generateKeypair()
424
+
constkeypairB=crypto.generateKeypair()
425
+
426
+
expect(()=>crypto.encryptAB(123,keypairB.publicKey,keypairA.secretKey)).toThrow('Message to encrypt must be a string')
427
+
expect(()=>crypto.encryptAB(null,keypairB.publicKey,keypairA.secretKey)).toThrow('Message to encrypt must be a string')
428
+
expect(()=>crypto.encryptAB(undefined,keypairB.publicKey,keypairA.secretKey)).toThrow('Message to encrypt must be a string')
429
+
expect(()=>crypto.encryptAB({},keypairB.publicKey,keypairA.secretKey)).toThrow('Message to encrypt must be a string')
430
+
})
431
+
432
+
it('should throw error when encrypting with invalid public key format',()=>{
433
+
constkeypairA=crypto.generateKeypair()
434
+
constmessage='test'
435
+
436
+
expect(()=>crypto.encryptAB(message,'invalid',keypairA.secretKey)).toThrow('Secret key string must be in hex format')
437
+
expect(()=>crypto.encryptAB(message,'12345',keypairA.secretKey)).toThrow('Secret key string must be in hex format')
438
+
expect(()=>crypto.encryptAB(message,'zzz',keypairA.secretKey)).toThrow('Secret key string must be in hex format')
439
+
})
440
+
441
+
it('should throw error when encrypting with invalid secret key format',()=>{
442
+
constkeypairB=crypto.generateKeypair()
443
+
constmessage='test'
444
+
445
+
expect(()=>crypto.encryptAB(message,keypairB.publicKey,'invalid')).toThrow('Secret key string must be in hex format')
446
+
expect(()=>crypto.encryptAB(message,keypairB.publicKey,'12345')).toThrow('Secret key string must be in hex format')
447
+
expect(()=>crypto.encryptAB(message,keypairB.publicKey,'zzz')).toThrow('Secret key string must be in hex format')
448
+
})
449
+
450
+
it('should throw error when decrypting with invalid message type',()=>{
451
+
constkeypairA=crypto.generateKeypair()
452
+
constkeypairB=crypto.generateKeypair()
453
+
454
+
expect(()=>crypto.decryptAB(123,keypairA.publicKey,keypairB.secretKey)).toThrow('Message to decrypt must be a string')
455
+
expect(()=>crypto.decryptAB(null,keypairA.publicKey,keypairB.secretKey)).toThrow('Message to decrypt must be a string')
456
+
expect(()=>crypto.decryptAB(undefined,keypairA.publicKey,keypairB.secretKey)).toThrow('Message to decrypt must be a string')
457
+
expect(()=>crypto.decryptAB({},keypairA.publicKey,keypairB.secretKey)).toThrow('Message to decrypt must be a string')
458
+
})
459
+
460
+
it('should throw error when decrypting with invalid message format',()=>{
461
+
constkeypairA=crypto.generateKeypair()
462
+
constkeypairB=crypto.generateKeypair()
463
+
464
+
// Missing colon separator
465
+
expect(()=>crypto.decryptAB('invalidformat',keypairA.publicKey,keypairB.secretKey)).toThrow('Message to decrypt in must have nonce:ciphertext as hex:base64')
466
+
467
+
// Invalid nonce (not hex)
468
+
expect(()=>crypto.decryptAB('zzz:validbase64',keypairA.publicKey,keypairB.secretKey)).toThrow('Message to decrypt in must have nonce:ciphertext as hex:base64')
469
+
470
+
// Invalid ciphertext (not base64)
471
+
expect(()=>crypto.decryptAB('a1b2c3:@@@###',keypairA.publicKey,keypairB.secretKey)).toThrow('Message to decrypt in must have nonce:ciphertext as hex:base64')
472
+
})
473
+
474
+
it('should throw error when decrypting with invalid public key format',()=>{
475
+
constkeypairA=crypto.generateKeypair()
476
+
constkeypairB=crypto.generateKeypair()
477
+
// First create a valid encrypted message to get the format right
0 commit comments