hash.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict';
  2. var tape = require('tape');
  3. var Buffer = require('safe-buffer').Buffer;
  4. var Hash = require('../hash');
  5. var hex = '0A1B2C3D4E5F6G7H';
  6. function equal(t, a, b) {
  7. t.equal(a.length, b.length);
  8. t.equal(a.toString('hex'), b.toString('hex'));
  9. }
  10. var hexBuf = Buffer.from('0A1B2C3D4E5F6G7H', 'utf8');
  11. var count16 = {
  12. strings: ['0A1B2C3D4E5F6G7H'],
  13. buffers: [
  14. hexBuf,
  15. Buffer.from('80000000000000000000000000000080', 'hex')
  16. ]
  17. };
  18. var empty = {
  19. strings: [''],
  20. buffers: [
  21. Buffer.from('80000000000000000000000000000000', 'hex')
  22. ]
  23. };
  24. var multi = {
  25. strings: ['abcd', 'efhijk', 'lmnopq'],
  26. buffers: [
  27. Buffer.from('abcdefhijklmnopq', 'ascii'),
  28. Buffer.from('80000000000000000000000000000080', 'hex')
  29. ]
  30. };
  31. var long = {
  32. strings: [hex + hex],
  33. buffers: [
  34. hexBuf,
  35. hexBuf,
  36. Buffer.from('80000000000000000000000000000100', 'hex')
  37. ]
  38. };
  39. function makeTest(name, data) {
  40. tape(name, function (t) {
  41. var h = new Hash(16, 8);
  42. var hash = Buffer.alloc(20);
  43. var n = 2;
  44. var expected = data.buffers.slice();
  45. // t.plan(expected.length + 1)
  46. h._update = function (block) {
  47. var e = expected.shift();
  48. equal(t, block, e);
  49. if (n < 0) {
  50. throw new Error('expecting only 2 calls to _update');
  51. }
  52. };
  53. h._hash = function () {
  54. return hash;
  55. };
  56. data.strings.forEach(function (string) {
  57. h.update(string, 'ascii');
  58. });
  59. equal(t, h.digest(), hash);
  60. t.end();
  61. });
  62. }
  63. makeTest('Hash#update 1 in 1', count16);
  64. makeTest('empty Hash#update', empty);
  65. makeTest('Hash#update 1 in 3', multi);
  66. makeTest('Hash#update 2 in 1', long);