@ -30,36 +30,25 @@
* [js-md5]{@link https://github.com/emn178/js-md5}
*
* @namespace md5
* @version 0.8 .0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2023
* @version 0.4 .0
* @author Yi-Cyuan Che n [emn178@gmail.com]
* @copyright Yi-Cyuan Chen 2014-2015
* @license MIT
*/
(function () {
(function (root ) {
'use strict';
var INPUT_ERROR = 'input is invalid type';
var FINALIZE_ERROR = 'finalize already called';
var WINDOW = typeof window === 'object';
var root = WINDOW ? window : {};
if (root.JS_MD5_NO_WINDOW) {
WINDOW = false;
}
var WEB_WORKER = !WINDOW & & typeof self === 'object';
var NODE_JS = !root.JS_MD5_NO_NODE_JS & & typeof process === 'object' & & process.versions & & process.versions.node;
var NODE_JS = typeof process == 'object' & & process.versions & & process.versions.node;
if (NODE_JS) {
root = global;
} else if (WEB_WORKER) {
root = self;
}
var COMMON_JS = !root.JS_MD5_NO_COMMON_JS & & typeof module = == 'object' & & module.exports;
var AMD = typeof define === 'function' & & define.amd;
var ARRAY_BUFFER = !root.JS_MD5_NO_ARRAY_BUFFER & & typeof ArrayBuffer != = 'undefined';
var COMMON_JS = !root.JS_MD5_TEST & & typeof module == 'object' & & module.exports;
var AMD = typeof define == 'function' & & define.amd;
var ARRAY_BUFFER = !root.JS_MD5_TEST & & typeof ArrayBuffer != 'undefined';
var HEX_CHARS = '0123456789abcdef'.split('');
var EXTRA = [128, 32768, 8388608, -2147483648];
var SHIFT = [0, 8, 16, 24];
var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer', 'arrayBuffer', 'base64'];
var BASE64_ENCODE_CHAR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer'];
var blocks = [], buffer8;
if (ARRAY_BUFFER) {
@ -68,38 +57,6 @@
blocks = new Uint32Array(buffer);
}
var isArray = Array.isArray;
if (root.JS_MD5_NO_NODE_JS || !isArray) {
isArray = function (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
}
var isView = ArrayBuffer.isView;
if (ARRAY_BUFFER & & (root.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
isView = function (obj) {
return typeof obj === 'object' & & obj.buffer & & obj.buffer.constructor === ArrayBuffer;
};
}
// [message: string, isString: bool]
var formatMessage = function (message) {
var type = typeof message;
if (type === 'string') {
return [message, true];
}
if (type !== 'object' || message === null) {
throw new Error(INPUT_ERROR);
}
if (ARRAY_BUFFER & & message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) & & !isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
}
/**
* @method hex
* @memberof md5
@ -129,18 +86,8 @@
* @example
* md5.array('The quick brown fox jumps over the lazy dog');
*/
/**
* @method arrayBuffer
* @memberof md5
* @description Output hash as ArrayBuffer
* @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
* @returns {ArrayBuffer} ArrayBuffer
* @example
* md5.arrayBuffer('The quick brown fox jumps over the lazy dog');
*/
/**
* @method buffer
* @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
* @memberof md5
* @description Output hash as ArrayBuffer
* @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
@ -148,19 +95,10 @@
* @example
* md5.buffer('The quick brown fox jumps over the lazy dog');
*/
/**
* @method base64
* @memberof md5
* @description Output hash as base64 string
* @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
* @returns {String} base64 string
* @example
* md5.base64('The quick brown fox jumps over the lazy dog');
*/
var createOutputMethod = function (outputType) {
return function (message) {
return function(message) {
return new Md5(true).update(message)[outputType]();
};
}
};
/**
@ -194,7 +132,7 @@
method.update = function (message) {
return method.create().update(message);
};
for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
for (var i = 0;i < OUTPUT_TYPES.length;++i) {
var type = OUTPUT_TYPES[i];
method[type] = createOutputMethod(type);
}
@ -202,145 +140,58 @@
};
var nodeWrap = function (method) {
var crypto = require('crypto')
var Buffer = require('buffer').Buffer;
var bufferFrom;
if (Buffer.from & & !root.JS_MD5_NO_BUFFER_FROM) {
bufferFrom = Buffer.from;
} else {
bufferFrom = function (message) {
return new Buffer(message);
};
var crypto, Buffer;
try {
if (root.JS_MD5_TEST) {
throw 'JS_MD5_TEST';
}
crypto = require('crypto');
Buffer = require('buffer').Buffer;
} catch (e) {
console.log(e);
return method;
}
var nodeMethod = function (message) {
if (typeof message === 'string') {
return crypto.createHash('md5').update(message, 'utf8').digest('hex');
} else {
if (message === null || message === undefined) {
throw new Error(INPUT_ERROR);
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
if (typeof message == 'string') {
if (message.length < = nodeMethod.utf8Threshold) {
return method(message);
} else if (message.length < = nodeMethod.asciiThreshold & & !/[^\x00-\x7F]/.test(message)) {
return method(message);
}
}
if (isArray(message) || isView(message) ||
message.constructor === Buffer) {
return crypto.createHash('md5').update(bufferFrom(message)).digest('hex');
} else {
return crypto.createHash('md5').update(message, 'utf8').digest('hex');
} else if (message.constructor == ArrayBuffer) {
message = new Uint8Array(message);
} else if (message.length === undefined || message.length < = nodeMethod.bytesThreshold) {
return method(message);
}
return crypto.createHash('md5').update(new Buffer(message)).digest('hex');
};
return nodeMethod;
};
/**
* @namespace md5.hmac
*/
/**
* @method hex
* @memberof md5.hmac
* @description Output hash as hex string
* @param {String|Array|Uint8Array|ArrayBuffer} key key
* @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
* @returns {String} Hex string
* @example
* md5.hmac.hex('key', 'The quick brown fox jumps over the lazy dog');
* // equal to
* md5.hmac('key', 'The quick brown fox jumps over the lazy dog');
*/
/**
* @member {Number} utf8Threshold
* @default 18
* @description To use node.js md5 if UTF-8 string length is greater than this value.
* @memberof md5
*/
nodeMethod.utf8Threshold = 18;
/**
* @method digest
* @memberof md5.hmac
* @description Output hash as bytes array
* @param {String|Array|Uint8Array|ArrayBuffer} key key
* @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
* @returns {Array} Bytes array
* @example
* md5.hmac.digest('key', 'The quick brown fox jumps over the lazy dog');
*/
/**
* @method array
* @memberof md5.hmac
* @description Output hash as bytes array
* @param {String|Array|Uint8Array|ArrayBuffer} key key
* @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
* @returns {Array} Bytes array
* @example
* md5.hmac.array('key', 'The quick brown fox jumps over the lazy dog');
*/
/**
* @method arrayBuffer
* @memberof md5.hmac
* @description Output hash as ArrayBuffer
* @param {String|Array|Uint8Array|ArrayBuffer} key key
* @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
* @returns {ArrayBuffer} ArrayBuffer
* @example
* md5.hmac.arrayBuffer('key', 'The quick brown fox jumps over the lazy dog');
*/
/**
* @method buffer
* @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
* @memberof md5.hmac
* @description Output hash as ArrayBuffer
* @param {String|Array|Uint8Array|ArrayBuffer} key key
* @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
* @returns {ArrayBuffer} ArrayBuffer
* @example
* md5.hmac.buffer('key', 'The quick brown fox jumps over the lazy dog');
*/
/**
* @method base64
* @memberof md5.hmac
* @description Output hash as base64 string
* @param {String|Array|Uint8Array|ArrayBuffer} key key
* @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
* @returns {String} base64 string
* @example
* md5.hmac.base64('key', 'The quick brown fox jumps over the lazy dog');
*/
var createHmacOutputMethod = function (outputType) {
return function (key, message) {
return new HmacMd5(key, true).update(message)[outputType]();
};
};
/**
* @member {Number} asciiThreshold
* @default 60
* @description To use node.js md5 if ascii string length is greater than this value.
* @memberof md5
*/
nodeMethod.asciiThreshold = 60;
/**
* @method create
* @memberof md5.hmac
* @description Create HmacMd5 object
* @param {String|Array|Uint8Array|ArrayBuffer} key key
* @returns {HmacMd5} HmacMd5 object.
* @example
* var hash = md5.hmac.create('key');
*/
/**
* @method update
* @memberof md5.hmac
* @description Create and update HmacMd5 object
* @param {String|Array|Uint8Array|ArrayBuffer} key key
* @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
* @returns {HmacMd5} HmacMd5 object.
* @example
* var hash = md5.hmac.update('key', 'The quick brown fox jumps over the lazy dog');
* // equal to
* var hash = md5.hmac.create('key');
* hash.update('The quick brown fox jumps over the lazy dog');
*/
var createHmacMethod = function () {
var method = createHmacOutputMethod('hex');
method.create = function (key) {
return new HmacMd5(key);
};
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);
}
return method;
};
/**
* @member {Number} bytesThreshold
* @default 245
* @description To use node.js md5 if bytes length is greater than this value.
* @memberof md5
*/
nodeMethod.bytesThreshold = 245;
return nodeMethod;
}
/**
* Md5 class
@ -365,7 +216,7 @@
this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
}
}
this.h0 = this.h1 = this.h2 = this.h3 = this.start = this.bytes = this.hBytes = 0;
this.h0 = this.h1 = this.h2 = this.h3 = this.start = this.bytes = 0;
this.finalized = this.hashed = false;
this.first = true;
}
@ -381,13 +232,13 @@
*/
Md5.prototype.update = function (message) {
if (this.finalized) {
throw new Error(FINALIZE_ERROR) ;
return ;
}
var result = formatMessage(message);
message = result[0] ;
var isString = result[1];
var code, index = 0, i, length = message.length, blocks = this.blocks;
var notString = typeof(message) != 'string';
if(notString & & message.constructor == root.ArrayBuffer) {
message = new Uint8Array(message) ;
}
var code, index = 0, i, length = message.length || 0 , blocks = this.blocks;
var buffer8 = this.buffer8;
while (index < length) {
@ -400,58 +251,58 @@
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
}
if (isString) {
if (notString) {
if (ARRAY_BUFFER) {
for (i = this.start;index < length & & i < 64; ++index) {
buffer8[i++] = message[index];
}
} else {
for (i = this.start;index < length & & i < 64; ++index) {
blocks[i >> 2] |= message[index] < < SHIFT[i++ & 3];
}
}
} else {
if (ARRAY_BUFFER) {
for (i = this.start; index < length & & i < 64; ++index) {
for (i = this.start;index < length & & i < 64; ++index) {
code = message.charCodeAt(index);
if (code < 0x80) {
buffer8[i++] = code;
} else if (code < 0x800) {
buffer8[i++] = 0xc0 | (code >>> 6);
buffer8[i++] = 0xc0 | (code >> 6);
buffer8[i++] = 0x80 | (code & 0x3f);
} else if (code < 0xd800 || code >= 0xe000) {
buffer8[i++] = 0xe0 | (code >>> 12);
buffer8[i++] = 0x80 | ((code >>> 6) & 0x3f);
buffer8[i++] = 0xe0 | (code >> 12);
buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
buffer8[i++] = 0x80 | (code & 0x3f);
} else {
code = 0x10000 + (((code & 0x3ff) < < 10) | (message.charCodeAt(++index) & 0x3ff));
buffer8[i++] = 0xf0 | (code >>> 18);
buffer8[i++] = 0x80 | ((code >>> 12) & 0x3f);
buffer8[i++] = 0x80 | ((code >>> 6) & 0x3f);
buffer8[i++] = 0xf0 | (code >> 18);
buffer8[i++] = 0x80 | ((code >> 12) & 0x3f);
buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
buffer8[i++] = 0x80 | (code & 0x3f);
}
}
} else {
for (i = this.start; index < length & & i < 64; ++index) {
for (i = this.start;index < length & & i < 64; ++index) {
code = message.charCodeAt(index);
if (code < 0x80) {
blocks[i >>> 2] |= code < < SHIFT[i++ & 3];
blocks[i >> 2] |= code < < SHIFT[i++ & 3];
} else if (code < 0x800) {
blocks[i >>> 2] |= (0xc0 | (code > >> 6)) < < SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) < < SHIFT[i++ & 3];
blocks[i >> 2] |= (0xc0 | (code >> 6)) < < SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) < < SHIFT[i++ & 3];
} else if (code < 0xd800 || code >= 0xe000) {
blocks[i >>> 2] |= (0xe0 | (code > >> 12)) < < SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code > >> 6) & 0x3f)) < < SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) < < SHIFT[i++ & 3];
blocks[i >> 2] |= (0xe0 | (code >> 12)) < < SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) < < SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) < < SHIFT[i++ & 3];
} else {
code = 0x10000 + (((code & 0x3ff) < < 10) | (message.charCodeAt(++index) & 0x3ff));
blocks[i >>> 2] |= (0xf0 | (code > >> 18)) < < SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code > >> 12) & 0x3f)) < < SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code > >> 6) & 0x3f)) < < SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) < < SHIFT[i++ & 3];
blocks[i >> 2] |= (0xf0 | (code >> 18)) < < SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) < < SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) < < SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) < < SHIFT[i++ & 3];
}
}
}
} else {
if (ARRAY_BUFFER) {
for (i = this.start; index < length & & i < 64; ++index) {
buffer8[i++] = message[index];
}
} else {
for (i = this.start; index < length & & i < 64; ++index) {
blocks[i >>> 2] |= message[index] < < SHIFT[i++ & 3];
}
}
}
this.lastByteIndex = i;
this.bytes += i - this.start;
@ -463,10 +314,6 @@
this.start = i;
}
}
if (this.bytes > 4294967295) {
this.hBytes += this.bytes / 4294967296 < < 0;
this.bytes = this.bytes % 4294967296;
}
return this;
};
@ -476,7 +323,7 @@
}
this.finalized = true;
var blocks = this.blocks, i = this.lastByteIndex;
blocks[i >>> 2] |= EXTRA[i & 3];
blocks[i >> 2] |= EXTRA[i & 3];
if (i >= 56) {
if (!this.hashed) {
this.hash();
@ -488,14 +335,13 @@
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
}
blocks[14] = this.bytes < < 3;
blocks[15] = this.hBytes < < 3 | this.bytes >>> 29;
this.hash();
};
Md5.prototype.hash = function () {
var a, b, c, d, bc, da, blocks = this.blocks;
if (this.first) {
if(this.first) {
a = blocks[0] - 680876937;
a = (a < < 7 | a >>> 25) - 271733879 < < 0;
d = (-1732584194 ^ a & 2004318071) + blocks[1] - 117830708;
@ -648,7 +494,7 @@
b += (d ^ (c | ~a)) + blocks[9] - 343485551;
b = (b < < 21 | b >>> 11) + c < < 0;
if (this.first) {
if(this.first) {
this.h0 = a + 1732584193 < < 0;
this.h1 = b - 271733879 < < 0;
this.h2 = c - 1732584194 < < 0;
@ -677,22 +523,22 @@
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
return HEX_CHARS[(h0 >>> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
HEX_CHARS[(h0 > >> 12) & 0x0F] + HEX_CHARS[(h0 > >> 8) & 0x0F] +
HEX_CHARS[(h0 > >> 20) & 0x0F] + HEX_CHARS[(h0 > >> 16) & 0x0F] +
HEX_CHARS[(h0 > >> 28) & 0x0F] + HEX_CHARS[(h0 > >> 24) & 0x0F] +
HEX_CHARS[(h1 > >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
HEX_CHARS[(h1 > >> 12) & 0x0F] + HEX_CHARS[(h1 > >> 8) & 0x0F] +
HEX_CHARS[(h1 > >> 20) & 0x0F] + HEX_CHARS[(h1 > >> 16) & 0x0F] +
HEX_CHARS[(h1 > >> 28) & 0x0F] + HEX_CHARS[(h1 > >> 24) & 0x0F] +
HEX_CHARS[(h2 > >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
HEX_CHARS[(h2 > >> 12) & 0x0F] + HEX_CHARS[(h2 > >> 8) & 0x0F] +
HEX_CHARS[(h2 > >> 20) & 0x0F] + HEX_CHARS[(h2 > >> 16) & 0x0F] +
HEX_CHARS[(h2 > >> 28) & 0x0F] + HEX_CHARS[(h2 > >> 24) & 0x0F] +
HEX_CHARS[(h3 > >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
HEX_CHARS[(h3 > >> 12) & 0x0F] + HEX_CHARS[(h3 > >> 8) & 0x0F] +
HEX_CHARS[(h3 > >> 20) & 0x0F] + HEX_CHARS[(h3 > >> 16) & 0x0F] +
HEX_CHARS[(h3 > >> 28) & 0x0F] + HEX_CHARS[(h3 > >> 24) & 0x0F];
return HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F];
};
/**
@ -717,15 +563,15 @@
* @example
* hash.digest();
*/
Md5.prototype.digest = function () {
Md5.prototype.digest = function() {
this.finalize();
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
return [
h0 & 0xFF, (h0 >>> 8) & 0xFF, (h0 > >> 16) & 0xFF, (h0 > >> 24) & 0xFF,
h1 & 0xFF, (h1 >>> 8) & 0xFF, (h1 > >> 16) & 0xFF, (h1 > >> 24) & 0xFF,
h2 & 0xFF, (h2 >>> 8) & 0xFF, (h2 > >> 16) & 0xFF, (h2 > >> 24) & 0xFF,
h3 & 0xFF, (h3 >>> 8) & 0xFF, (h3 > >> 16) & 0xFF, (h3 > >> 24) & 0xFF
h0 & 0xFF, (h0 >> 8) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 24) & 0xFF,
h1 & 0xFF, (h1 >> 8) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 24) & 0xFF,
h2 & 0xFF, (h2 >> 8) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 24) & 0xFF,
h3 & 0xFF, (h3 >> 8) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 24) & 0xFF
];
};
@ -742,16 +588,16 @@
Md5.prototype.array = Md5.prototype.digest;
/**
* @method arrayB uffer
* @method b uffer
* @memberof Md5
* @instance
* @description Output hash as ArrayBuffer
* @returns {ArrayBuffer} ArrayBuffer
* @see {@link md5.arrayB uffer}
* @see {@link md5.b uffer}
* @example
* hash.arrayB uffer();
* hash.b uffer();
*/
Md5.prototype.arrayBuffer = function () {
Md5.prototype.buffer = function () {
this.finalize();
var buffer = new ArrayBuffer(16);
@ -763,116 +609,7 @@
return buffer;
};
/**
* @method buffer
* @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
* @memberof Md5
* @instance
* @description Output hash as ArrayBuffer
* @returns {ArrayBuffer} ArrayBuffer
* @see {@link md5.buffer}
* @example
* hash.buffer();
*/
Md5.prototype.buffer = Md5.prototype.arrayBuffer;
/**
* @method base64
* @memberof Md5
* @instance
* @description Output hash as base64 string
* @returns {String} base64 string
* @see {@link md5.base64}
* @example
* hash.base64();
*/
Md5.prototype.base64 = function () {
var v1, v2, v3, base64Str = '', bytes = this.array();
for (var i = 0; i < 15;) {
v1 = bytes[i++];
v2 = bytes[i++];
v3 = bytes[i++];
base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
BASE64_ENCODE_CHAR[(v1 < < 4 | v2 >>> 4) & 63] +
BASE64_ENCODE_CHAR[(v2 < < 2 | v3 >>> 6) & 63] +
BASE64_ENCODE_CHAR[v3 & 63];
}
v1 = bytes[i];
base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
BASE64_ENCODE_CHAR[(v1 < < 4) & 63] +
'==';
return base64Str;
};
/**
* HmacMd5 class
* @class HmacMd5
* @extends Md5
* @description This is internal class.
* @see {@link md5.hmac.create}
*/
function HmacMd5(key, sharedMemory) {
var i, result = formatMessage(key);
key = result[0];
if (result[1]) {
var bytes = [], length = key.length, index = 0, code;
for (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 > 64) {
key = (new Md5(true)).update(key).array();
}
var oKeyPad = [], iKeyPad = [];
for (i = 0; i < 64; ++i) {
var b = key[i] || 0;
oKeyPad[i] = 0x5c ^ b;
iKeyPad[i] = 0x36 ^ b;
}
Md5.call(this, sharedMemory);
this.update(iKeyPad);
this.oKeyPad = oKeyPad;
this.inner = true;
this.sharedMemory = sharedMemory;
}
HmacMd5.prototype = new Md5();
HmacMd5.prototype.finalize = function () {
Md5.prototype.finalize.call(this);
if (this.inner) {
this.inner = false;
var innerHash = this.array();
Md5.call(this, this.sharedMemory);
this.update(this.oKeyPad);
this.update(innerHash);
Md5.prototype.finalize.call(this);
}
};
var exports = createMethod();
exports.md5 = exports;
exports.md5.hmac = createHmacMethod();
if (COMMON_JS) {
module.exports = exports;
@ -901,7 +638,7 @@
});
}
}
}) ();
}(this) );
< / code > < / pre >
< / article >
< / section >
@ -912,13 +649,13 @@
< / div >
< nav >
< h2 > < a href = "index.html" > Home< / a > < / h2 > < h3 > Namespac es< / h3 > < ul > < li > < a href = " md5_.html"> m d5< / a > < / li > < li > < a href = "md5.hmac.html" > hmac< / a > < / li > < / ul > < h3 > Class es< / h3 > < ul > < li > < a href = " H macM d5.html"> H macMd5< / a > < / li > < li > < a href = "Md5.html" > M d5< / a > < / li > < / ul > < h3 > Global< / h3 > < ul > < li > < a href = "global.html#md5%2508" > md5< / a > < / li > < / ul >
< h2 > < a href = "index.html" > Home< / a > < / h2 > < h3 > Class es< / h3 > < ul > < li > < a href = " Md5_.html"> M d5< / a > < / li > < / ul > < h3 > Namespac es< / h3 > < ul > < li > < a href = " md5.html"> md5< / a > < / li > < / ul > < h3 > Global< / h3 > < ul > < li > < a href = "global.html#md5%2508" > md5< / a > < / li > < / ul >
< / nav >
< br class = "clear" >
< footer >
Documentation generated by < a href = "https://github.com/jsdoc /jsdoc"> JSDoc 4.0.2< / a > on Wed Sep 27 2023 21:32:04 GMT+0800 (台北標準時間 )
Documentation generated by < a href = "https://github.com/jsdoc 3/jsdoc"> JSDoc 3.4.0< / a > on Mon Dec 28 2015 15:48:47 GMT+0800 (CST )
< / footer >
< script > prettyPrint ( ) ; < / script >