index.spec.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true, jasmine:true */
  9. 'use strict';
  10. var messageDigest = require('../lib/index');
  11. var cryptoPolicy = require('scytl-cryptopolicy');
  12. var bitwise = require('scytl-bitwise');
  13. var codec = require('scytl-codec');
  14. describe('The message digest module should be able to ...', function() {
  15. var DATA = 'Ox2fUJq1gAbX';
  16. var OTHER_DATA_1 = DATA + 'a';
  17. var OTHER_DATA_2 = DATA + 'b';
  18. var data_;
  19. var otherData1_;
  20. var otherData2_;
  21. var service_;
  22. beforeAll(function() {
  23. data_ = codec.utf8Encode(DATA);
  24. otherData1_ = codec.utf8Encode(OTHER_DATA_1);
  25. otherData2_ = codec.utf8Encode(OTHER_DATA_2);
  26. service_ = messageDigest.newService();
  27. });
  28. describe('create a message digest service that should be able to ..', function() {
  29. describe('create a message digester that should be able to', function() {
  30. it('generate digests that are deterministic', function() {
  31. // Pass the digester some data.
  32. var digester = service_.newDigester();
  33. var firstMsgDigest = digester.digest(data_);
  34. // Pass another digester the same data.
  35. digester = service_.newDigester();
  36. var secondMsgDigest = digester.digest(data_);
  37. // Verify that both processes have produced the same result.
  38. expect(secondMsgDigest).toEqual(firstMsgDigest);
  39. // Pass yet another digester slightly altered data.
  40. digester = service_.newDigester();
  41. var thirdMsgDigest = digester.digest(otherData1_);
  42. // Verify that new message digest does not match the first two.
  43. expect(thirdMsgDigest).not.toEqual(firstMsgDigest);
  44. });
  45. it('generate different digests from the same data, when using different hash algorithms',
  46. function() {
  47. // Generate a SHA256 digest.
  48. var sha256Policy = cryptoPolicy.newInstance();
  49. sha256Policy.primitives.messageDigest.algorithm =
  50. cryptoPolicy.options.primitives.messageDigest.algorithm.SHA256;
  51. var service = messageDigest.newService({policy: sha256Policy});
  52. var digester = service.newDigester();
  53. var sha256Digest = digester.digest(data_);
  54. // Generate a SHA512 digest.
  55. var sha512Policy = cryptoPolicy.newInstance();
  56. sha512Policy.primitives.messageDigest.algorithm =
  57. cryptoPolicy.options.primitives.messageDigest.algorithm
  58. .SHA512_224;
  59. service = messageDigest.newService({policy: sha512Policy});
  60. digester = service.newDigester();
  61. var sha512Digest = digester.digest(data_);
  62. // Verify that the digests do not match.
  63. expect(sha256Digest).not.toEqual(sha512Digest);
  64. });
  65. it('generate a digest from multiple data parts', function() {
  66. var dataParts = [data_, otherData1_, otherData2_];
  67. var digester = service_.newDigester();
  68. for (var i = 0; i < dataParts.length; i++) {
  69. digester.update(dataParts[i]);
  70. }
  71. var firstMsgDigest = digester.digest();
  72. digester = service_.newDigester();
  73. var secondMsgDigest = digester.digest(
  74. bitwise.concatenate(data_, otherData1_, otherData2_));
  75. expect(secondMsgDigest).toEqual(firstMsgDigest);
  76. digester = service_.newDigester();
  77. for (var j = 0; j < (dataParts.length - 1); j++) {
  78. digester.update(dataParts[j]);
  79. }
  80. firstMsgDigest = digester.digest(otherData2_);
  81. digester = service_.newDigester();
  82. secondMsgDigest = digester.digest(
  83. bitwise.concatenate(data_, otherData1_, otherData2_));
  84. expect(secondMsgDigest).toEqual(firstMsgDigest);
  85. });
  86. it('generate a digest from multiple data parts, using method chaining',
  87. function() {
  88. var digester = service_.newDigester();
  89. var firstMsgDigest = digester.digest(
  90. bitwise.concatenate(data_, otherData1_, otherData2_));
  91. var secondMsgDigest = service_.newDigester()
  92. .update(data_)
  93. .update(otherData1_)
  94. .update(otherData2_)
  95. .digest();
  96. expect(secondMsgDigest).toEqual(firstMsgDigest);
  97. secondMsgDigest = service_.newDigester()
  98. .update(data_)
  99. .update(otherData1_)
  100. .digest(otherData2_);
  101. expect(secondMsgDigest).toEqual(firstMsgDigest);
  102. });
  103. it('generate a digest with the expected value when using data of type string',
  104. function() {
  105. var digester = service_.newDigester();
  106. var firstMsgDigest =
  107. digester.update(data_).update(otherData1_).digest(otherData2_);
  108. var secondMsgDigest =
  109. digester.update(DATA).update(OTHER_DATA_1).digest(OTHER_DATA_2);
  110. expect(secondMsgDigest).toEqual(firstMsgDigest);
  111. });
  112. it('be automatically reinitialized after digesting', function() {
  113. var digester = service_.newDigester();
  114. var firstMsgDigest = digester.digest(data_);
  115. var secondMsgDigest = digester.digest(data_);
  116. expect(secondMsgDigest).toEqual(firstMsgDigest);
  117. });
  118. it('throw an error upon creation if an unsupported crytographic policy was provided to the service',
  119. function() {
  120. var policy = cryptoPolicy.newInstance();
  121. policy.primitives.messageDigest.algorithm = 'SHA0';
  122. var service = messageDigest.newService({policy: policy});
  123. expect(function() {
  124. service.newDigester();
  125. }).toThrow();
  126. });
  127. it('throw an error when generating a digest without either providing data or having previously updated the digester with some data',
  128. function() {
  129. var digester = service_.newDigester();
  130. expect(function() {
  131. digester.digest();
  132. }).toThrow();
  133. });
  134. });
  135. });
  136. });