hash.js 1.8 KB

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