hmac.js 984 B

123456789101112131415161718192021222324252627282930313233343536
  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. (function(){
  8. // Shortcut
  9. var util = Crypto.util;
  10. Crypto.HMAC = function (hasher, message, key, options) {
  11. // Allow arbitrary length keys
  12. key = key.length > hasher._blocksize * 4 ?
  13. hasher(key, { asBytes: true }) :
  14. util.stringToBytes(key);
  15. // XOR keys with pad constants
  16. var okey = key,
  17. ikey = key.slice(0);
  18. for (var i = 0; i < hasher._blocksize * 4; i++) {
  19. okey[i] ^= 0x5C;
  20. ikey[i] ^= 0x36;
  21. }
  22. var hmacbytes = hasher(util.bytesToString(okey) +
  23. hasher(util.bytesToString(ikey) + message, { asString: true }),
  24. { asBytes: true });
  25. return options && options.asBytes ? hmacbytes :
  26. options && options.asString ? util.bytesToString(hmacbytes) :
  27. util.bytesToHex(hmacbytes);
  28. };
  29. })();