| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Implementation of HMAC in JavaScript. |
| 17 |
* |
| 18 |
* Usage: |
| 19 |
* var hmac = new goog.crypt.Hmac(new goog.crypt.sha1(), key, 64); |
| 20 |
* var digest = hmac.getHmac(bytes); |
| 21 |
* |
| 22 |
* @author [email protected] (Jige Yu) - port to closure |
| 23 |
*/ |
| 24 |
|
| 25 |
|
| 26 |
goog.provide('goog.crypt.Hmac'); |
| 27 |
|
| 28 |
goog.require('goog.crypt.Hash'); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* @constructor |
| 34 |
* @param {!goog.crypt.Hash} hasher An object to serve as a hash function. |
| 35 |
* @param {Array<number>} key The secret key to use to calculate the hmac. |
| 36 |
* Should be an array of not more than {@code blockSize} integers in |
| 37 |
{0, 255}. |
| 38 |
* @param {number=} opt_blockSize Optional. The block size {@code hasher} uses. |
| 39 |
* If not specified, uses the block size from the hasher, or 16 if it is |
| 40 |
* not specified. |
| 41 |
* @extends {goog.crypt.Hash} |
| 42 |
* @final |
| 43 |
* @struct |
| 44 |
*/ |
| 45 |
goog.crypt.Hmac = function(hasher, key, opt_blockSize) { |
| 46 |
goog.crypt.Hmac.base(this, 'constructor'); |
| 47 |
|
| 48 |
/** |
| 49 |
* The underlying hasher to calculate hash. |
| 50 |
* |
| 51 |
* @type {!goog.crypt.Hash} |
| 52 |
* @private |
| 53 |
*/ |
| 54 |
this.hasher_ = hasher; |
| 55 |
|
| 56 |
this.blockSize = opt_blockSize || hasher.blockSize || 16; |
| 57 |
|
| 58 |
/** |
| 59 |
* The outer padding array of hmac |
| 60 |
* |
| 61 |
* @type {!Array<number>} |
| 62 |
* @private |
| 63 |
*/ |
| 64 |
this.keyO_ = new Array(this.blockSize); |
| 65 |
|
| 66 |
/** |
| 67 |
* The inner padding array of hmac |
| 68 |
* |
| 69 |
* @type {!Array<number>} |
| 70 |
* @private |
| 71 |
*/ |
| 72 |
this.keyI_ = new Array(this.blockSize); |
| 73 |
|
| 74 |
this.initialize_(key); |
| 75 |
}; |
| 76 |
goog.inherits(goog.crypt.Hmac, goog.crypt.Hash); |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* Outer padding byte of HMAC algorith, per http://en.wikipedia.org/wiki/HMAC |
| 81 |
* |
| 82 |
* @type {number} |
| 83 |
* @private |
| 84 |
*/ |
| 85 |
goog.crypt.Hmac.OPAD_ = 0x5c; |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Inner padding byte of HMAC algorith, per http://en.wikipedia.org/wiki/HMAC |
| 90 |
* |
| 91 |
* @type {number} |
| 92 |
* @private |
| 93 |
*/ |
| 94 |
goog.crypt.Hmac.IPAD_ = 0x36; |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Initializes Hmac by precalculating the inner and outer paddings. |
| 99 |
* |
| 100 |
* @param {Array<number>} key The secret key to use to calculate the hmac. |
| 101 |
* Should be an array of not more than {@code blockSize} integers in |
| 102 |
{0, 255}. |
| 103 |
* @private |
| 104 |
*/ |
| 105 |
goog.crypt.Hmac.prototype.initialize_ = function(key) { |
| 106 |
if (key.length > this.blockSize) { |
| 107 |
this.hasher_.update(key); |
| 108 |
key = this.hasher_.digest(); |
| 109 |
this.hasher_.reset(); |
| 110 |
} |
| 111 |
// Precalculate padded and xor'd keys. |
| 112 |
var keyByte; |
| 113 |
for (var i = 0; i < this.blockSize; i++) { |
| 114 |
if (i < key.length) { |
| 115 |
keyByte = key[i]; |
| 116 |
} else { |
| 117 |
keyByte = 0; |
| 118 |
} |
| 119 |
this.keyO_[i] = keyByte ^ goog.crypt.Hmac.OPAD_; |
| 120 |
this.keyI_[i] = keyByte ^ goog.crypt.Hmac.IPAD_; |
| 121 |
} |
| 122 |
// Be ready for an immediate update. |
| 123 |
this.hasher_.update(this.keyI_); |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
/** @override */ |
| 128 |
goog.crypt.Hmac.prototype.reset = function() { |
| 129 |
this.hasher_.reset(); |
| 130 |
this.hasher_.update(this.keyI_); |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
/** @override */ |
| 135 |
goog.crypt.Hmac.prototype.update = function(bytes, opt_length) { |
| 136 |
this.hasher_.update(bytes, opt_length); |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
/** @override */ |
| 141 |
goog.crypt.Hmac.prototype.digest = function() { |
| 142 |
var temp = this.hasher_.digest(); |
| 143 |
this.hasher_.reset(); |
| 144 |
this.hasher_.update(this.keyO_); |
| 145 |
this.hasher_.update(temp); |
| 146 |
return this.hasher_.digest(); |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* Calculates an HMAC for a given message. |
| 152 |
* |
| 153 |
* @param {Array<number>|Uint8Array|string} message Data to Hmac. |
| 154 |
* @return {!Array<number>} the digest of the given message. |
| 155 |
*/ |
| 156 |
goog.crypt.Hmac.prototype.getHmac = function(message) { |
| 157 |
this.reset(); |
| 158 |
this.update(message); |
| 159 |
return this.digest(); |
| 160 |
}; |