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

test.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var crypto = require('crypto')
  2. var tape = require('tape')
  3. var Sha1 = require('../').sha1
  4. var inputs = [
  5. ['', 'ascii'],
  6. ['abc', 'ascii'],
  7. ['123', 'ascii'],
  8. ['123456789abcdef123456789abcdef123456789abcdef123456789abcdef', 'ascii'],
  9. ['123456789abcdef123456789abcdef123456789abcdef123456789abc', 'ascii'],
  10. ['123456789abcdef123456789abcdef123456789abcdef123456789ab', 'ascii'],
  11. ['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde', 'ascii'],
  12. ['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', 'ascii'],
  13. ['foobarbaz', 'ascii']
  14. ]
  15. tape("hash is the same as node's crypto", function (t) {
  16. inputs.forEach(function (v) {
  17. var a = new Sha1().update(v[0], v[1]).digest('hex')
  18. var e = crypto.createHash('sha1').update(v[0], v[1]).digest('hex')
  19. console.log(a, '==', e)
  20. t.equal(a, e)
  21. })
  22. t.end()
  23. })
  24. tape('call update multiple times', function (t) {
  25. inputs.forEach(function (v) {
  26. var hash = new Sha1()
  27. var _hash = crypto.createHash('sha1')
  28. for (var i = 0; i < v[0].length; i = (i + 1) * 2) {
  29. var s = v[0].substring(i, (i + 1) * 2)
  30. hash.update(s, v[1])
  31. _hash.update(s, v[1])
  32. }
  33. var a = hash.digest('hex')
  34. var e = _hash.digest('hex')
  35. console.log(a, '==', e)
  36. t.equal(a, e)
  37. })
  38. t.end()
  39. })
  40. tape('call update twice', function (t) {
  41. var _hash = crypto.createHash('sha1')
  42. var hash = new Sha1()
  43. _hash.update('foo', 'ascii')
  44. hash.update('foo', 'ascii')
  45. _hash.update('bar', 'ascii')
  46. hash.update('bar', 'ascii')
  47. _hash.update('baz', 'ascii')
  48. hash.update('baz', 'ascii')
  49. var a = hash.digest('hex')
  50. var e = _hash.digest('hex')
  51. t.equal(a, e)
  52. t.end()
  53. })
  54. tape('hex encoding', function (t) {
  55. inputs.forEach(function (v) {
  56. var hash = new Sha1()
  57. var _hash = crypto.createHash('sha1')
  58. for (var i = 0; i < v[0].length; i = (i + 1) * 2) {
  59. var s = v[0].substring(i, (i + 1) * 2)
  60. hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex')
  61. _hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex')
  62. }
  63. var a = hash.digest('hex')
  64. var e = _hash.digest('hex')
  65. console.log(a, '==', e)
  66. t.equal(a, e)
  67. })
  68. t.end()
  69. })
  70. tape('call digest for more than MAX_UINT32 bits of data', function (t) {
  71. var _hash = crypto.createHash('sha1')
  72. var hash = new Sha1()
  73. var bigData = Buffer.alloc(0x1ffffffff / 8)
  74. hash.update(bigData)
  75. _hash.update(bigData)
  76. var a = hash.digest('hex')
  77. var e = _hash.digest('hex')
  78. t.equal(a, e)
  79. t.end()
  80. })