test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use strict';
  2. var crypto = require('crypto');
  3. var tape = require('tape');
  4. var Buffer = require('safe-buffer').Buffer;
  5. var Sha1 = require('../').sha1;
  6. var nodeSupportsUint16 = false;
  7. try {
  8. crypto.createHash('sha1').update(new Uint16Array());
  9. nodeSupportsUint16 = true;
  10. } catch (err) {}
  11. var inputs = [
  12. ['', 'ascii'],
  13. ['abc', 'ascii'],
  14. ['123', 'ascii'],
  15. ['123456789abcdef123456789abcdef123456789abcdef123456789abcdef', 'ascii'],
  16. ['123456789abcdef123456789abcdef123456789abcdef123456789abc', 'ascii'],
  17. ['123456789abcdef123456789abcdef123456789abcdef123456789ab', 'ascii'],
  18. ['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde', 'ascii'],
  19. ['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', 'ascii'],
  20. ['foobarbaz', 'ascii'],
  21. [Buffer.from('buffer')],
  22. nodeSupportsUint16 ? [new Uint16Array([1, 2, 3])] : null
  23. ].filter(Boolean);
  24. tape("hash is the same as node's crypto", function (t) {
  25. inputs.forEach(function (v) {
  26. var a = new Sha1().update(v[0], v[1]).digest('hex');
  27. var e = crypto.createHash('sha1').update(v[0], v[1]).digest('hex');
  28. t.equal(a, e, a + ' == ' + e);
  29. });
  30. t.end();
  31. });
  32. tape('call update multiple times', function (t) {
  33. inputs.forEach(function (v) {
  34. var hash = new Sha1();
  35. var sha1hash = crypto.createHash('sha1');
  36. for (var i = 0; i < v[0].length; i = (i + 1) * 2) {
  37. var s = v[0].slice(i, (i + 1) * 2);
  38. hash.update(s, v[1]);
  39. sha1hash.update(s, v[1]);
  40. }
  41. var a = hash.digest('hex');
  42. var e = sha1hash.digest('hex');
  43. t.equal(a, e, a + ' == ' + e);
  44. });
  45. t.end();
  46. });
  47. tape('call update twice', function (t) {
  48. var sha1hash = crypto.createHash('sha1');
  49. var hash = new Sha1();
  50. sha1hash.update('foo', 'ascii');
  51. hash.update('foo', 'ascii');
  52. sha1hash.update('bar', 'ascii');
  53. hash.update('bar', 'ascii');
  54. sha1hash.update('baz', 'ascii');
  55. hash.update('baz', 'ascii');
  56. var a = hash.digest('hex');
  57. var e = sha1hash.digest('hex');
  58. t.equal(a, e);
  59. t.end();
  60. });
  61. tape('hex encoding', function (t) {
  62. inputs.forEach(function (v) {
  63. var hash = new Sha1();
  64. var sha1hash = crypto.createHash('sha1');
  65. for (var i = 0; i < v[0].length; i = (i + 1) * 2) {
  66. var s = v[0].slice(i, (i + 1) * 2);
  67. hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex');
  68. sha1hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex');
  69. }
  70. var a = hash.digest('hex');
  71. var e = sha1hash.digest('hex');
  72. t.equal(a, e, a + ' == ' + e);
  73. });
  74. t.end();
  75. });
  76. tape('throws on invalid input', function (t) {
  77. var invalid = [
  78. {}, // non-arrayish
  79. { length: 20 }, // undefined values
  80. [NaN], // non-numbers
  81. [[]], // non-numbers
  82. [1, 1.5], // non-integers
  83. [1, 256], // out of bounds
  84. [-1, 0] // out of bounds
  85. ];
  86. invalid.forEach(function (input) {
  87. var hash = new Sha1();
  88. t['throws'](function () {
  89. hash.update(input);
  90. hash.digest('hex');
  91. });
  92. });
  93. t.end();
  94. });
  95. tape('call digest for more than MAX_UINT32 bits of data', function (t) {
  96. var sha1hash = crypto.createHash('sha1');
  97. var hash = new Sha1();
  98. var bigData;
  99. try {
  100. bigData = Buffer.alloc(0x1ffffffff / 8);
  101. } catch (err) {
  102. // node < 3 has a lower buffer size limit than node 3+. node 0.10 requires the `/8`, 0.12 - 2 are fine with `-8`
  103. bigData = Buffer.alloc(0x3fffffff / 8);
  104. }
  105. hash.update(bigData);
  106. sha1hash.update(bigData);
  107. var a = hash.digest('hex');
  108. var e = sha1hash.digest('hex');
  109. t.equal(a, e, a + ' == ' + e);
  110. t.end();
  111. });