test.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var test = require('tape')
  2. var Buffer = require('safe-buffer').Buffer
  3. var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160', 'ripemd160']
  4. var encodings = ['hex', 'base64'] // ignore binary
  5. var vectors = require('hash-test-vectors')
  6. vectors.forEach(function (vector) {
  7. vector.ripemd160 = vector.rmd160
  8. })
  9. var createHash = require('./browser')
  10. algorithms.forEach(function (algorithm) {
  11. test('test ' + algorithm + ' against test vectors', function (t) {
  12. vectors.forEach(function (obj, i) {
  13. var input = Buffer.from(obj.input, 'base64')
  14. var node = obj[algorithm]
  15. var js = createHash(algorithm).update(input).digest('hex')
  16. t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
  17. })
  18. encodings.forEach(function (encoding) {
  19. vectors.forEach(function (obj, i) {
  20. var input = Buffer.from(obj.input, 'base64').toString(encoding)
  21. var node = obj[algorithm]
  22. var js = createHash(algorithm).update(input, encoding).digest('hex')
  23. t.equal(js, node, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + node)
  24. })
  25. })
  26. vectors.forEach(function (obj, i) {
  27. var input = Buffer.from(obj.input, 'base64')
  28. var node = obj[algorithm]
  29. var hash = createHash(algorithm)
  30. hash.end(input)
  31. var js = hash.read().toString('hex')
  32. t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
  33. })
  34. t.end()
  35. })
  36. })