123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- goog.provide('goog.crypt.Hmac');
- goog.require('goog.crypt.Hash');
- goog.crypt.Hmac = function(hasher, key, opt_blockSize) {
- goog.crypt.Hmac.base(this, 'constructor');
-
- this.hasher_ = hasher;
- this.blockSize = opt_blockSize || hasher.blockSize || 16;
-
- this.keyO_ = new Array(this.blockSize);
-
- this.keyI_ = new Array(this.blockSize);
- this.initialize_(key);
- };
- goog.inherits(goog.crypt.Hmac, goog.crypt.Hash);
- goog.crypt.Hmac.OPAD_ = 0x5c;
- goog.crypt.Hmac.IPAD_ = 0x36;
- goog.crypt.Hmac.prototype.initialize_ = function(key) {
- if (key.length > this.blockSize) {
- this.hasher_.update(key);
- key = this.hasher_.digest();
- this.hasher_.reset();
- }
-
- var keyByte;
- for (var i = 0; i < this.blockSize; i++) {
- if (i < key.length) {
- keyByte = key[i];
- } else {
- keyByte = 0;
- }
- this.keyO_[i] = keyByte ^ goog.crypt.Hmac.OPAD_;
- this.keyI_[i] = keyByte ^ goog.crypt.Hmac.IPAD_;
- }
-
- this.hasher_.update(this.keyI_);
- };
- goog.crypt.Hmac.prototype.reset = function() {
- this.hasher_.reset();
- this.hasher_.update(this.keyI_);
- };
- goog.crypt.Hmac.prototype.update = function(bytes, opt_length) {
- this.hasher_.update(bytes, opt_length);
- };
- goog.crypt.Hmac.prototype.digest = function() {
- var temp = this.hasher_.digest();
- this.hasher_.reset();
- this.hasher_.update(this.keyO_);
- this.hasher_.update(temp);
- return this.hasher_.digest();
- };
- goog.crypt.Hmac.prototype.getHmac = function(message) {
- this.reset();
- this.update(message);
- return this.digest();
- };
|