mirror of
https://github.com/emn178/js-sha1.git
synced 2026-11-17 14:18:44 +08:00
### Added
- ESM builds and package `exports`. ### Fixed - Incorrect hash for strings with unpaired UTF-16 surrogates; they are now encoded as U+FFFD, the same as `TextEncoder`. - Detect browsers with a `process` polyfill (e.g. BrowserFS) and Electron renderers as non-Node.js environments. ### Changed - Replace Travis CI with GitHub Actions. - Update dev dependencies and fix security issues. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
fd520b41fa
commit
3e1a92f2a4
@@ -0,0 +1,25 @@
|
||||
import { createRequire } from 'module';
|
||||
import expect from 'expect.js';
|
||||
import * as esm from '../build/sha1.mjs';
|
||||
import * as esmMin from '../build/sha1.min.mjs';
|
||||
import * as esmNode from '../build/sha1.node.mjs';
|
||||
import * as pkg from 'js-sha1';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
globalThis.expect = expect;
|
||||
|
||||
[['build/sha1.mjs', esm], ['build/sha1.min.mjs', esmMin], ['build/sha1.node.mjs', esmNode], ['js-sha1', pkg]].forEach(([name, mod]) => {
|
||||
describe('ESM ' + name, () => {
|
||||
it('should export the named member matching the default export', () => {
|
||||
expect(mod.sha1).to.be(mod.default);
|
||||
expect(mod.default.hmac).to.be.a('function');
|
||||
});
|
||||
});
|
||||
|
||||
globalThis.sha1 = mod.default;
|
||||
delete require.cache[require.resolve('./test.js')];
|
||||
delete require.cache[require.resolve('./hmac-test.js')];
|
||||
require('./test.js');
|
||||
require('./hmac-test.js');
|
||||
});
|
||||
@@ -45,6 +45,10 @@
|
||||
'f495de6d0f1b5681070a024bbaed5b5f42847306': ['中文', '中文'],
|
||||
'58891d68487ffebddba5925abedec77a5a578db2': ['aécio', 'aécio'],
|
||||
'a1816bff2dae324c283aeab564d5edb5170fbada': ['𠜎', '𠜎']
|
||||
},
|
||||
'UTF8 unpaired surrogates': {
|
||||
'f1166943c193e8d50d63047b45fa9e37af769de4': ['\ud800', 'a\ud83db'],
|
||||
'922021b7e658c519bbc6f33d66fd8ac9935541a9': ['a\udc00b', '\ud800']
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
+49
-50
@@ -73,6 +73,55 @@ define.amd = true;
|
||||
require('../src/sha1.js');
|
||||
unset();
|
||||
|
||||
// browser with a process polyfill (e.g. BrowserFS)
|
||||
window = global;
|
||||
runCommonJsTest();
|
||||
|
||||
// Electron renderer
|
||||
process.type = 'renderer';
|
||||
sha1 = require('../src/sha1.js');
|
||||
delete process.type;
|
||||
require('./test.js');
|
||||
require('./hmac-test.js');
|
||||
unset();
|
||||
|
||||
// environment detection in isolated contexts (no Node.js globals)
|
||||
describe('environment detection', function () {
|
||||
var vm = require('vm');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var source = fs.readFileSync(path.join(__dirname, '../src/sha1.js'), 'utf8');
|
||||
|
||||
function run(sandbox) {
|
||||
vm.runInContext(source, vm.createContext(sandbox));
|
||||
return sandbox.sha1;
|
||||
}
|
||||
|
||||
function check(sha1) {
|
||||
expect(sha1('abc')).to.be('a9993e364706816aba3e25717850c26c9cd0d89d');
|
||||
expect(sha1('中文')).to.be('7be2d2d20c106eee0836c9bc2b939890a78e8fb3');
|
||||
expect(sha1.hmac('key', 'abc')).to.be('4fd0b215276ef12f2b3e4c8ecac2811498b656fc');
|
||||
}
|
||||
|
||||
it('should not treat a browser with a process polyfill as Node.js', function () {
|
||||
var sandbox = { process: { versions: { node: '18.0.0' }, env: {} } };
|
||||
sandbox.window = sandbox.self = sandbox;
|
||||
check(run(sandbox));
|
||||
});
|
||||
|
||||
it('should not treat an Electron renderer as Node.js', function () {
|
||||
var sandbox = { process: { type: 'renderer', versions: { node: '20.0.0' } } };
|
||||
sandbox.window = sandbox.self = sandbox;
|
||||
check(run(sandbox));
|
||||
});
|
||||
|
||||
it('should expose to the global scope in a web worker', function () {
|
||||
var sandbox = {};
|
||||
sandbox.self = sandbox;
|
||||
check(run(sandbox));
|
||||
});
|
||||
});
|
||||
|
||||
// webworker
|
||||
WORKER = 'tests/worker.js';
|
||||
SOURCE = 'src/sha1.js';
|
||||
@@ -106,53 +155,3 @@ 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');
|
||||
|
||||
@@ -28,6 +28,12 @@
|
||||
'ad8aae581c915fe01c4964a5e8b322cae74ee5c5': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
|
||||
'3a15ad3ce9efdd4bf982eaaaecdeda36a887a3f9': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。'
|
||||
},
|
||||
'UTF8 unpaired surrogates': {
|
||||
'9bdb77276c1852e1fb067820472812fcf6084024': '\ud800',
|
||||
'c3693aea616c886c93746deab3d42921ca20f04e': 'a\ud83db',
|
||||
'dd5098b85f2b1fe5179188703409f3bcef8ef82e': '\ud83d\ud83d\ude00\udfff',
|
||||
'4c40ec6d4181034dc5130d01ebf4aea4d7e87f13': Array(63).join('a') + '\ud800b'
|
||||
},
|
||||
'special length': {
|
||||
'4cdeae78e8b7285aef73e0a15eec7d5b30f3f3e3': '0123456780123456780123456780123456780123456780123456780',
|
||||
'e657e6bb6b5d0c2bf7e929451c14a5302589a60b': '01234567801234567801234567801234567801234567801234567801',
|
||||
@@ -190,6 +196,13 @@
|
||||
});
|
||||
});
|
||||
|
||||
context('when a surrogate pair is split across updates', function () {
|
||||
it('should encode each unpaired half as U+FFFD', function () {
|
||||
expect(sha1.update('a\ud83d').update('\ude00b').hex()).to.be('0d202f075aad3e6c714389bf818e2877cc537c85');
|
||||
expect(sha1.update('a\ud83d').update('b').hex()).to.be('c3693aea616c886c93746deab3d42921ca20f04e');
|
||||
});
|
||||
});
|
||||
|
||||
context('when update after finalize', function () {
|
||||
it('should throw error', function () {
|
||||
expect(function () {
|
||||
|
||||
Reference in New Issue
Block a user