* Added support for bytes.

* Added support for AMD.
This commit is contained in:
Yi-Cyuan
2015-12-01 13:32:46 +08:00
parent b1f1942f17
commit 9a8df5e5e4
12 changed files with 219 additions and 145 deletions
+12
View File
@@ -9,3 +9,15 @@ md4 = null
JS_MD4_TEST = true;
require('../src/md4.js');
require('./test.js');
delete require.cache[require.resolve('../src/md4.js')];
delete require.cache[require.resolve('./test.js')];
md4 = null;
define = function(func) {
md4 = func();
require('./test.js');
};
define.amd = true;
require('../src/md4.js');
+24
View File
@@ -0,0 +1,24 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MD4</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect.js/0.2.0/expect.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.17/require.min.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
mocha.setup('bdd');
require(['../src/md4.js'], function(md4) {
window.md4 = md4;
require(['test.js'], function() {
mocha.checkLeaks();
mocha.run();
})
});
</script>
</body>
</html>
+62 -42
View File
@@ -1,47 +1,67 @@
(function(md4) {
describe('md4', function() {
describe('ascii', function() {
describe('less than 64 bytes', function() {
it('should be successful', function() {
expect(md4('')).to.be('31d6cfe0d16ae931b73c59d7e0c089c0');
expect(md4('The quick brown fox jumps over the lazy dog')).to.be('1bee69a46ba811185c194762abaeae90');
expect(md4('The quick brown fox jumps over the lazy dog.')).to.be('2812c6c7136898c51f6f6739ad08750e');
});
});
var testCases = {
'ascii': {
'31d6cfe0d16ae931b73c59d7e0c089c0': '',
'1bee69a46ba811185c194762abaeae90': 'The quick brown fox jumps over the lazy dog',
'2812c6c7136898c51f6f6739ad08750e': 'The quick brown fox jumps over the lazy dog.'
},
'ascii more than 64 bytes': {
'e995876fc5a7870c478d20312edf17da': 'The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.'
},
'UTF8': {
'223088bf7bd45a16436b15360c5fc5a0': '中文',
'0b1f6347ef0be74383f7ae7547359a4c': 'aécio',
'cb17a223ccf45757d08260b6bfab78ab': '𠜎'
},
'UTF8 more than 64 bytes': {
'968bd34f00469adbddbe6d803b28cff9': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
'2e03bd374f7be036d4fa838cb9662597': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。'
},
'special length': {
'91df808c37b8c5544391a3aa2196114e': '0123456780123456780123456780123456780123456780123456780',
'3825a0afe234b8029ccad9a31ec5f8ee': '01234567801234567801234567801234567801234567801234567801',
'f9b968c94ec709be9f306d90cd424228': '0123456780123456780123456780123456780123456780123456780123456780',
'08b0ded59615dc18407569a9ceb263ba': '01234567801234567801234567801234567801234567801234567801234567801234567',
'9c637e494a39f7920c7e83b665284f03': '012345678012345678012345678012345678012345678012345678012345678012345678',
'47eebbaaa1fca842a7bff2d3b7c9f0c6': '012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678'
},
'Array': {
'31d6cfe0d16ae931b73c59d7e0c089c0': [],
'47c61a0fa8738ba77308a8a600f88e4b': [0],
'1bee69a46ba811185c194762abaeae90': [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103],
'4c832b90373e2d3f4a8ce06172989e2b': [72, 69, 76, 76, 79]
},
'Uint8Array': {
'1bee69a46ba811185c194762abaeae90': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]),
'4c832b90373e2d3f4a8ce06172989e2b': new Uint8Array([72, 69, 76, 76, 79])
},
'Int8Array': {
'1bee69a46ba811185c194762abaeae90': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]),
'4c832b90373e2d3f4a8ce06172989e2b': new Int8Array([72, 69, 76, 76, 79])
},
'ArrayBuffer': {
'47c61a0fa8738ba77308a8a600f88e4b': new ArrayBuffer(1)
},
'Object': {
'31d6cfe0d16ae931b73c59d7e0c089c0': {},
'31d6cfe0d16ae931b73c59d7e0c089c0': {what: 'ever'}
}
};
describe('more than 64 bytes', function() {
it('should be successful', function() {
expect(md4('The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.')).to.be('e995876fc5a7870c478d20312edf17da');
describe('#md4', function() {
for(var testCaseName in testCases) {
(function(testCaseName) {
var testCase = testCases[testCaseName];
context('when ' + testCaseName, function() {
for(var hash in testCase) {
(function(message, hash) {
it('should be equal', function() {
expect(md4(message)).to.be(hash);
});
})(testCase[hash], hash);
}
});
});
});
describe('UTF8', function() {
describe('less than 64 bytes', function() {
it('should be successful', function() {
expect(md4('中文')).to.be('223088bf7bd45a16436b15360c5fc5a0');
expect(md4('aécio')).to.be('0b1f6347ef0be74383f7ae7547359a4c');
expect(md4('𠜎')).to.be('cb17a223ccf45757d08260b6bfab78ab');
});
});
describe('more than 64 bytes', function() {
it('should be successful', function() {
expect(md4('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('968bd34f00469adbddbe6d803b28cff9');
expect(md4('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('2e03bd374f7be036d4fa838cb9662597');
});
});
});
describe('special length', function() {
it('should be successful', function() {
expect(md4('0123456780123456780123456780123456780123456780123456780')).to.be('91df808c37b8c5544391a3aa2196114e');
expect(md4('01234567801234567801234567801234567801234567801234567801')).to.be('3825a0afe234b8029ccad9a31ec5f8ee');
expect(md4('0123456780123456780123456780123456780123456780123456780123456780')).to.be('f9b968c94ec709be9f306d90cd424228');
expect(md4('01234567801234567801234567801234567801234567801234567801234567801234567')).to.be('08b0ded59615dc18407569a9ceb263ba');
expect(md4('012345678012345678012345678012345678012345678012345678012345678012345678')).to.be('9c637e494a39f7920c7e83b665284f03');
expect(md4('012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678')).to.be('47eebbaaa1fca842a7bff2d3b7c9f0c6');
});
});
})(testCaseName);
}
});
})(md4);