mirror of
https://github.com/emn178/js-sha256.git
synced 2026-08-12 20:32:16 +08:00
### Added
- Add dedicated ESM, CommonJS, Node.js, and browser distribution entry points. - Add streaming support to the native Node.js SHA-256, SHA-224, and HMAC implementations. - Add distribution contract tests and real browser Web Worker tests with Playwright. - Add Node.js 16, 18, 20, 22, and 24 runtime tests against the packed npm artifact. ### Changed - Refactor the implementation into environment-independent core and Node.js modules. - Use the native Node.js `crypto` implementation for hashing and HMAC. - Load the package version automatically when generating distribution banners. - Export unminified ESM source directly and build only CommonJS and browser/minified distributions. - Replace `expect.js`, NYC, Tiny Worker, and UglifyJS with Node.js assert, c8, Playwright, and Rollup/Terser. - Build automatically before `npm pack` and `npm publish`. - Throw error if update after finalize ### Removed - Remove runtime CommonJS, AMD, Node.js, Web Worker, and ArrayBuffer environment switches from the core implementation. - Remove legacy environment flags and obsolete compatibility polyfills. - Remove the old generated `sha256.mjs` and `sha256.node.mjs` entry points.
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
importScripts('/build/sha256.min.js');
|
||||
|
||||
self.onmessage = function (event) {
|
||||
self.postMessage(sha256(event.data));
|
||||
};
|
||||
@@ -0,0 +1,5 @@
|
||||
import { sha256 } from '../../src/index.mjs';
|
||||
|
||||
self.onmessage = function (event) {
|
||||
self.postMessage(sha256(event.data));
|
||||
};
|
||||
@@ -0,0 +1,95 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
import { createReadStream } from 'node:fs';
|
||||
import { stat } from 'node:fs/promises';
|
||||
import { createServer } from 'node:http';
|
||||
import { extname, join, normalize } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const root = fileURLToPath(new URL('../..', import.meta.url));
|
||||
const contentTypes = {
|
||||
'.js': 'text/javascript',
|
||||
'.mjs': 'text/javascript'
|
||||
};
|
||||
|
||||
let server;
|
||||
let origin;
|
||||
|
||||
test.beforeAll(async () => {
|
||||
server = createServer(async (request, response) => {
|
||||
if (request.url === '/') {
|
||||
response.writeHead(200, { 'Content-Type': 'text/html' });
|
||||
response.end('<!doctype html><title>js-sha256 worker test</title>');
|
||||
return;
|
||||
}
|
||||
|
||||
const path = normalize(join(root, decodeURIComponent(request.url)));
|
||||
if (!path.startsWith(root)) {
|
||||
response.writeHead(403);
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await stat(path);
|
||||
response.writeHead(200, {
|
||||
'Content-Type': contentTypes[extname(path)] || 'application/octet-stream'
|
||||
});
|
||||
createReadStream(path).pipe(response);
|
||||
} catch {
|
||||
response.writeHead(404);
|
||||
response.end();
|
||||
}
|
||||
});
|
||||
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
const address = server.address();
|
||||
origin = `http://127.0.0.1:${address.port}`;
|
||||
});
|
||||
|
||||
test.afterAll(async () => {
|
||||
await new Promise((resolve, reject) => {
|
||||
server.close(error => error ? reject(error) : resolve());
|
||||
});
|
||||
});
|
||||
|
||||
async function hashInWorker(page, workerPath, options) {
|
||||
return page.evaluate(({ workerPath, options }) => new Promise((resolve, reject) => {
|
||||
const worker = new Worker(workerPath, options);
|
||||
const timeout = setTimeout(() => {
|
||||
worker.terminate();
|
||||
reject(new Error('Worker timed out'));
|
||||
}, 5000);
|
||||
|
||||
worker.onmessage = event => {
|
||||
clearTimeout(timeout);
|
||||
worker.terminate();
|
||||
resolve(event.data);
|
||||
};
|
||||
worker.onerror = event => {
|
||||
clearTimeout(timeout);
|
||||
worker.terminate();
|
||||
reject(new Error(event.message));
|
||||
};
|
||||
worker.postMessage('abc');
|
||||
}), { workerPath, options });
|
||||
}
|
||||
|
||||
test('classic Worker loads the minified UMD build', async ({ page }) => {
|
||||
await page.goto(origin);
|
||||
const hash = await hashInWorker(page, '/tests/browser/classic-worker.js');
|
||||
expect(hash).toBe(
|
||||
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
|
||||
);
|
||||
});
|
||||
|
||||
test('module Worker imports the ESM source entry', async ({ page }) => {
|
||||
await page.goto(origin);
|
||||
const hash = await hashInWorker(
|
||||
page,
|
||||
'/tests/browser/module-worker.mjs',
|
||||
{ type: 'module' }
|
||||
);
|
||||
expect(hash).toBe(
|
||||
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { sha224, sha256 } from '../src/core.mjs';
|
||||
|
||||
globalThis.assert = assert;
|
||||
globalThis.sha256 = sha256;
|
||||
globalThis.sha224 = sha224;
|
||||
globalThis.TEST_INTERNALS = true;
|
||||
|
||||
await import('./test.js');
|
||||
await import('./hmac-test.js');
|
||||
@@ -0,0 +1,87 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { createRequire } from 'node:module';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import vm from 'node:vm';
|
||||
import packageSha256, {
|
||||
sha224 as packageSha224,
|
||||
sha256 as namedPackageSha256
|
||||
} from 'js-sha256';
|
||||
import browserSha256, {
|
||||
sha224 as browserSha224
|
||||
} from '../src/index.mjs';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const commonJs = require('../build/sha256.cjs');
|
||||
const nodeCommonJs = require('js-sha256');
|
||||
|
||||
const SHA256_ABC = 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad';
|
||||
const SHA224_ABC = '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7';
|
||||
const HMAC_ABC = '9c196e32dc0175f86f4b1cb89289d6619de6bee699e4c378e68309ed97a1a6ab';
|
||||
|
||||
function testContract(name, sha256, sha224) {
|
||||
assert.equal(sha256('abc'), SHA256_ABC, `${name}: SHA-256`);
|
||||
assert.equal(sha224('abc'), SHA224_ABC, `${name}: SHA-224`);
|
||||
assert.equal(sha256.hmac('key', 'abc'), HMAC_ABC, `${name}: HMAC`);
|
||||
assert.equal(sha256.update('a').update('bc').hex(), SHA256_ABC, `${name}: streaming`);
|
||||
assert.equal(sha256([97, 98, 99]), SHA256_ABC, `${name}: array`);
|
||||
assert.equal(sha256(new Uint8Array([97, 98, 99])), SHA256_ABC, `${name}: typed array`);
|
||||
assert.equal(sha256.sha256, sha256, `${name}: sha256 property`);
|
||||
assert.equal(sha256.sha224, sha224, `${name}: sha224 property`);
|
||||
}
|
||||
|
||||
function testStreamingOutputs(name, algorithm, expected) {
|
||||
const hasher = algorithm.create().update('a').update('bc');
|
||||
assert.equal(hasher.hex(), expected, `${name}: hex`);
|
||||
assert.equal(hasher.toString(), expected, `${name}: toString`);
|
||||
assert.deepEqual(hasher.array(), Array.from(Buffer.from(expected, 'hex')), `${name}: array`);
|
||||
assert.deepEqual(hasher.digest(), hasher.array(), `${name}: digest`);
|
||||
assert.equal(
|
||||
Buffer.from(hasher.arrayBuffer()).toString('hex'),
|
||||
expected,
|
||||
`${name}: arrayBuffer`
|
||||
);
|
||||
assert.equal(hasher.hex(), expected, `${name}: repeated output`);
|
||||
}
|
||||
|
||||
function testHmacStreamingOutputs(name, hmac) {
|
||||
const hasher = hmac.create('key').update('a').update('bc');
|
||||
assert.equal(hasher.hex(), HMAC_ABC, `${name}: hex`);
|
||||
assert.equal(hasher.toString(), HMAC_ABC, `${name}: toString`);
|
||||
assert.deepEqual(hasher.array(), Array.from(Buffer.from(HMAC_ABC, 'hex')), `${name}: array`);
|
||||
assert.deepEqual(hasher.digest(), hasher.array(), `${name}: digest`);
|
||||
assert.equal(
|
||||
Buffer.from(hasher.arrayBuffer()).toString('hex'),
|
||||
HMAC_ABC,
|
||||
`${name}: arrayBuffer`
|
||||
);
|
||||
assert.equal(hasher.hex(), HMAC_ABC, `${name}: repeated output`);
|
||||
}
|
||||
|
||||
testContract('package ESM', packageSha256, packageSha224);
|
||||
assert.equal(namedPackageSha256, packageSha256);
|
||||
testStreamingOutputs('package ESM SHA-256 streaming', packageSha256, SHA256_ABC);
|
||||
testStreamingOutputs('package ESM SHA-224 streaming', packageSha224, SHA224_ABC);
|
||||
testHmacStreamingOutputs('package ESM HMAC streaming', packageSha256.hmac);
|
||||
testContract('browser ESM', browserSha256, browserSha224);
|
||||
testContract('generic CommonJS', commonJs, commonJs.sha224);
|
||||
testContract('package CommonJS', nodeCommonJs, nodeCommonJs.sha224);
|
||||
testStreamingOutputs('package CommonJS SHA-256 streaming', nodeCommonJs, SHA256_ABC);
|
||||
testStreamingOutputs('package CommonJS SHA-224 streaming', nodeCommonJs.sha224, SHA224_ABC);
|
||||
testHmacStreamingOutputs('package CommonJS HMAC streaming', nodeCommonJs.hmac);
|
||||
|
||||
const umdCode = await readFile(new URL('../build/sha256.min.js', import.meta.url), 'utf8');
|
||||
const browserContext = vm.createContext({});
|
||||
vm.runInContext(umdCode, browserContext);
|
||||
testContract('browser UMD', browserContext.sha256, browserContext.sha224);
|
||||
|
||||
let amdSha256;
|
||||
const amdContext = vm.createContext({
|
||||
define(factory) {
|
||||
amdSha256 = factory();
|
||||
}
|
||||
});
|
||||
amdContext.define.amd = true;
|
||||
vm.runInContext(umdCode, amdContext);
|
||||
testContract('AMD', amdSha256, amdSha256.sha224);
|
||||
|
||||
console.log('Distribution contracts: PASS');
|
||||
@@ -1,20 +0,0 @@
|
||||
import sha256, { sha224, sha256 as namedSha256 } from 'js-sha256';
|
||||
import browserSha256, { sha224 as browserSha224 } from 'js-sha256/build/sha256.mjs';
|
||||
|
||||
const SHA256_ABC = 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad';
|
||||
const SHA224_ABC = '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7';
|
||||
|
||||
if (sha256('abc') !== SHA256_ABC || namedSha256('abc') !== SHA256_ABC) {
|
||||
throw new Error('ESM SHA-256 import failed');
|
||||
}
|
||||
if (sha224('abc') !== SHA224_ABC) {
|
||||
throw new Error('ESM SHA-224 import failed');
|
||||
}
|
||||
if (typeof sha256.hmac !== 'function' || typeof sha224.hmac !== 'function') {
|
||||
throw new Error('ESM HMAC API is missing');
|
||||
}
|
||||
if (browserSha256('abc') !== SHA256_ABC || browserSha224('abc') !== SHA224_ABC) {
|
||||
throw new Error('Browser ESM import failed');
|
||||
}
|
||||
|
||||
console.log('ESM imports: PASS');
|
||||
+28
-31
@@ -78,32 +78,30 @@
|
||||
}
|
||||
};
|
||||
|
||||
if (!(typeof JS_SHA256_NO_ARRAY_BUFFER === 'boolean' && JS_SHA256_NO_ARRAY_BUFFER)) {
|
||||
testCases.sha256_hmac.Uint8Array = {
|
||||
'e48411262715c8370cd5e7bf8e82bef53bd53712d007f3429351843b77c7bb9b': [
|
||||
new Uint8Array(0),
|
||||
'Hi There'
|
||||
]
|
||||
};
|
||||
testCases.sha256_hmac.ArrayBuffer = {
|
||||
'e48411262715c8370cd5e7bf8e82bef53bd53712d007f3429351843b77c7bb9b': [
|
||||
new ArrayBuffer(0),
|
||||
'Hi There'
|
||||
]
|
||||
};
|
||||
testCases.sha224_hmac.Uint8Array = {
|
||||
'da8f94de91d62154b55ea4e8d6eb133f6d553bcd1f1ba205b9488945': [
|
||||
new ArrayBuffer(0),
|
||||
'Hi There'
|
||||
]
|
||||
};
|
||||
testCases.sha224_hmac.ArrayBuffer = {
|
||||
'da8f94de91d62154b55ea4e8d6eb133f6d553bcd1f1ba205b9488945': [
|
||||
new ArrayBuffer(0),
|
||||
'Hi There'
|
||||
]
|
||||
};
|
||||
}
|
||||
testCases.sha256_hmac.Uint8Array = {
|
||||
'e48411262715c8370cd5e7bf8e82bef53bd53712d007f3429351843b77c7bb9b': [
|
||||
new Uint8Array(0),
|
||||
'Hi There'
|
||||
]
|
||||
};
|
||||
testCases.sha256_hmac.ArrayBuffer = {
|
||||
'e48411262715c8370cd5e7bf8e82bef53bd53712d007f3429351843b77c7bb9b': [
|
||||
new ArrayBuffer(0),
|
||||
'Hi There'
|
||||
]
|
||||
};
|
||||
testCases.sha224_hmac.Uint8Array = {
|
||||
'da8f94de91d62154b55ea4e8d6eb133f6d553bcd1f1ba205b9488945': [
|
||||
new ArrayBuffer(0),
|
||||
'Hi There'
|
||||
]
|
||||
};
|
||||
testCases.sha224_hmac.ArrayBuffer = {
|
||||
'da8f94de91d62154b55ea4e8d6eb133f6d553bcd1f1ba205b9488945': [
|
||||
new ArrayBuffer(0),
|
||||
'Hi There'
|
||||
]
|
||||
};
|
||||
|
||||
var errorTestCases = [null, undefined, { length: 0 }, 0, 1, false, true, NaN, Infinity, function () {}];
|
||||
|
||||
@@ -179,7 +177,6 @@
|
||||
call: function (key, message) {
|
||||
var hash = algorithm.update(key, message);
|
||||
hash.hex();
|
||||
hash.update(message);
|
||||
return hash.hex();
|
||||
}
|
||||
}
|
||||
@@ -197,7 +194,7 @@
|
||||
for (var hash in testCase) {
|
||||
(function (message, hash) {
|
||||
it('should be equal', function () {
|
||||
expect(method.call(message[0], message[1])).to.be(hash);
|
||||
assert.equal(method.call(message[0], message[1]), hash);
|
||||
});
|
||||
})(testCase[hash], hash);
|
||||
}
|
||||
@@ -216,7 +213,7 @@
|
||||
for (var hash in testCase) {
|
||||
(function (message, hash) {
|
||||
it('should be equal', function () {
|
||||
expect(method.call(message[0], message[1])).to.be(hash);
|
||||
assert.equal(method.call(message[0], message[1]), hash);
|
||||
});
|
||||
})(testCase[hash], hash);
|
||||
}
|
||||
@@ -230,9 +227,9 @@
|
||||
errorTestCases.forEach(function (testCase) {
|
||||
context('when ' + testCase, function () {
|
||||
it('should throw error', function () {
|
||||
expect(function () {
|
||||
assert.throws(function () {
|
||||
algorithm(testCase, '');
|
||||
}).to.throwError(/input is invalid type/);
|
||||
}, /input is invalid type/);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>SHA256</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="../src/sha256.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
<script>
|
||||
mocha.setup('bdd');
|
||||
</script>
|
||||
<script src="test.js"></script>
|
||||
<script src="hmac-test.js"></script>
|
||||
<script>
|
||||
mocha.checkLeaks();
|
||||
mocha.run();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,137 +0,0 @@
|
||||
expect = require('expect.js');
|
||||
Worker = require("tiny-worker");
|
||||
|
||||
function unset() {
|
||||
delete require.cache[require.resolve('../src/sha256.js')];
|
||||
delete require.cache[require.resolve('./test.js')];
|
||||
delete require.cache[require.resolve('./hmac-test.js')];
|
||||
sha256 = null;
|
||||
sha224 = null;
|
||||
BUFFER = undefined;
|
||||
JS_SHA256_NO_WINDOW = undefined;
|
||||
JS_SHA256_NO_NODE_JS = undefined;
|
||||
JS_SHA256_NO_COMMON_JS = undefined;
|
||||
JS_SHA256_NO_ARRAY_BUFFER = undefined;
|
||||
JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW = undefined;
|
||||
window = undefined;
|
||||
}
|
||||
|
||||
function requireToGlobal() {
|
||||
sha256 = require('../src/sha256.js').sha256;
|
||||
sha224 = require('../src/sha256.js').sha224;
|
||||
}
|
||||
|
||||
function runCommonJsTest() {
|
||||
requireToGlobal();
|
||||
require('./test.js');
|
||||
require('./hmac-test.js');
|
||||
unset();
|
||||
}
|
||||
|
||||
function runWindowTest() {
|
||||
window = global;
|
||||
require('../src/sha256.js');
|
||||
require('./test.js');
|
||||
require('./hmac-test.js');
|
||||
unset();
|
||||
}
|
||||
|
||||
function runBrowserWithProcessPolyfillTest() {
|
||||
var Module = require('module');
|
||||
var originalRequire = Module.prototype.require;
|
||||
window = global;
|
||||
Module.prototype.require = function (id) {
|
||||
if (id === 'crypto' || id === 'buffer') {
|
||||
throw new Error('Node.js module loaded in browser environment');
|
||||
}
|
||||
return originalRequire.apply(this, arguments);
|
||||
};
|
||||
try {
|
||||
runCommonJsTest();
|
||||
} finally {
|
||||
Module.prototype.require = originalRequire;
|
||||
}
|
||||
}
|
||||
|
||||
// Node.js env
|
||||
BUFFER = true;
|
||||
runCommonJsTest();
|
||||
|
||||
// Node.js env, no Buffer.from
|
||||
JS_SHA256_NO_BUFFER_FROM = true
|
||||
runCommonJsTest();
|
||||
|
||||
// browser env with a Node.js process polyfill
|
||||
runBrowserWithProcessPolyfillTest();
|
||||
|
||||
// Webpack browser env
|
||||
JS_SHA256_NO_NODE_JS = true;
|
||||
window = global;
|
||||
runCommonJsTest();
|
||||
|
||||
// browser env
|
||||
JS_SHA256_NO_NODE_JS = true;
|
||||
JS_SHA256_NO_COMMON_JS = true;
|
||||
runWindowTest();
|
||||
|
||||
// browser env and no array buffer
|
||||
JS_SHA256_NO_NODE_JS = true;
|
||||
JS_SHA256_NO_COMMON_JS = true;
|
||||
JS_SHA256_NO_ARRAY_BUFFER = true;
|
||||
runWindowTest();
|
||||
|
||||
// browser env and no isView
|
||||
JS_SHA256_NO_NODE_JS = true;
|
||||
JS_SHA256_NO_COMMON_JS = true;
|
||||
JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW = true;
|
||||
runWindowTest();
|
||||
|
||||
// browser AMD
|
||||
JS_SHA256_NO_NODE_JS = true;
|
||||
JS_SHA256_NO_COMMON_JS = true;
|
||||
JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW = false;
|
||||
window = global;
|
||||
define = function (func) {
|
||||
sha256 = func();
|
||||
sha224 = sha256.sha224;
|
||||
require('./test.js');
|
||||
require('./hmac-test.js');
|
||||
};
|
||||
define.amd = true;
|
||||
|
||||
require('../src/sha256.js');
|
||||
unset();
|
||||
|
||||
// webworker
|
||||
WORKER = 'tests/worker.js';
|
||||
SOURCE = 'src/sha256.js';
|
||||
|
||||
require('./worker-test.js');
|
||||
|
||||
delete require.cache[require.resolve('./worker-test.js')];
|
||||
|
||||
// cover webworker
|
||||
JS_SHA256_NO_WINDOW = true;
|
||||
JS_SHA256_NO_NODE_JS = true;
|
||||
WORKER = './worker.js';
|
||||
SOURCE = '../src/sha256.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 () {};
|
||||
|
||||
requireToGlobal();
|
||||
require('./worker-test.js');
|
||||
@@ -0,0 +1,10 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { sha224, sha256 } from 'js-sha256';
|
||||
|
||||
globalThis.assert = assert;
|
||||
globalThis.sha256 = sha256;
|
||||
globalThis.sha224 = sha224;
|
||||
globalThis.TEST_INTERNALS = false;
|
||||
|
||||
await import('./test.js');
|
||||
await import('./hmac-test.js');
|
||||
+51
-44
@@ -77,41 +77,37 @@
|
||||
}
|
||||
};
|
||||
|
||||
if (!(typeof JS_SHA256_NO_ARRAY_BUFFER === 'boolean' && JS_SHA256_NO_ARRAY_BUFFER)) {
|
||||
testCases.sha256.Uint8Array = {
|
||||
'182889f925ae4e5cc37118ded6ed87f7bdc7cab5ec5e78faef2e50048999473f': new Uint8Array([211, 212]),
|
||||
'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592': 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.sha256.Int8Array = {
|
||||
'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592': 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.sha256.ArrayBuffer = {
|
||||
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': new ArrayBuffer(0),
|
||||
'6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d': new ArrayBuffer(1)
|
||||
};
|
||||
testCases.sha224.Uint8Array = {
|
||||
'e17541396a3ecd1cd5a2b968b84e597e8eae3b0ea3127963bf48dd3b': new Uint8Array([211, 212]),
|
||||
'730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525': 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.sha224.Int8Array = {
|
||||
'730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525': 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.sha224.ArrayBuffer = {
|
||||
'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f': new ArrayBuffer(0),
|
||||
'fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073': new ArrayBuffer(1),
|
||||
};
|
||||
}
|
||||
testCases.sha256.Uint8Array = {
|
||||
'182889f925ae4e5cc37118ded6ed87f7bdc7cab5ec5e78faef2e50048999473f': new Uint8Array([211, 212]),
|
||||
'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592': 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.sha256.Int8Array = {
|
||||
'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592': 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.sha256.ArrayBuffer = {
|
||||
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': new ArrayBuffer(0),
|
||||
'6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d': new ArrayBuffer(1)
|
||||
};
|
||||
testCases.sha224.Uint8Array = {
|
||||
'e17541396a3ecd1cd5a2b968b84e597e8eae3b0ea3127963bf48dd3b': new Uint8Array([211, 212]),
|
||||
'730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525': 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.sha224.Int8Array = {
|
||||
'730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525': 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.sha224.ArrayBuffer = {
|
||||
'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f': new ArrayBuffer(0),
|
||||
'fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073': new ArrayBuffer(1),
|
||||
};
|
||||
|
||||
if (typeof BUFFER === 'boolean' && BUFFER) {
|
||||
testCases.sha256.Buffer = {
|
||||
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': Buffer.from([]),
|
||||
'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592': 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]))
|
||||
};
|
||||
testCases.sha224.Buffer = {
|
||||
'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f': Buffer.from([]),
|
||||
'730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525': 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]))
|
||||
};
|
||||
}
|
||||
testCases.sha256.Buffer = {
|
||||
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': Buffer.from([]),
|
||||
'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592': 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]))
|
||||
};
|
||||
testCases.sha224.Buffer = {
|
||||
'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f': Buffer.from([]),
|
||||
'730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525': 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 () {}];
|
||||
|
||||
@@ -187,7 +183,6 @@
|
||||
call: function (message) {
|
||||
var hash = algorithm.update(message);
|
||||
hash.hex();
|
||||
hash.update(message);
|
||||
return hash.hex();
|
||||
}
|
||||
}
|
||||
@@ -205,7 +200,7 @@
|
||||
for (var hash in testCase) {
|
||||
(function (message, hash) {
|
||||
it('should be equal', function () {
|
||||
expect(method.call(message)).to.be(hash);
|
||||
assert.equal(method.call(message), hash);
|
||||
});
|
||||
})(testCase[hash], hash);
|
||||
}
|
||||
@@ -224,7 +219,7 @@
|
||||
for (var hash in testCase) {
|
||||
(function (message, hash) {
|
||||
it('should be equal', function () {
|
||||
expect(method.call(message)).to.be(hash);
|
||||
assert.equal(method.call(message), hash);
|
||||
});
|
||||
})(testCase[hash], hash);
|
||||
}
|
||||
@@ -238,19 +233,31 @@
|
||||
errorTestCases.forEach(function (testCase) {
|
||||
context('when ' + testCase, function () {
|
||||
it('should throw error', function () {
|
||||
expect(function () {
|
||||
assert.throws(function () {
|
||||
algorithm(testCase);
|
||||
}).to.throwError(/input is invalid type/);
|
||||
}, /input is invalid type/);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('when large size', function () {
|
||||
var hash = algorithm.create();
|
||||
hash.bytes = 4294967295;
|
||||
hash.update('any');
|
||||
expect(hash.hBytes).to.be(1);
|
||||
context('when update after finalize', function () {
|
||||
it('should throw error', function () {
|
||||
assert.throws(function () {
|
||||
var hash = algorithm.update('any');
|
||||
hash.hex();
|
||||
hash.update('any');
|
||||
}, /finalize already called/);
|
||||
});
|
||||
});
|
||||
|
||||
if (TEST_INTERNALS) {
|
||||
context('when large size', function () {
|
||||
var hash = algorithm.create();
|
||||
hash.bytes = 4294967295;
|
||||
hash.update('any');
|
||||
assert.equal(hash.hBytes, 1);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
(function (Worker, WORKER, SOURCE) {
|
||||
var cases = {
|
||||
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': '',
|
||||
'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592': 'The quick brown fox jumps over the lazy dog',
|
||||
'ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c': 'The quick brown fox jumps over the lazy dog.'
|
||||
};
|
||||
|
||||
describe('#sha256', 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);
|
||||
@@ -1,26 +0,0 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>SHA256</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="../src/sha256.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
<script>
|
||||
WORKER = 'worker.js';
|
||||
SOURCE = '../src/sha256.js';
|
||||
mocha.setup('bdd');
|
||||
</script>
|
||||
<script src="worker-test.js"></script>
|
||||
<script>
|
||||
mocha.checkLeaks();
|
||||
mocha.run();
|
||||
</script>
|
||||
<script>
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,12 +0,0 @@
|
||||
var imported = false;
|
||||
onmessage = function(e) {
|
||||
if (imported) {
|
||||
postMessage(sha256(e.data));
|
||||
if (typeof exports !== 'undefined') {
|
||||
imported = false;
|
||||
}
|
||||
} else {
|
||||
imported = true;
|
||||
importScripts(e.data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user