create-hmac.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. var test = require('tape')
  2. var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
  3. var vectors = require('hash-test-vectors/hmac')
  4. testLib('createHmac in crypto-browserify', require('../').createHmac)
  5. testLib('create-hmac/browser', require('create-hmac/browser'))
  6. function testLib (name, createHmac) {
  7. algorithms.forEach(function (alg) {
  8. test(name + ' hmac(' + alg + ')', function (t) {
  9. run(0)
  10. function run (i) {
  11. if (i >= vectors.length) {
  12. return t.end()
  13. }
  14. var input = vectors[i]
  15. var output = createHmac(alg, new Buffer(input.key, 'hex'))
  16. .update(input.data, 'hex').digest()
  17. output = input.truncate ? output.slice(0, input.truncate) : output
  18. output = output.toString('hex')
  19. if (output !== input[alg]) {
  20. t.equal(output, input[alg])
  21. }
  22. setTimeout(run, 0, i + 1)
  23. }
  24. })
  25. test('hmac(' + alg + ')', function (t) {
  26. run(0)
  27. function run (i) {
  28. if (i >= vectors.length) {
  29. return t.end()
  30. }
  31. var input = vectors[i]
  32. var hmac = createHmac(alg, new Buffer(input.key, 'hex'))
  33. hmac.end(input.data, 'hex')
  34. var output = hmac.read()
  35. output = input.truncate ? output.slice(0, input.truncate) : output
  36. output = output.toString('hex')
  37. if (output !== input[alg]) {
  38. t.equal(output, input[alg])
  39. }
  40. setTimeout(run, 0, i + 1)
  41. }
  42. })
  43. })
  44. }