hmac.js 1014 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*!
  2. * Crypto-JS v1.1.0
  3. * http://code.google.com/p/crypto-js/
  4. * Copyright (c) 2009, Jeff Mott. All rights reserved.
  5. * http://code.google.com/p/crypto-js/wiki/License
  6. */
  7. const Crypto = require('./crypto.js');
  8. (function(){
  9. // Shortcut
  10. var util = Crypto.util;
  11. Crypto.HMAC = function (hasher, message, key, options) {
  12. // Allow arbitrary length keys
  13. key = key.length > hasher._blocksize * 4 ?
  14. hasher(key, { asBytes: true }) :
  15. util.stringToBytes(key);
  16. // XOR keys with pad constants
  17. var okey = key,
  18. ikey = key.slice(0);
  19. for (var i = 0; i < hasher._blocksize * 4; i++) {
  20. okey[i] ^= 0x5C;
  21. ikey[i] ^= 0x36;
  22. }
  23. var hmacbytes = hasher(util.bytesToString(okey) +
  24. hasher(util.bytesToString(ikey) + message, { asString: true }),
  25. { asBytes: true });
  26. return options && options.asBytes ? hmacbytes :
  27. options && options.asString ? util.bytesToString(hmacbytes) :
  28. util.bytesToHex(hmacbytes);
  29. };
  30. })();
  31. module.exports = Crypto;