mirror of
https://github.com/emn178/js-sha512.git
synced 2026-08-13 04:51:38 +08:00
Added support HMAC. #8
This commit is contained in:
+101
-2
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* [js-sha512]{@link https://github.com/emn178/js-sha512}
|
||||
*
|
||||
* @version 0.6.0
|
||||
* @version 0.7.0
|
||||
* @author Chen, Yi-Cyuan [emn178@gmail.com]
|
||||
* @copyright Chen, Yi-Cyuan 2014-2017
|
||||
* @license MIT
|
||||
@@ -109,6 +109,27 @@
|
||||
return method;
|
||||
};
|
||||
|
||||
var createHmacOutputMethod = function (outputType, bits) {
|
||||
return function (key, message) {
|
||||
return new HmacSha512(key, bits, true).update(message)[outputType]();
|
||||
};
|
||||
};
|
||||
|
||||
var createHmacMethod = function (bits) {
|
||||
var method = createHmacOutputMethod('hex', bits);
|
||||
method.create = function (key) {
|
||||
return new HmacSha512(key, bits);
|
||||
};
|
||||
method.update = function (key, message) {
|
||||
return method.create(key).update(message);
|
||||
};
|
||||
for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
|
||||
var type = OUTPUT_TYPES[i];
|
||||
method[type] = createHmacOutputMethod(type, bits);
|
||||
}
|
||||
return method;
|
||||
};
|
||||
|
||||
function Sha512(bits, sharedMemory) {
|
||||
if (sharedMemory) {
|
||||
blocks[0] = blocks[1] = blocks[2] = blocks[3] = blocks[4] =
|
||||
@@ -220,7 +241,6 @@
|
||||
}
|
||||
notString = true;
|
||||
}
|
||||
var length = message.length;
|
||||
var code, index = 0, i, length = message.length, blocks = this.blocks;
|
||||
|
||||
while (index < length) {
|
||||
@@ -776,11 +796,90 @@
|
||||
return buffer;
|
||||
};
|
||||
|
||||
function HmacSha512(key, bits, sharedMemory) {
|
||||
var notString, type = typeof key;
|
||||
if (type !== 'string') {
|
||||
if (type === 'object') {
|
||||
if (key === null) {
|
||||
throw ERROR;
|
||||
} else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
|
||||
key = new Uint8Array(key);
|
||||
} else if (!Array.isArray(key)) {
|
||||
if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
|
||||
throw ERROR;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw ERROR;
|
||||
}
|
||||
notString = true;
|
||||
}
|
||||
var length = key.length;
|
||||
if (!notString) {
|
||||
var bytes = [], length = key.length, index = 0, code;
|
||||
for (var i = 0; i < length; ++i) {
|
||||
code = key.charCodeAt(i);
|
||||
if (code < 0x80) {
|
||||
bytes[index++] = code;
|
||||
} else if (code < 0x800) {
|
||||
bytes[index++] = (0xc0 | (code >> 6));
|
||||
bytes[index++] = (0x80 | (code & 0x3f));
|
||||
} else if (code < 0xd800 || code >= 0xe000) {
|
||||
bytes[index++] = (0xe0 | (code >> 12));
|
||||
bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
|
||||
bytes[index++] = (0x80 | (code & 0x3f));
|
||||
} else {
|
||||
code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
|
||||
bytes[index++] = (0xf0 | (code >> 18));
|
||||
bytes[index++] = (0x80 | ((code >> 12) & 0x3f));
|
||||
bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
|
||||
bytes[index++] = (0x80 | (code & 0x3f));
|
||||
}
|
||||
}
|
||||
key = bytes;
|
||||
}
|
||||
|
||||
if (key.length > 128) {
|
||||
key = (new Sha512(bits, true)).update(key).array();
|
||||
}
|
||||
|
||||
var oKeyPad = [], iKeyPad = [];
|
||||
for (var i = 0; i < 128; ++i) {
|
||||
var b = key[i] || 0;
|
||||
oKeyPad[i] = 0x5c ^ b;
|
||||
iKeyPad[i] = 0x36 ^ b;
|
||||
}
|
||||
|
||||
Sha512.call(this, bits, sharedMemory);
|
||||
|
||||
this.update(iKeyPad);
|
||||
this.oKeyPad = oKeyPad;
|
||||
this.inner = true;
|
||||
this.sharedMemory = sharedMemory;
|
||||
}
|
||||
HmacSha512.prototype = new Sha512();
|
||||
|
||||
HmacSha512.prototype.finalize = function () {
|
||||
Sha512.prototype.finalize.call(this);
|
||||
if (this.inner) {
|
||||
this.inner = false;
|
||||
var innerHash = this.array();
|
||||
Sha512.call(this, this.bits, this.sharedMemory);
|
||||
this.update(this.oKeyPad);
|
||||
this.update(innerHash);
|
||||
Sha512.prototype.finalize.call(this);
|
||||
}
|
||||
};
|
||||
|
||||
var exports = createMethod(512);
|
||||
exports.sha512 = exports;
|
||||
exports.sha384 = createMethod(384);
|
||||
exports.sha512_256 = createMethod(256);
|
||||
exports.sha512_224 = createMethod(224);
|
||||
exports.sha512.hmac = createHmacMethod(512);
|
||||
exports.sha384.hmac = createHmacMethod(384);
|
||||
exports.sha512_256.hmac = createHmacMethod(256);
|
||||
exports.sha512_224.hmac = createHmacMethod(224);
|
||||
|
||||
if (COMMON_JS) {
|
||||
module.exports = exports;
|
||||
|
||||
Reference in New Issue
Block a user