1
0

AES.spec.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const AES = require('../../gramjs/crypto/AES')
  2. const AESModeCTR = require('../../gramjs/crypto/AESCTR')
  3. describe('IGE encrypt function', () => {
  4. test('it should return 4a657a834edc2956ec95b2a42ec8c1f2d1f0a6028ac26fd830ed23855574b4e69dd1a2be2ba18a53a49b879b2' +
  5. '45e1065e14b6e8ac5ba9b24befaff3209b77b5f', () => {
  6. const plainText = Buffer.from('this should hold a 64 characters long string. only 10 more left.')
  7. const iv = Buffer.from('the iv needs 32 characters long.')
  8. const key = Buffer.from('the key needs 32 characters long')
  9. const encrypted = Buffer.from('4a657a834edc2956ec95b2a42ec8c1f2d1f0a6028ac26fd830ed23855574b4e69dd1a2be' +
  10. '2ba18a53a49b879b245e1065e14b6e8ac5ba9b24befaff3209b77b5f', 'hex')
  11. expect(AES.encryptIge(plainText, key, iv)).toEqual(encrypted)
  12. })
  13. })
  14. describe('IGE decrypt function', () => {
  15. test('it should return "this should hold a 64 characters long string. only 10 more left."', () => {
  16. const encrypted = Buffer.from('4a657a834edc2956ec95b2a42ec8c1f2d1f0a6028ac26fd830ed23855574b4e69dd1a2be' +
  17. '2ba18a53a49b879b245e1065e14b6e8ac5ba9b24befaff3209b77b5f', 'hex')
  18. const iv = Buffer.from('the iv needs 32 characters long.')
  19. const key = Buffer.from('the key needs 32 characters long')
  20. const plainText = Buffer.from('this should hold a 64 characters long string. only 10 more left.')
  21. expect(AES.decryptIge(encrypted, key, iv)).toEqual(plainText)
  22. })
  23. })
  24. describe('CTR encrypt function', () => {
  25. test('it should return 5f40f14f8b70178f70e8045b44eff5f1b148714f23cd and' +
  26. ' cd0779d148b466935cf573450212451692bc82fccd5b106e53', () => {
  27. const encryptKey = Buffer.from('the key needs 32 characters long')
  28. const encryptIv = Buffer.from('the iv does not.')
  29. const encryptor = new AESModeCTR(encryptKey, encryptIv)
  30. const firstData = Buffer.from('this can be any length')
  31. const firstResult = encryptor.encrypt(firstData)
  32. const secondData = Buffer.from('this also can be anything')
  33. const secondResult = encryptor.encrypt(secondData)
  34. const outputFirst = Buffer.from('5f40f14f8b70178f70e8045b44eff5f1b148714f23cd', 'hex')
  35. const outputSecond = Buffer.from('cd0779d148b466935cf573450212451692bc82fccd5b106e53', 'hex')
  36. expect([firstResult, secondResult]).toEqual([outputFirst, outputSecond])
  37. })
  38. })