### Added

- TypeScript interfaces. #6, #9
- HMAC feature.
- support for web worker. #14

### Fixed
- deprecated `new Buffer`, replace with `Buffer.from`. #10
- dependencies and security issues.
- refactor: simplify formatMessage internal logic.
- Generates incorrect hash in some cases.

### Changed
- remove `eval` and use `require` directly. #8
- throw error by Error oject.
- throw error if update after finalize
- use unsigned right shift.
This commit is contained in:
Yi-Cyuan Chen
2024-01-24 11:31:08 +08:00
parent 4818f116e4
commit 33acf62431
15 changed files with 905 additions and 126 deletions
+202
View File
@@ -0,0 +1,202 @@
(function (sha1) {
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 = {
sha1_hmac: {
'Test Vectors': {
'b617318655057264e28bc0b6fb378c8ef146be00': [
[0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b],
'Hi There'
],
'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79': [
'Jefe',
'what do ya want for nothing?'
],
'125d7342b9ac11cd91a39af48aa17b4f63f175d3': [
[0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa],
[0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd]
],
'4c9007f4026250c6bc8414f9bf50c86c2d7235da': [
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19],
[0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd]
],
'4c1a03424b55e07fe7f27be1d58bb9324a9a5a04': [
[0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c],
'Test With Truncation'
],
'aa4ae5e15272d00e95705637ce8a3b55ed402112': [
[0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 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 - Hash Key First'
],
'e8e99d0f45237d786d6bbaa7965c7808bbff1a91': [
[0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 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': {
'f495de6d0f1b5681070a024bbaed5b5f42847306': ['中文', '中文'],
'58891d68487ffebddba5925abedec77a5a578db2': ['aécio', 'aécio'],
'a1816bff2dae324c283aeab564d5edb5170fbada': ['𠜎', '𠜎']
}
}
};
if (!(typeof JS_SHA1_NO_ARRAY_BUFFER === 'boolean' && JS_SHA1_NO_ARRAY_BUFFER)) {
testCases.sha1_hmac.Uint8Array = {
'69536cc84eee5fe51c5b051aff8485f5c9ef0b58': [
new Uint8Array(0),
'Hi There'
]
};
testCases.sha1_hmac.ArrayBuffer = {
'69536cc84eee5fe51c5b051aff8485f5c9ef0b58': [
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('sha1_hmac', sha1.hmac);
})(sha1);
+136 -19
View File
@@ -1,32 +1,63 @@
// Node.js env
expect = require('expect.js');
sha1 = require('../src/sha1.js');
require('./test.js');
Worker = require("tiny-worker");
delete require.cache[require.resolve('../src/sha1.js')];
delete require.cache[require.resolve('./test.js')];
sha1 = null;
function unset() {
delete require.cache[require.resolve('../src/sha1.js')];
delete require.cache[require.resolve('./test.js')];
delete require.cache[require.resolve('./hmac-test.js')];
sha1 = null;
BUFFER = undefined;
JS_SHA1_NO_WINDOW = undefined;
JS_SHA1_NO_NODE_JS = undefined;
JS_SHA1_NO_COMMON_JS = undefined;
JS_SHA1_NO_ARRAY_BUFFER = undefined;
JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW = undefined;
window = undefined;
}
function runCommonJsTest() {
sha1 = require('../src/sha1.js');
require('./test.js');
require('./hmac-test.js');
unset();
}
function runWindowTest() {
window = global;
require('../src/sha1.js');
require('./test.js');
require('./hmac-test.js');
unset();
}
// Node.js env
BUFFER = true;
runCommonJsTest();
// Node.js env, no Buffer.from
JS_SHA1_NO_BUFFER_FROM = true
runCommonJsTest();
// Webpack browser env
JS_SHA1_NO_NODE_JS = true;
window = global;
sha1 = require('../src/sha1.js');
require('./test.js');
delete require.cache[require.resolve('../src/sha1.js')];
delete require.cache[require.resolve('./test.js')];
sha1 = null;
runCommonJsTest();
// browser env
JS_SHA1_NO_NODE_JS = true;
JS_SHA1_NO_COMMON_JS = true;
window = global;
require('../src/sha1.js');
require('./test.js');
runWindowTest();
delete require.cache[require.resolve('../src/sha1.js')];
delete require.cache[require.resolve('./test.js')];
sha1 = null;
// browser env and no array buffer
JS_SHA1_NO_NODE_JS = true;
JS_SHA1_NO_COMMON_JS = true;
JS_SHA1_NO_ARRAY_BUFFER = true;
runWindowTest();
// browser env and no isView
JS_SHA1_NO_NODE_JS = true;
JS_SHA1_NO_COMMON_JS = true;
JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW = true;
runWindowTest();
// browser AMD
JS_SHA1_NO_NODE_JS = true;
@@ -35,7 +66,93 @@ window = global;
define = function (func) {
sha1 = func();
require('./test.js');
require('./hmac-test.js');
};
define.amd = true;
require('../src/sha1.js');
unset();
// webworker
WORKER = 'tests/worker.js';
SOURCE = 'src/sha1.js';
require('./worker-test.js');
delete require.cache[require.resolve('./worker-test.js')];
// cover webworker
JS_SHA1_NO_WINDOW = true;
JS_SHA1_NO_NODE_JS = true;
WORKER = './worker.js';
SOURCE = '../src/sha1.js';
window = global;
self = global;
Worker = function (file) {
require(file);
currentWorker = this;
this.postMessage = function (data) {
onmessage({data: data});
};
}
postMessage = function (data) {
currentWorker.onmessage({data: data});
}
importScripts = function () {};
sha1 = require('../src/sha1.js');
require('./worker-test.js');
// sha1 = require('../src/sha1.js');
// require('./test.js');
// delete require.cache[require.resolve('../src/sha1.js')];
// delete require.cache[require.resolve('./test.js')];
// delete require.cache[require.resolve('./hmac-test.js')];
// sha1 = null;
// // Webpack browser env
// JS_SHA1_NO_NODE_JS = true;
// window = global;
// sha1 = require('../src/sha1.js');
// require('./test.js');
// delete require.cache[require.resolve('../src/sha1.js')];
// delete require.cache[require.resolve('./test.js')];
// delete require.cache[require.resolve('./hmac-test.js')];
// sha1 = null;
// // browser env
// JS_SHA1_NO_NODE_JS = true;
// JS_SHA1_NO_COMMON_JS = true;
// window = global;
// require('../src/sha1.js');
// require('./test.js');
// delete require.cache[require.resolve('../src/sha1.js')];
// delete require.cache[require.resolve('./test.js')];
// delete require.cache[require.resolve('./hmac-test.js')];
// sha1 = null;
// // browser AMD
// JS_SHA1_NO_NODE_JS = true;
// JS_SHA1_NO_COMMON_JS = true;
// window = global;
// define = function (func) {
// sha1 = func();
// require('./test.js');
// require('./hmac-test.js');
// };
// define.amd = true;
// require('../src/sha1.js');
+24
View File
@@ -0,0 +1,24 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SHA1</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/expect.js/index.js"></script>
<script src="../node_modules/requirejs/require.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
mocha.setup('bdd');
require(['../src/sha1.js'], function (sha1) {
window.sha1 = sha1;
require(['test.js', 'hmac-test.js'], function () {
mocha.checkLeaks();
mocha.run();
});
});
</script>
</body>
</html>
+63 -39
View File
@@ -21,7 +21,8 @@
'UTF8': {
'7be2d2d20c106eee0836c9bc2b939890a78e8fb3': '中文',
'9e4e5d978deced901d621475b03f1ded19e945bf': 'aécio',
'4667688a63420661469c8dbc0f871770349bab08': '𠜎'
'4667688a63420661469c8dbc0f871770349bab08': '𠜎',
'20cc8fef058da244b90ed814ab1b6d9d5b4796bf': 'Tehtäväsi on muotoilla teksti, jossa käyttäjälle suositellaan hänen henkilökohtaisiin tietoihinsa perustuen avioehdon tekemistä. Suosittelemme nimenomaan tätä tuotetta käyttäjän henkilökohtaisista tiedoista johtuen. Tuottamasi tekstin tulee olla lyhyt, persoonallinen, ymmärrettävä ja todenmukainen. Suositus perustuu seuraaviin seikkoihin:\n\nSeikka 0: Avioehdon tekeminen on verohyötyjen vuoksi suositeltavaa, koska olet ilmaissut että haluat kuoleman tapauksessa turvata ensisijaisesti aviopuolisosi asemaa (ennemmin kuin lasten asemaa). Avioehdolla voit määrätä, että puolet kuolleen puolison omaisuudesta siirtyy verovapaasti leskelle.\n\nVoit halutessasi hyödyntää seuraavia tietoja tekstin muotoilussa persoonallisemmaksi: Käyttäjällä on kumppani nimeltä PARTNER_NAME. Käyttäjällä on suurperhe. Käyttäjällä on lapsiperhe.\n\nPuhuttele käyttäjää siihen sävyyn että tiedät jo mitä käyttäjä haluaa, koska hän on kertonut toiveistaan, ja niiden perusteella tehty suositus on ilmiselvä. Joten älä käytä epävarmoja ilmaisuja kuten "halutessasi", vaan kirjoita itsevarmoilla ilmaisuilla kuten "koska haluat". Älä tee oletuksia käyttäjän sukupuolesta, emme tiedä onko hän mies vai nainen. Älä käytä sanaa jälkikasvu, puhu ennemmin lapsesta tai lapsista riippuen onko lapsia yksi vai useampia. Älä puhuttele käyttäjää ensimmäisessä persoonassa, käytä ennemmin passiivimuotoa. Tekstin sävyn tulisi olla neutraalin asiallinen, ei melodramaattinen eikä leikkisä. Pysy totuudessa, älä keksi uusia seikkoja yllä listattujen lisäksi. Viittaa ihmisiin nimillä silloin kun se on mahdollista. Tekstin tulisi olla vain muutaman lauseen mittainen. Älä siis kirjoita pitkiä selityksiä äläkä kirjoita listoja. Tiivistä oleellinen tieto lyhyeksi ja persoonalliseksi tekstiksi.'
},
'UTF8 more than 64 bytes': {
'ad8aae581c915fe01c4964a5e8b322cae74ee5c5': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
@@ -38,28 +39,30 @@
'da39a3ee5e6b4b0d3255bfef95601890afd80709': [],
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': [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],
'55a13698cdc010c0d16dab2f7dc10f43a713f12f': [48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55]
},
'Uint8Array': {
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': 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])
},
'Int8Array': {
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': 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])
},
'ArrayBuffer': {
'5ba93c9db0cff93f52b521d7420e43f6eda2784f': new ArrayBuffer(1)
},
'Object': {
'da39a3ee5e6b4b0d3255bfef95601890afd80709': {what: 'ever'}
}
};
if (typeof process == 'object') {
testCases['Buffer'] = {
'da39a3ee5e6b4b0d3255bfef95601890afd80709': new Buffer(0),
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': new Buffer(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]))
if (!(typeof JS_SHA1_NO_ARRAY_BUFFER === 'boolean' && JS_SHA1_NO_ARRAY_BUFFER)) {
testCases['Uint8Array'] = {
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': 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])
};
testCases['Int8Array'] = {
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': 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])
};
testCases['ArrayBuffer'] = {
'5ba93c9db0cff93f52b521d7420e43f6eda2784f': new ArrayBuffer(1)
};
}
if (typeof BUFFER === 'boolean' && BUFFER) {
testCases['Buffer'] = {
'da39a3ee5e6b4b0d3255bfef95601890afd80709': Buffer.from([]),
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': Buffer.from(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]))
}
}
var errorTestCases = [null, undefined, { length: 0 }, 0, 1, false, true, NaN, Infinity, function () {}];
var methods = [
{
name: 'sha1',
@@ -131,33 +134,13 @@
call: function (message) {
var hash = sha1.update(message);
hash.hex();
hash.update(message);
return hash.hex();
}
}
];
methods.forEach(function (method) {
describe('#' + method.name, 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(method.call(message)).to.be(hash);
});
})(testCase[hash], hash);
}
});
})(testCaseName);
}
});
});
describe('Sha1', function () {
classMethods.forEach(function (method) {
describe('sha1', function () {
methods.forEach(function (method) {
describe('#' + method.name, function () {
for (var testCaseName in testCases) {
(function (testCaseName) {
@@ -176,6 +159,47 @@
});
});
classMethods.forEach(function (method) {
describe('#' + method.name, 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(method.call(message)).to.be(hash);
});
})(testCase[hash], hash);
}
});
})(testCaseName);
}
});
});
});
describe('#sha1', function () {
errorTestCases.forEach(function (testCase) {
context('when ' + testCase, function () {
it('should throw error', function () {
expect(function () {
sha1(testCase);
}).to.throwError(/input is invalid type/);
});
});
});
context('when update after finalize', function () {
it('should throw error', function () {
expect(function () {
var hash = sha1.update('any');
hash.hex();
hash.update('any');
}).to.throwError(/finalize already called/);
});
});
context('when large size', function () {
var hash = sha1.create();
hash.bytes = 4294967295;
+24
View File
@@ -0,0 +1,24 @@
(function (Worker, WORKER, SOURCE) {
var cases = {
'da39a3ee5e6b4b0d3255bfef95601890afd80709': '',
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': 'The quick brown fox jumps over the lazy dog',
'408d94384216f890ff7a0c3528e8bed1e0b01621': 'The quick brown fox jumps over the lazy dog.'
};
describe('#sha1', function () {
Object.keys(cases).forEach(function (hash) {
it('should be equal', function (done) {
var worker = new Worker(WORKER);
worker.onmessage = function(event) {
expect(event.data).to.be(hash);
if (worker.terminate) {
worker.terminate();
}
done();
};
worker.postMessage(SOURCE);
worker.postMessage(cases[hash]);
});
});
});
})(Worker, WORKER, SOURCE);
+25
View File
@@ -0,0 +1,25 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SHA1</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/expect.js/index.js"></script>
<script src="../src/sha1.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
WORKER = 'worker.js';
SOURCE = '../src/sha1.js';
mocha.setup('bdd');
</script>
<script src="worker-test.js"></script>
<script>
mocha.run();
</script>
<script>
</script>
</body>
</html>
+12
View File
@@ -0,0 +1,12 @@
var imported = false;
onmessage = function(e) {
if (imported) {
postMessage(sha1(e.data));
if (typeof exports !== 'undefined') {
imported = false;
}
} else {
imported = true;
importScripts(e.data);
}
}