hash-test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict';
  2. /* global describe it */
  3. var assert = require('assert');
  4. var crypto = require('crypto');
  5. var hash = require('../');
  6. describe('Hash', function() {
  7. function test(fn, cases) {
  8. for (var i = 0; i < cases.length; i++) {
  9. var msg = cases[i][0];
  10. var res = cases[i][1];
  11. var enc = cases[i][2];
  12. var dgst = fn().update(msg, enc).digest('hex');
  13. assert.equal(dgst, res);
  14. // Split message
  15. dgst = fn().update(msg.slice(0, 2), enc)
  16. .update(msg.slice(2), enc)
  17. .digest('hex');
  18. assert.equal(dgst, res);
  19. }
  20. }
  21. it('should support sha256', function() {
  22. assert.equal(hash.sha256.blockSize, 512);
  23. assert.equal(hash.sha256.outSize, 256);
  24. test(hash.sha256, [
  25. [ 'abc',
  26. 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad' ],
  27. [ 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
  28. '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1' ],
  29. [ 'deadbeef',
  30. '5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953',
  31. 'hex' ],
  32. ]);
  33. });
  34. it('should support sha224', function() {
  35. assert.equal(hash.sha224.blockSize, 512);
  36. assert.equal(hash.sha224.outSize, 224);
  37. test(hash.sha224, [
  38. [ 'abc',
  39. '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7' ],
  40. [ 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
  41. '75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525' ],
  42. [ 'deadbeef',
  43. '55b9eee5f60cc362ddc07676f620372611e22272f60fdbec94f243f8',
  44. 'hex' ],
  45. ]);
  46. });
  47. it('should support ripemd160', function() {
  48. assert.equal(hash.ripemd160.blockSize, 512);
  49. assert.equal(hash.ripemd160.outSize, 160);
  50. test(hash.ripemd160, [
  51. [ '', '9c1185a5c5e9fc54612808977ee8f548b2258d31'],
  52. [ 'abc',
  53. '8eb208f7e05d987a9b044a8e98c6b087f15a0bfc' ],
  54. [ 'message digest',
  55. '5d0689ef49d2fae572b881b123a85ffa21595f36' ],
  56. [ 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
  57. '12a053384a9c0c88e405a06c27dcf49ada62eb2b' ],
  58. [ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
  59. 'b0e20b6e3116640286ed3a87a5713079b21f5189' ],
  60. ]);
  61. });
  62. it('should support sha1', function() {
  63. assert.equal(hash.sha1.blockSize, 512);
  64. assert.equal(hash.sha1.outSize, 160);
  65. test(hash.sha1, [
  66. [ '',
  67. 'da39a3ee5e6b4b0d3255bfef95601890afd80709' ],
  68. [ 'abc',
  69. 'a9993e364706816aba3e25717850c26c9cd0d89d' ],
  70. [ 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
  71. '84983e441c3bd26ebaae4aa1f95129e5e54670f1' ],
  72. [ 'deadbeef',
  73. 'd78f8bb992a56a597f6c7a1fb918bb78271367eb',
  74. 'hex' ],
  75. ]);
  76. });
  77. it('should support sha512', function() {
  78. assert.equal(hash.sha512.blockSize, 1024);
  79. assert.equal(hash.sha512.outSize, 512);
  80. test(hash.sha512, [
  81. [ 'abc',
  82. 'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a' +
  83. '2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f'
  84. ],
  85. [
  86. 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn' +
  87. 'hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu',
  88. '8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018' +
  89. '501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909'
  90. ]
  91. ]);
  92. });
  93. it('should support sha384', function() {
  94. assert.equal(hash.sha384.blockSize, 1024);
  95. assert.equal(hash.sha384.outSize, 384);
  96. test(hash.sha384, [
  97. [ 'abc',
  98. 'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed' +
  99. '8086072ba1e7cc2358baeca134c825a7'
  100. ],
  101. [
  102. 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn' +
  103. 'hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu',
  104. '09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712' +
  105. 'fcc7c71a557e2db966c3e9fa91746039'
  106. ]
  107. ]);
  108. });
  109. it('handles utf8 in strings just like crypto', function() {
  110. const algorithm = 'sha256';
  111. test(hash[algorithm], [
  112. 'hello', // one byte per character
  113. 'привет', // two bytes per character
  114. '您好', // three bytes per character
  115. '👋', // four bytes per character
  116. 'hello привет 您好 👋!!!' // mixed character lengths
  117. ].map(str => [str, crypto
  118. .createHash(algorithm)
  119. .update(str)
  120. .digest('hex')]));
  121. });
  122. });