create-hmac.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. var test = require('tape');
  3. var satisfies = require('semver').satisfies;
  4. var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160'];
  5. var vectors = require('hash-test-vectors/hmac');
  6. function testLib(name, createHmac) {
  7. algorithms.forEach(function (alg) {
  8. var isUnsupported = satisfies(process.version, '^17') && (
  9. alg === 'rmd160'
  10. || alg === 'hmac(rmd160)'
  11. );
  12. test(
  13. name + ' hmac(' + alg + ')',
  14. { skip: isUnsupported && 'this node version does not support ' + alg },
  15. function (t) {
  16. vectors.forEach(function (input) {
  17. var output = createHmac(alg, new Buffer(input.key, 'hex'))
  18. .update(input.data, 'hex').digest();
  19. output = input.truncate ? output.slice(0, input.truncate) : output;
  20. output = output.toString('hex');
  21. t.equal(output, input[alg]);
  22. });
  23. t.end();
  24. }
  25. );
  26. test(
  27. 'hmac(' + alg + ')',
  28. { skip: isUnsupported && 'this node version does not support ' + alg },
  29. function (t) {
  30. vectors.forEach(function (input) {
  31. var hmac = createHmac(alg, new Buffer(input.key, 'hex'));
  32. hmac.end(input.data, 'hex');
  33. var output = hmac.read();
  34. output = input.truncate ? output.slice(0, input.truncate) : output;
  35. output = output.toString('hex');
  36. t.equal(output, input[alg]);
  37. });
  38. t.end();
  39. }
  40. );
  41. });
  42. }
  43. testLib('createHmac in crypto-browserify', require('../').createHmac);
  44. testLib('create-hmac/browser', require('create-hmac/browser'));