vectors.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var tape = require('tape')
  2. var vectors = require('hash-test-vectors')
  3. // var from = require('bops/typedarray/from')
  4. var Buffer = require('safe-buffer').Buffer
  5. var createHash = require('../')
  6. function makeTest (alg, i, verbose) {
  7. var v = vectors[i]
  8. tape(alg + ': NIST vector ' + i, function (t) {
  9. if (verbose) {
  10. console.log(v)
  11. console.log('VECTOR', i)
  12. console.log('INPUT', v.input)
  13. console.log(Buffer.from(v.input, 'base64').toString('hex'))
  14. }
  15. var buf = Buffer.from(v.input, 'base64')
  16. t.equal(createHash(alg).update(buf).digest('hex'), v[alg])
  17. i = ~~(buf.length / 2)
  18. var buf1 = buf.slice(0, i)
  19. var buf2 = buf.slice(i, buf.length)
  20. console.log(buf1.length, buf2.length, buf.length)
  21. console.log(createHash(alg)._block.length)
  22. t.equal(
  23. createHash(alg)
  24. .update(buf1)
  25. .update(buf2)
  26. .digest('hex'),
  27. v[alg]
  28. )
  29. var j, buf3
  30. i = ~~(buf.length / 3)
  31. j = ~~(buf.length * 2 / 3)
  32. buf1 = buf.slice(0, i)
  33. buf2 = buf.slice(i, j)
  34. buf3 = buf.slice(j, buf.length)
  35. t.equal(
  36. createHash(alg)
  37. .update(buf1)
  38. .update(buf2)
  39. .update(buf3)
  40. .digest('hex'),
  41. v[alg]
  42. )
  43. setTimeout(function () {
  44. // avoid "too much recursion" errors in tape in firefox
  45. t.end()
  46. })
  47. })
  48. }
  49. if (process.argv[2]) {
  50. makeTest(process.argv[2], parseInt(process.argv[3], 10), true)
  51. } else {
  52. vectors.forEach(function (v, i) {
  53. makeTest('sha', i)
  54. makeTest('sha1', i)
  55. makeTest('sha224', i)
  56. makeTest('sha256', i)
  57. makeTest('sha384', i)
  58. makeTest('sha512', i)
  59. })
  60. }