mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2024-12-17 19:54:23 -05:00
41 lines
1.1 KiB
JavaScript
41 lines
1.1 KiB
JavaScript
|
/** @fileOverview HMAC implementation.
|
||
|
*
|
||
|
* @author Emily Stark
|
||
|
* @author Mike Hamburg
|
||
|
* @author Dan Boneh
|
||
|
*/
|
||
|
|
||
|
/** HMAC with the specified hash function.
|
||
|
* @constructor
|
||
|
* @param {bitArray} key the key for HMAC.
|
||
|
* @param {Object} [hash=sjcl.hash.sha256] The hash function to use.
|
||
|
*/
|
||
|
sjcl.misc.hmac = function (key, Hash) {
|
||
|
this._hash = Hash = Hash || sjcl.hash.sha256;
|
||
|
var exKey = [[],[]], i,
|
||
|
bs = Hash.prototype.blockSize / 32;
|
||
|
this._baseHash = [new Hash(), new Hash()];
|
||
|
|
||
|
if (key.length > bs) {
|
||
|
key = Hash.hash(key);
|
||
|
}
|
||
|
|
||
|
for (i=0; i<bs; i++) {
|
||
|
exKey[0][i] = key[i]^0x36363636;
|
||
|
exKey[1][i] = key[i]^0x5C5C5C5C;
|
||
|
}
|
||
|
|
||
|
this._baseHash[0].update(exKey[0]);
|
||
|
this._baseHash[1].update(exKey[1]);
|
||
|
};
|
||
|
|
||
|
/** HMAC with the specified hash function. Also called encrypt since it's a prf.
|
||
|
* @param {bitArray|String} data The data to mac.
|
||
|
* @param {Codec} [encoding] the encoding function to use.
|
||
|
*/
|
||
|
sjcl.misc.hmac.prototype.encrypt = sjcl.misc.hmac.prototype.mac = function (data, encoding) {
|
||
|
var w = new (this._hash)(this._baseHash[0]).update(data, encoding).finalize();
|
||
|
return new (this._hash)(this._baseHash[1]).update(w).finalize();
|
||
|
};
|
||
|
|