mirror of https://github.com/emn178/js-md5.git
Compare commits
11 Commits
Author | SHA1 | Date |
---|---|---|
![]() |
4c40b94725 | 2 years ago |
![]() |
1ed1b0677d | 2 years ago |
![]() |
698290b4bc | 2 years ago |
![]() |
0ecaeee185 | 2 years ago |
![]() |
daf1188dc6 | 2 years ago |
![]() |
a9de3df8c6 | 2 years ago |
![]() |
08049ec6fa | 2 years ago |
![]() |
bdf1150097 | 2 years ago |
![]() |
c2e8d8683d | 2 years ago |
![]() |
c154bc5a38 | 2 years ago |
![]() |
0263ab835f | 8 years ago |
@ -1,2 +0,0 @@
|
||||
/node_modules/
|
||||
/tests/
|
@ -1,2 +1,4 @@
|
||||
/node_modules/
|
||||
/covreporter/
|
||||
/.nyc_output/
|
||||
/coverage/
|
||||
|
@ -0,0 +1,9 @@
|
||||
/node_modules/
|
||||
/coverage/
|
||||
/.nyc_output/
|
||||
/tests/
|
||||
/doc/
|
||||
.covignore
|
||||
.travis.yml
|
||||
.npmignore
|
||||
bower.json
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,168 @@
|
||||
type Message = string | number[] | ArrayBuffer | Uint8Array;
|
||||
|
||||
interface Hasher {
|
||||
/**
|
||||
* Update hash
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
update(message: Message): Hasher;
|
||||
|
||||
/**
|
||||
* Return hash in hex string.
|
||||
*/
|
||||
hex(): string;
|
||||
|
||||
/**
|
||||
* Return hash in hex string.
|
||||
*/
|
||||
toString(): string;
|
||||
|
||||
/**
|
||||
* Return hash in ArrayBuffer.
|
||||
*/
|
||||
arrayBuffer(): ArrayBuffer;
|
||||
|
||||
/**
|
||||
* Return hash in integer array.
|
||||
*/
|
||||
digest(): number[];
|
||||
|
||||
/**
|
||||
* Return hash in integer array.
|
||||
*/
|
||||
array(): number[];
|
||||
|
||||
/**
|
||||
* Return hash in base64 string.
|
||||
*/
|
||||
base64(): string;
|
||||
}
|
||||
|
||||
interface Hmac {
|
||||
/**
|
||||
* Computes a Hash-based message authentication code (HMAC) using a secret key
|
||||
*
|
||||
* @param secretKey The Secret Key
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
(secretKey: Message, message: Message): string;
|
||||
|
||||
/**
|
||||
* Create a hash object using a secret key.
|
||||
*
|
||||
* @param secretKey The Secret Key
|
||||
*/
|
||||
create(secretKey: Message): Hasher;
|
||||
|
||||
/**
|
||||
* Create a hash object and hash message using a secret key
|
||||
*
|
||||
* @param secretKey The Secret Key
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
update(secretKey: Message, message: Message): Hasher;
|
||||
|
||||
/**
|
||||
* Return hash in hex string.
|
||||
*
|
||||
* @param secretKey The Secret Key
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
hex(secretKey: Message, message: Message): string;
|
||||
|
||||
/**
|
||||
* Return hash in ArrayBuffer.
|
||||
*
|
||||
* @param secretKey The Secret Key
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
arrayBuffer(secretKey: Message, message: Message): ArrayBuffer;
|
||||
|
||||
/**
|
||||
* Return hash in integer array.
|
||||
*
|
||||
* @param secretKey The Secret Key
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
digest(secretKey: Message, message: Message): number[];
|
||||
|
||||
/**
|
||||
* Return hash in integer array.
|
||||
*
|
||||
* @param secretKey The Secret Key
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
array(secretKey: Message, message: Message): number[];
|
||||
|
||||
/**
|
||||
* Return hash in base64 string.
|
||||
*
|
||||
* @param secretKey The Secret Key
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
base64(secretKey: Message, message: Message): string;
|
||||
}
|
||||
|
||||
interface Hash {
|
||||
/**
|
||||
* Hash and return hex string.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
(message: Message): string;
|
||||
|
||||
/**
|
||||
* Create a hash object.
|
||||
*/
|
||||
create(): Hasher;
|
||||
|
||||
/**
|
||||
* Create a hash object and hash message.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
update(message: Message): Hasher;
|
||||
|
||||
/**
|
||||
* Return hash in hex string.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
hex(message: Message): string;
|
||||
|
||||
/**
|
||||
* Return hash in ArrayBuffer.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
arrayBuffer(message: Message): ArrayBuffer;
|
||||
|
||||
/**
|
||||
* Return hash in integer array.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
digest(message: Message): number[];
|
||||
|
||||
/**
|
||||
* Return hash in integer array.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
array(message: Message): number[];
|
||||
|
||||
/**
|
||||
* Return hash in base64 string.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
base64(message: Message): string;
|
||||
|
||||
/**
|
||||
* HMAC interface
|
||||
*/
|
||||
hmac: Hmac;
|
||||
}
|
||||
|
||||
export var md5: Hash;
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,202 @@
|
||||
(function (md5) {
|
||||
Array.prototype.toHexString = ArrayBuffer.prototype.toHexString = function () {
|
||||
var array = new Uint8Array(this);
|
||||
var hex = '';
|
||||
for (var i = 0; i < array.length; ++i) {
|
||||
var c = array[i].toString('16');
|
||||
hex += c.length === 1 ? '0' + c : c;
|
||||
}
|
||||
return hex;
|
||||
};
|
||||
|
||||
var testCases = {
|
||||
md5_hmac: {
|
||||
'Test Vectors': {
|
||||
'9294727a3638bb1c13f48ef8158bfc9d': [
|
||||
[0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b],
|
||||
'Hi There'
|
||||
],
|
||||
'750c783e6ab0b503eaa86e310a5db738': [
|
||||
'Jefe',
|
||||
'what do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a, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa],
|
||||
'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'
|
||||
]
|
||||
},
|
||||
'UTF8': {
|
||||
'05fe3b294344f4e93c811e10f9825a38': ['中文', '中文'],
|
||||
'c1bc6df91f38e6332b8562324005103d': ['aécio', 'aécio'],
|
||||
'14eef78533c35078177d11fb4593a19e': ['𠜎', '𠜎']
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (!(typeof JS_MD5_NO_ARRAY_BUFFER === 'boolean' && JS_MD5_NO_ARRAY_BUFFER)) {
|
||||
testCases.md5_hmac.Uint8Array = {
|
||||
'72c33c78cac0b7a581ac263a344ed01d': [
|
||||
new Uint8Array(0),
|
||||
'Hi There'
|
||||
]
|
||||
};
|
||||
testCases.md5_hmac.ArrayBuffer = {
|
||||
'72c33c78cac0b7a581ac263a344ed01d': [
|
||||
new ArrayBuffer(0),
|
||||
'Hi There'
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
var errorTestCases = [null, undefined, { length: 0 }, 0, 1, false, true, NaN, Infinity, function () {}];
|
||||
|
||||
function runTestCases(name, algorithm) {
|
||||
var methods = [
|
||||
{
|
||||
name: name,
|
||||
call: algorithm,
|
||||
},
|
||||
{
|
||||
name: name + '.hex',
|
||||
call: algorithm.hex
|
||||
},
|
||||
{
|
||||
name: name + '.array',
|
||||
call: function (key, message) {
|
||||
return algorithm.array(key, message).toHexString();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: name + '.digest',
|
||||
call: function (key, message) {
|
||||
return algorithm.digest(key, message).toHexString();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: name + '.arrayBuffer',
|
||||
call: function (key, message) {
|
||||
return algorithm.arrayBuffer(key, message).toHexString();
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
var classMethods = [
|
||||
{
|
||||
name: 'create',
|
||||
call: function (key, message) {
|
||||
return algorithm.create(key).update(message).toString();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'update',
|
||||
call: function (key, message) {
|
||||
return algorithm.update(key, message).toString();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'hex',
|
||||
call: function (key, message) {
|
||||
return algorithm.update(key, message).hex();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'array',
|
||||
call: function (key, message) {
|
||||
return algorithm.update(key, message).array().toHexString();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'digest',
|
||||
call: function (key, message) {
|
||||
return algorithm.update(key, message).digest().toHexString();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'arrayBuffer',
|
||||
call: function (key, message) {
|
||||
return algorithm.update(key, message).arrayBuffer().toHexString();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'finalize',
|
||||
call: function (key, message) {
|
||||
var hash = algorithm.update(key, message);
|
||||
hash.hex();
|
||||
return hash.hex();
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
var subTestCases = testCases[name];
|
||||
|
||||
describe(name, function () {
|
||||
methods.forEach(function (method) {
|
||||
describe('#' + method.name, function () {
|
||||
for (var testCaseName in subTestCases) {
|
||||
(function (testCaseName) {
|
||||
var testCase = subTestCases[testCaseName];
|
||||
context('when ' + testCaseName, function () {
|
||||
for (var hash in testCase) {
|
||||
(function (message, hash) {
|
||||
it('should be equal', function () {
|
||||
expect(method.call(message[0], message[1])).to.be(hash);
|
||||
});
|
||||
})(testCase[hash], hash);
|
||||
}
|
||||
});
|
||||
})(testCaseName);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
classMethods.forEach(function (method) {
|
||||
describe('#' + method.name, function () {
|
||||
for (var testCaseName in subTestCases) {
|
||||
(function (testCaseName) {
|
||||
var testCase = subTestCases[testCaseName];
|
||||
context('when ' + testCaseName, function () {
|
||||
for (var hash in testCase) {
|
||||
(function (message, hash) {
|
||||
it('should be equal', function () {
|
||||
expect(method.call(message[0], message[1])).to.be(hash);
|
||||
});
|
||||
})(testCase[hash], hash);
|
||||
}
|
||||
});
|
||||
})(testCaseName);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('#' + name, function () {
|
||||
errorTestCases.forEach(function (testCase) {
|
||||
context('when ' + testCase, function () {
|
||||
it('should throw error', function () {
|
||||
expect(function () {
|
||||
algorithm(testCase, '');
|
||||
}).to.throwError(/input is invalid type/);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
runTestCases('md5_hmac', md5.hmac);
|
||||
})(md5);
|
Loading…
Reference in New Issue