vectors.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. var tape = require('tape');
  3. var vectors = require('hash-test-vectors');
  4. // var from = require('bops/typedarray/from')
  5. var Buffer = require('safe-buffer').Buffer;
  6. var createHash = require('../');
  7. function makeTest(alg, i, verbose) {
  8. var v = vectors[i];
  9. tape(alg + ': NIST vector ' + i, function (t) {
  10. if (verbose) {
  11. t.comment(v);
  12. t.comment('VECTOR', i);
  13. t.comment('INPUT', v.input);
  14. t.comment(Buffer.from(v.input, 'base64').toString('hex'));
  15. }
  16. var buf = Buffer.from(v.input, 'base64');
  17. t.equal(createHash(alg).update(buf).digest('hex'), v[alg]);
  18. // eslint-disable-next-line no-param-reassign
  19. i = ~~(buf.length / 2);
  20. var buf1 = buf.slice(0, i);
  21. var buf2 = buf.slice(i, buf.length);
  22. t.comment(buf1.length + ', ' + buf2.length + ', ' + buf.length);
  23. t.comment(createHash(alg)._block.length);
  24. t.equal(
  25. createHash(alg)
  26. .update(buf1)
  27. .update(buf2)
  28. .digest('hex'),
  29. v[alg]
  30. );
  31. var j, buf3;
  32. // eslint-disable-next-line no-param-reassign
  33. i = ~~(buf.length / 3);
  34. j = ~~(buf.length * 2 / 3);
  35. buf1 = buf.slice(0, i);
  36. buf2 = buf.slice(i, j);
  37. buf3 = buf.slice(j, buf.length);
  38. t.equal(
  39. createHash(alg)
  40. .update(buf1)
  41. .update(buf2)
  42. .update(buf3)
  43. .digest('hex'),
  44. v[alg]
  45. );
  46. setTimeout(function () {
  47. // avoid "too much recursion" errors in tape in firefox
  48. t.end();
  49. });
  50. });
  51. }
  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. });