primitives.messagedigest.spec.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. // Test Sha256 hash function.
  9. describe('Message digest ...', function() {
  10. 'use strict';
  11. var _data = 'Ox2fUJq1gAbX';
  12. describe('when algorithm is SHA256 ..', function() {
  13. describe('should be able to ..', function() {
  14. it('generate identical message digests when using identical data.',
  15. function() {
  16. cryptolib(
  17. 'primitives.messagedigest', 'commons.utils', function(box) {
  18. var messageDigestFactory = new box.primitives.messagedigest
  19. .factory.MessageDigestFactory();
  20. var converters = new box.commons.utils.Converters();
  21. var sha256Digest =
  22. messageDigestFactory.getCryptoMessageDigest();
  23. sha256Digest.start();
  24. var dataB64;
  25. dataB64 = converters.base64Encode(_data);
  26. var arrayDataBase64 = [dataB64];
  27. sha256Digest.update(arrayDataBase64);
  28. var msgDigest = sha256Digest.digest();
  29. // Generate new message digest from same test string.
  30. sha256Digest.start();
  31. sha256Digest.update(arrayDataBase64);
  32. var msgDigestNew = sha256Digest.digest();
  33. // Verify that new message digest is equal to original.
  34. expect(msgDigestNew).toBe(msgDigest);
  35. });
  36. });
  37. it('generate different message digests when using different data.',
  38. function() {
  39. cryptolib(
  40. 'primitives.messagedigest', 'commons.utils', function(box) {
  41. // Generate message digest from test string.
  42. var messageDigestFactory = new box.primitives.messagedigest
  43. .factory.MessageDigestFactory();
  44. var converters = new box.commons.utils.Converters();
  45. var sha256Digest =
  46. messageDigestFactory.getCryptoMessageDigest();
  47. sha256Digest.start();
  48. var dataB64;
  49. dataB64 = converters.base64Encode(_data);
  50. var arrayDataBase64 = [dataB64];
  51. sha256Digest.update(arrayDataBase64);
  52. var msgDigest = sha256Digest.digest();
  53. // Generate new message digest from slightly altered test
  54. // string.
  55. sha256Digest.start();
  56. dataB64 = converters.base64Encode(_data + 'x');
  57. arrayDataBase64 = [dataB64];
  58. sha256Digest.update(arrayDataBase64);
  59. var msgDigestNew = sha256Digest.digest();
  60. // Verify that new message digest is not equal to original.
  61. expect(msgDigestNew).not.toBe(msgDigest);
  62. });
  63. });
  64. it('throw an error when the array of data is null.', function() {
  65. cryptolib('primitives.messagedigest', function(box) {
  66. // Generate message digest from test string.
  67. var messageDigestFactory =
  68. new box.primitives.messagedigest.factory.MessageDigestFactory();
  69. var sha256Digest = messageDigestFactory.getCryptoMessageDigest();
  70. sha256Digest.start();
  71. expect(function() {
  72. sha256Digest.update(undefined);
  73. }).toThrow();
  74. expect(function() {
  75. sha256Digest.update(null);
  76. }).toThrow();
  77. });
  78. });
  79. it('throw an error when the array of data contains less than one element.',
  80. function() {
  81. cryptolib('primitives.messagedigest', function(box) {
  82. // Generate message digest from test string.
  83. var messageDigestFactory = new box.primitives.messagedigest.factory
  84. .MessageDigestFactory();
  85. var sha256Digest = messageDigestFactory.getCryptoMessageDigest();
  86. sha256Digest.start();
  87. expect(function() {
  88. sha256Digest.update([]);
  89. }).toThrow();
  90. });
  91. });
  92. });
  93. });
  94. describe('when algorithm is SHA512/224 ..', function() {
  95. describe('should be able to ..', function() {
  96. it('generate identical message digests when using identical data.',
  97. function() {
  98. var f = function(box) {
  99. var messageDigestFactory = new box.primitives.messagedigest.factory
  100. .MessageDigestFactory();
  101. var converters = new box.commons.utils.Converters();
  102. var digester = messageDigestFactory.getCryptoMessageDigest();
  103. digester.start();
  104. var dataB64;
  105. dataB64 = converters.base64Encode(_data);
  106. var arrayDataBase64 = [dataB64];
  107. digester.update(arrayDataBase64);
  108. var msgDigest = digester.digest();
  109. // Generate new message digest from same test string.
  110. digester.start();
  111. digester.update(arrayDataBase64);
  112. var msgDigestNew = digester.digest();
  113. // Verify that new message digest is equal to original.
  114. expect(msgDigestNew).toBe(msgDigest);
  115. };
  116. f.policies = {
  117. primitives:
  118. {messagedigest: {algorithm: 'SHA512/224', provider: 'Forge'}}
  119. };
  120. cryptolib('primitives.messagedigest', 'commons.utils', f);
  121. });
  122. it('generate different message digests when using different data.',
  123. function() {
  124. var f = function(box) {
  125. // Generate message digest from test string.
  126. var messageDigestFactory = new box.primitives.messagedigest.factory
  127. .MessageDigestFactory();
  128. var converters = new box.commons.utils.Converters();
  129. var digester = messageDigestFactory.getCryptoMessageDigest();
  130. digester.start();
  131. var dataB64;
  132. dataB64 = converters.base64Encode(_data);
  133. var arrayDataBase64 = [dataB64];
  134. digester.update(arrayDataBase64);
  135. var msgDigest = digester.digest();
  136. // Generate new message digest from slightly altered test string.
  137. digester.start();
  138. dataB64 = converters.base64Encode(_data + 'x');
  139. arrayDataBase64 = [dataB64];
  140. digester.update(arrayDataBase64);
  141. var msgDigestNew = digester.digest();
  142. // Verify that new message digest is not equal to original.
  143. expect(msgDigestNew).not.toBe(msgDigest);
  144. };
  145. f.policies = {
  146. primitives:
  147. {messagedigest: {algorithm: 'SHA512/224', provider: 'Forge'}}
  148. };
  149. cryptolib('primitives.messagedigest', 'commons.utils', f);
  150. });
  151. it('throw an error when the array of data is null.', function() {
  152. var f = function(box) {
  153. // Generate message digest from test string.
  154. var messageDigestFactory =
  155. new box.primitives.messagedigest.factory.MessageDigestFactory();
  156. var digester = messageDigestFactory.getCryptoMessageDigest();
  157. digester.start();
  158. expect(function() {
  159. digester.update(undefined);
  160. }).toThrow();
  161. expect(function() {
  162. digester.update(null);
  163. }).toThrow();
  164. };
  165. f.policies = {
  166. primitives:
  167. {messagedigest: {algorithm: 'SHA512/224', provider: 'Forge'}}
  168. };
  169. cryptolib('primitives.messagedigest', f);
  170. });
  171. it('throw an error when the array of data contains less than one element.',
  172. function() {
  173. var f = function(box) {
  174. // Generate message digest from test string.
  175. var messageDigestFactory = new box.primitives.messagedigest.factory
  176. .MessageDigestFactory();
  177. var digester = messageDigestFactory.getCryptoMessageDigest();
  178. digester.start();
  179. expect(function() {
  180. digester.update([]);
  181. }).toThrow();
  182. };
  183. f.policies = {
  184. primitives:
  185. {messagedigest: {algorithm: 'SHA512/224', provider: 'Forge'}}
  186. };
  187. cryptolib('primitives.messagedigest', f);
  188. });
  189. });
  190. });
  191. });