You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.6 KiB

4 years ago
  1. var test = require('tape')
  2. var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
  3. var encodings = ['hex', 'base64'] // FIXME: test binary
  4. var vectors = require('hash-test-vectors')
  5. testLib('createHash in crypto-browserify', require('../').createHash)
  6. testLib('create-hash/browser', require('create-hash/browser'))
  7. function testLib (name, createHash) {
  8. algorithms.forEach(function (algorithm) {
  9. runTest(name, createHash, algorithm)
  10. })
  11. }
  12. function runTest (name, createHash, algorithm) {
  13. test(name + ' test ' + algorithm + ' against test vectors', function (t) {
  14. run(0)
  15. function run (i) {
  16. if (i >= vectors.length) {
  17. return t.end()
  18. }
  19. var obj = vectors[i]
  20. var input = new Buffer(obj.input, 'base64')
  21. var node = obj[algorithm]
  22. var js = createHash(algorithm).update(input).digest('hex')
  23. if (js !== node) {
  24. t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
  25. }
  26. encodings.forEach(function (encoding) {
  27. var input = new Buffer(obj.input, 'base64').toString(encoding)
  28. var node = obj[algorithm]
  29. var js = createHash(algorithm).update(input, encoding).digest('hex')
  30. if (js !== node) {
  31. t.equal(js, node, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + node)
  32. }
  33. })
  34. input = new Buffer(obj.input, 'base64')
  35. node = obj[algorithm]
  36. var hash = createHash(algorithm)
  37. hash.end(input)
  38. js = hash.read().toString('hex')
  39. if (js !== node) {
  40. t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
  41. }
  42. setTimeout(run, 0, i + 1)
  43. }
  44. })
  45. }