您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

hash.js 1.5KB

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