AES.spec.js 2.5 KB

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