Author SHA1 Message Date
Yi-Cyuan ChenandClaude Opus 5.5 3e1a92f2a4 ### 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>
2026-11-17 13:58:54 +08:00
emn178andGitHub fd520b41fa Merge pull request #23 from emn178/dependabot/npm_and_yarn/multi-4debdb1300
Bump brace-expansion
2026-11-17 12:03:57 +08:00
22 changed files with 3136 additions and 1922 deletions
-2
View File
@@ -1,2 +0,0 @@
/node_modules/
/tests/
+77
View File
@@ -0,0 +1,77 @@
name: CI
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node 24
uses: actions/setup-node@v4
with:
node-version: 24.x
- name: Install dependencies
run: npm ci
- name: Pack npm package
run: |
mkdir -p package-artifact
npm pack --pack-destination package-artifact
- name: Upload package artifact
uses: actions/upload-artifact@v4
with:
name: package-node-24
path: package-artifact/*.tgz
if-no-files-found: error
runtime-test:
needs: build
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x, 22.x, 24.x]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm ci
- name: Download package artifact
uses: actions/download-artifact@v4
with:
name: package-node-24
path: package-artifact
- name: Extract package artifact
run: tar -xzf package-artifact/*.tgz --strip-components=1 -C .
- name: Run runtime tests
if: matrix.node-version != '24.x'
run: npm run test:runtime
- name: Run runtime tests with coverage
if: matrix.node-version == '24.x'
run: npm run test:coverage
- name: Upload to coveralls
if: matrix.node-version == '24.x'
uses: coverallsapp/github-action@v2
with:
file: ./coverage/lcov.info
+32
View File
@@ -0,0 +1,32 @@
name: Publish
on:
release:
types: [published]
permissions:
contents: read
id-token: write
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 24.x
registry-url: https://registry.npmjs.org
package-manager-cache: false
- run: npm ci
- name: Verify release version
run: |
PACKAGE_VERSION=$(node -p "require('./package.json').version")
test "$GITHUB_REF_NAME" = "v$PACKAGE_VERSION"
- run: npm run build
- run: npm test
- run: npm publish --provenance
+2 -2
View File
@@ -2,7 +2,7 @@
/coverage/ /coverage/
/.nyc_output/ /.nyc_output/
/tests/ /tests/
.covignore /.github/
.travis.yml
.npmignore .npmignore
bower.json bower.json
rollup.config.mjs
-10
View File
@@ -1,10 +0,0 @@
language: node_js
node_js:
- "6.11.4"
- "8.6.0"
before_install:
- npm install coveralls
after_success: npm run coveralls
branches:
only:
- master
+12
View File
@@ -1,5 +1,17 @@
# Change Log # Change Log
## v0.8.0 / 2026-11-17
### 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.
## v0.7.0 / 2024-01-24 ## v0.7.0 / 2024-01-24
### Added ### Added
- TypeScript interfaces. #6, #9 - TypeScript interfaces. #6, #9
+1 -1
View File
@@ -1,4 +1,4 @@
Copyright 2014-2024 Chen, Yi-Cyuan Copyright 2014-2026 Chen, Yi-Cyuan
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the
+23 -3
View File
@@ -1,11 +1,13 @@
# js-sha1 # js-sha1
[![Build Status](https://travis-ci.org/emn178/js-sha1.svg?branch=master)](https://travis-ci.org/emn178/js-sha1) [![CI](https://github.com/emn178/js-sha1/actions/workflows/ci.yml/badge.svg)](https://github.com/emn178/js-sha1/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/emn178/js-sha1/badge.svg?branch=master)](https://coveralls.io/r/emn178/js-sha1?branch=master) [![Coverage Status](https://coveralls.io/repos/emn178/js-sha1/badge.svg?branch=master)](https://coveralls.io/r/emn178/js-sha1?branch=master)
[![NPM](https://nodei.co/npm/js-sha1.png?stars&downloads)](https://nodei.co/npm/js-sha1/) [![NPM](https://img.shields.io/npm/v/js-sha1)](https://www.npmjs.com/package/js-sha1)
[![CDNJS](https://img.shields.io/jsdelivr/npm/hm/js-sha1)](https://www.jsdelivr.com/package/npm/js-sha1)
A simple SHA1 hash function for JavaScript supports UTF-8 encoding. A simple SHA1 hash function for JavaScript supports UTF-8 encoding.
## Demo ## Demo
[SHA1 Online](http://emn178.github.io/online-tools/sha1.html) [SHA1 Online](https://emn178.github.io/online-tools/sha1.html)
[SHA1 File Checksum Online](https://emn178.github.io/online-tools/sha1_checksum.html) [SHA1 File Checksum Online](https://emn178.github.io/online-tools/sha1_checksum.html)
## Download ## Download
@@ -46,6 +48,21 @@ If you use node.js, you should require the module first:
var sha1 = require('js-sha1'); var sha1 = require('js-sha1');
``` ```
### ESM
If you use node.js ESM or a bundler, you can import like this:
```JavaScript
import { sha1 } from 'js-sha1';
// or
import sha1 from 'js-sha1';
```
In browsers, you can import the ESM build directly:
```HTML
<script type="module">
import { sha1 } from 'https://cdn.jsdelivr.net/npm/js-sha1/build/sha1.min.mjs';
</script>
```
### TypeScript ### TypeScript
If you use TypeScript, you can import like this: If you use TypeScript, you can import like this:
```TypeScript ```TypeScript
@@ -69,6 +86,9 @@ sha1('The quick brown fox jumps over the lazy dog.'); // 408d94384216f890ff7a0c3
// It also supports UTF-8 encoding // It also supports UTF-8 encoding
sha1('中文'); // 7be2d2d20c106eee0836c9bc2b939890a78e8fb3 sha1('中文'); // 7be2d2d20c106eee0836c9bc2b939890a78e8fb3
// Unpaired surrogates are encoded as U+FFFD, the same as TextEncoder
sha1('\ud800'); // 9bdb77276c1852e1fb067820472812fcf6084024
// It also supports byte `Array`, `Uint8Array`, `ArrayBuffer` // It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
sha1([]); // da39a3ee5e6b4b0d3255bfef95601890afd80709 sha1([]); // da39a3ee5e6b4b0d3255bfef95601890afd80709
sha1(new Uint8Array([])); // da39a3ee5e6b4b0d3255bfef95601890afd80709 sha1(new Uint8Array([])); // da39a3ee5e6b4b0d3255bfef95601890afd80709
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "js-sha1", "name": "js-sha1",
"version": "0.7.0", "version": "0.8.0",
"main": ["src/sha1.js"], "main": ["src/sha1.js"],
"ignore": [ "ignore": [
"samples", "samples",
+3 -3
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+492
View File
@@ -0,0 +1,492 @@
function getDefaultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
var sha1$1 = {exports: {}};
/*
* [js-sha1]{@link https://github.com/emn178/js-sha1}
*
* @version 0.8.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
*/
(function (module) {
/*jslint bitwise: true */
(function() {
var INPUT_ERROR = 'input is invalid type';
var FINALIZE_ERROR = 'finalize already called';
var WINDOW = typeof window === 'object';
var root = WINDOW ? window : {};
if (root.JS_SHA1_NO_WINDOW) {
WINDOW = false;
}
var WEB_WORKER = !WINDOW && typeof self === 'object';
if (WEB_WORKER) {
root = self;
}
var COMMON_JS = !root.JS_SHA1_NO_COMMON_JS && 'object' === 'object' && module.exports;
var ARRAY_BUFFER = !root.JS_SHA1_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
var HEX_CHARS = '0123456789abcdef'.split('');
var EXTRA = [-2147483648, 8388608, 32768, 128];
var SHIFT = [24, 16, 8, 0];
var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
var blocks = [];
var isArray = Array.isArray;
if (root.JS_SHA1_NO_NODE_JS || !isArray) {
isArray = function (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
}
var isView = ArrayBuffer.isView;
if (ARRAY_BUFFER && (root.JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
isView = function (obj) {
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
};
}
// [message: string, isString: bool]
var formatMessage = function (message) {
var type = typeof message;
if (type === 'string') {
return [message, true];
}
if (type !== 'object' || message === null) {
throw new Error(INPUT_ERROR);
}
if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
};
var createOutputMethod = function (outputType) {
return function (message) {
return new Sha1(true).update(message)[outputType]();
};
};
var createMethod = function () {
var method = createOutputMethod('hex');
method.create = function () {
return new Sha1();
};
method.update = function (message) {
return method.create().update(message);
};
for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
var type = OUTPUT_TYPES[i];
method[type] = createOutputMethod(type);
}
return method;
};
var createHmacOutputMethod = function (outputType) {
return function (key, message) {
return new HmacSha1(key, true).update(message)[outputType]();
};
};
var createHmacMethod = function () {
var method = createHmacOutputMethod('hex');
method.create = function (key) {
return new HmacSha1(key);
};
method.update = function (key, message) {
return method.create(key).update(message);
};
for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
var type = OUTPUT_TYPES[i];
method[type] = createHmacOutputMethod(type);
}
return method;
};
function Sha1(sharedMemory) {
if (sharedMemory) {
blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
this.blocks = blocks;
} else {
this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
}
this.h0 = 0x67452301;
this.h1 = 0xEFCDAB89;
this.h2 = 0x98BADCFE;
this.h3 = 0x10325476;
this.h4 = 0xC3D2E1F0;
this.block = this.start = this.bytes = this.hBytes = 0;
this.finalized = this.hashed = false;
this.first = true;
}
Sha1.prototype.update = function (message) {
if (this.finalized) {
throw new Error(FINALIZE_ERROR);
}
var result = formatMessage(message);
message = result[0];
var isString = result[1];
var code, code2, index = 0, i, length = message.length || 0, blocks = this.blocks;
while (index < length) {
if (this.hashed) {
this.hashed = false;
blocks[0] = this.block;
this.block = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
}
if(isString) {
for (i = this.start; index < length && i < 64; ++index) {
code = message.charCodeAt(index);
if (code < 0x80) {
blocks[i >>> 2] |= code << SHIFT[i++ & 3];
} else if (code < 0x800) {
blocks[i >>> 2] |= (0xc0 | (code >>> 6)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else if (code < 0xd800 || code >= 0xe000) {
blocks[i >>> 2] |= (0xe0 | (code >>> 12)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else if (code < 0xdc00 && (code2 = message.charCodeAt(index + 1)) >= 0xdc00 && code2 < 0xe000) {
++index;
code = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
blocks[i >>> 2] |= (0xf0 | (code >>> 18)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 12) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else {
// unpaired surrogate: U+FFFD
blocks[i >>> 2] |= 0xef << SHIFT[i++ & 3];
blocks[i >>> 2] |= 0xbf << SHIFT[i++ & 3];
blocks[i >>> 2] |= 0xbd << SHIFT[i++ & 3];
}
}
} else {
for (i = this.start; index < length && i < 64; ++index) {
blocks[i >>> 2] |= message[index] << SHIFT[i++ & 3];
}
}
this.lastByteIndex = i;
this.bytes += i - this.start;
if (i >= 64) {
this.block = blocks[16];
this.start = i - 64;
this.hash();
this.hashed = true;
} else {
this.start = i;
}
}
if (this.bytes > 4294967295) {
this.hBytes += this.bytes / 4294967296 << 0;
this.bytes = this.bytes % 4294967296;
}
return this;
};
Sha1.prototype.finalize = function () {
if (this.finalized) {
return;
}
this.finalized = true;
var blocks = this.blocks, i = this.lastByteIndex;
blocks[16] = this.block;
blocks[i >>> 2] |= EXTRA[i & 3];
this.block = blocks[16];
if (i >= 56) {
if (!this.hashed) {
this.hash();
}
blocks[0] = this.block;
blocks[16] = blocks[1] = blocks[2] = blocks[3] =
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
}
blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
blocks[15] = this.bytes << 3;
this.hash();
};
Sha1.prototype.hash = function () {
var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4;
var f, j, t, blocks = this.blocks;
for(j = 16; j < 80; ++j) {
t = blocks[j - 3] ^ blocks[j - 8] ^ blocks[j - 14] ^ blocks[j - 16];
blocks[j] = (t << 1) | (t >>> 31);
}
for(j = 0; j < 20; j += 5) {
f = (b & c) | ((~b) & d);
t = (a << 5) | (a >>> 27);
e = t + f + e + 1518500249 + blocks[j] << 0;
b = (b << 30) | (b >>> 2);
f = (a & b) | ((~a) & c);
t = (e << 5) | (e >>> 27);
d = t + f + d + 1518500249 + blocks[j + 1] << 0;
a = (a << 30) | (a >>> 2);
f = (e & a) | ((~e) & b);
t = (d << 5) | (d >>> 27);
c = t + f + c + 1518500249 + blocks[j + 2] << 0;
e = (e << 30) | (e >>> 2);
f = (d & e) | ((~d) & a);
t = (c << 5) | (c >>> 27);
b = t + f + b + 1518500249 + blocks[j + 3] << 0;
d = (d << 30) | (d >>> 2);
f = (c & d) | ((~c) & e);
t = (b << 5) | (b >>> 27);
a = t + f + a + 1518500249 + blocks[j + 4] << 0;
c = (c << 30) | (c >>> 2);
}
for(; j < 40; j += 5) {
f = b ^ c ^ d;
t = (a << 5) | (a >>> 27);
e = t + f + e + 1859775393 + blocks[j] << 0;
b = (b << 30) | (b >>> 2);
f = a ^ b ^ c;
t = (e << 5) | (e >>> 27);
d = t + f + d + 1859775393 + blocks[j + 1] << 0;
a = (a << 30) | (a >>> 2);
f = e ^ a ^ b;
t = (d << 5) | (d >>> 27);
c = t + f + c + 1859775393 + blocks[j + 2] << 0;
e = (e << 30) | (e >>> 2);
f = d ^ e ^ a;
t = (c << 5) | (c >>> 27);
b = t + f + b + 1859775393 + blocks[j + 3] << 0;
d = (d << 30) | (d >>> 2);
f = c ^ d ^ e;
t = (b << 5) | (b >>> 27);
a = t + f + a + 1859775393 + blocks[j + 4] << 0;
c = (c << 30) | (c >>> 2);
}
for(; j < 60; j += 5) {
f = (b & c) | (b & d) | (c & d);
t = (a << 5) | (a >>> 27);
e = t + f + e - 1894007588 + blocks[j] << 0;
b = (b << 30) | (b >>> 2);
f = (a & b) | (a & c) | (b & c);
t = (e << 5) | (e >>> 27);
d = t + f + d - 1894007588 + blocks[j + 1] << 0;
a = (a << 30) | (a >>> 2);
f = (e & a) | (e & b) | (a & b);
t = (d << 5) | (d >>> 27);
c = t + f + c - 1894007588 + blocks[j + 2] << 0;
e = (e << 30) | (e >>> 2);
f = (d & e) | (d & a) | (e & a);
t = (c << 5) | (c >>> 27);
b = t + f + b - 1894007588 + blocks[j + 3] << 0;
d = (d << 30) | (d >>> 2);
f = (c & d) | (c & e) | (d & e);
t = (b << 5) | (b >>> 27);
a = t + f + a - 1894007588 + blocks[j + 4] << 0;
c = (c << 30) | (c >>> 2);
}
for(; j < 80; j += 5) {
f = b ^ c ^ d;
t = (a << 5) | (a >>> 27);
e = t + f + e - 899497514 + blocks[j] << 0;
b = (b << 30) | (b >>> 2);
f = a ^ b ^ c;
t = (e << 5) | (e >>> 27);
d = t + f + d - 899497514 + blocks[j + 1] << 0;
a = (a << 30) | (a >>> 2);
f = e ^ a ^ b;
t = (d << 5) | (d >>> 27);
c = t + f + c - 899497514 + blocks[j + 2] << 0;
e = (e << 30) | (e >>> 2);
f = d ^ e ^ a;
t = (c << 5) | (c >>> 27);
b = t + f + b - 899497514 + blocks[j + 3] << 0;
d = (d << 30) | (d >>> 2);
f = c ^ d ^ e;
t = (b << 5) | (b >>> 27);
a = t + f + a - 899497514 + blocks[j + 4] << 0;
c = (c << 30) | (c >>> 2);
}
this.h0 = this.h0 + a << 0;
this.h1 = this.h1 + b << 0;
this.h2 = this.h2 + c << 0;
this.h3 = this.h3 + d << 0;
this.h4 = this.h4 + e << 0;
};
Sha1.prototype.hex = function () {
this.finalize();
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4;
return HEX_CHARS[(h0 >>> 28) & 0x0F] + HEX_CHARS[(h0 >>> 24) & 0x0F] +
HEX_CHARS[(h0 >>> 20) & 0x0F] + HEX_CHARS[(h0 >>> 16) & 0x0F] +
HEX_CHARS[(h0 >>> 12) & 0x0F] + HEX_CHARS[(h0 >>> 8) & 0x0F] +
HEX_CHARS[(h0 >>> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
HEX_CHARS[(h1 >>> 28) & 0x0F] + HEX_CHARS[(h1 >>> 24) & 0x0F] +
HEX_CHARS[(h1 >>> 20) & 0x0F] + HEX_CHARS[(h1 >>> 16) & 0x0F] +
HEX_CHARS[(h1 >>> 12) & 0x0F] + HEX_CHARS[(h1 >>> 8) & 0x0F] +
HEX_CHARS[(h1 >>> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
HEX_CHARS[(h2 >>> 28) & 0x0F] + HEX_CHARS[(h2 >>> 24) & 0x0F] +
HEX_CHARS[(h2 >>> 20) & 0x0F] + HEX_CHARS[(h2 >>> 16) & 0x0F] +
HEX_CHARS[(h2 >>> 12) & 0x0F] + HEX_CHARS[(h2 >>> 8) & 0x0F] +
HEX_CHARS[(h2 >>> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
HEX_CHARS[(h3 >>> 28) & 0x0F] + HEX_CHARS[(h3 >>> 24) & 0x0F] +
HEX_CHARS[(h3 >>> 20) & 0x0F] + HEX_CHARS[(h3 >>> 16) & 0x0F] +
HEX_CHARS[(h3 >>> 12) & 0x0F] + HEX_CHARS[(h3 >>> 8) & 0x0F] +
HEX_CHARS[(h3 >>> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
HEX_CHARS[(h4 >>> 28) & 0x0F] + HEX_CHARS[(h4 >>> 24) & 0x0F] +
HEX_CHARS[(h4 >>> 20) & 0x0F] + HEX_CHARS[(h4 >>> 16) & 0x0F] +
HEX_CHARS[(h4 >>> 12) & 0x0F] + HEX_CHARS[(h4 >>> 8) & 0x0F] +
HEX_CHARS[(h4 >>> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F];
};
Sha1.prototype.toString = Sha1.prototype.hex;
Sha1.prototype.digest = function () {
this.finalize();
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4;
return [
(h0 >>> 24) & 0xFF, (h0 >>> 16) & 0xFF, (h0 >>> 8) & 0xFF, h0 & 0xFF,
(h1 >>> 24) & 0xFF, (h1 >>> 16) & 0xFF, (h1 >>> 8) & 0xFF, h1 & 0xFF,
(h2 >>> 24) & 0xFF, (h2 >>> 16) & 0xFF, (h2 >>> 8) & 0xFF, h2 & 0xFF,
(h3 >>> 24) & 0xFF, (h3 >>> 16) & 0xFF, (h3 >>> 8) & 0xFF, h3 & 0xFF,
(h4 >>> 24) & 0xFF, (h4 >>> 16) & 0xFF, (h4 >>> 8) & 0xFF, h4 & 0xFF
];
};
Sha1.prototype.array = Sha1.prototype.digest;
Sha1.prototype.arrayBuffer = function () {
this.finalize();
var buffer = new ArrayBuffer(20);
var dataView = new DataView(buffer);
dataView.setUint32(0, this.h0);
dataView.setUint32(4, this.h1);
dataView.setUint32(8, this.h2);
dataView.setUint32(12, this.h3);
dataView.setUint32(16, this.h4);
return buffer;
};
function HmacSha1(key, sharedMemory) {
var i, result = formatMessage(key);
key = result[0];
if (result[1]) {
var bytes = [], length = key.length, index = 0, code, code2;
for (i = 0; i < length; ++i) {
code = key.charCodeAt(i);
if (code < 0x80) {
bytes[index++] = code;
} else if (code < 0x800) {
bytes[index++] = (0xc0 | (code >>> 6));
bytes[index++] = (0x80 | (code & 0x3f));
} else if (code < 0xd800 || code >= 0xe000) {
bytes[index++] = (0xe0 | (code >>> 12));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
} else if (code < 0xdc00 && (code2 = key.charCodeAt(i + 1)) >= 0xdc00 && code2 < 0xe000) {
++i;
code = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
bytes[index++] = (0xf0 | (code >>> 18));
bytes[index++] = (0x80 | ((code >>> 12) & 0x3f));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
} else {
// unpaired surrogate: U+FFFD
bytes[index++] = 0xef;
bytes[index++] = 0xbf;
bytes[index++] = 0xbd;
}
}
key = bytes;
}
if (key.length > 64) {
key = (new Sha1(true)).update(key).array();
}
var oKeyPad = [], iKeyPad = [];
for (i = 0; i < 64; ++i) {
var b = key[i] || 0;
oKeyPad[i] = 0x5c ^ b;
iKeyPad[i] = 0x36 ^ b;
}
Sha1.call(this, sharedMemory);
this.update(iKeyPad);
this.oKeyPad = oKeyPad;
this.inner = true;
this.sharedMemory = sharedMemory;
}
HmacSha1.prototype = new Sha1();
HmacSha1.prototype.finalize = function () {
Sha1.prototype.finalize.call(this);
if (this.inner) {
this.inner = false;
var innerHash = this.array();
Sha1.call(this, this.sharedMemory);
this.update(this.oKeyPad);
this.update(innerHash);
Sha1.prototype.finalize.call(this);
}
};
var exports = createMethod();
exports.sha1 = exports;
exports.sha1.hmac = createHmacMethod();
if (COMMON_JS) {
module.exports = exports;
} else {
root.sha1 = exports;
}
})();
} (sha1$1));
var sha1Exports = sha1$1.exports;
var sha1 = /*@__PURE__*/getDefaultExportFromCjs(sha1Exports);
export { sha1 as default, sha1 };
+534
View File
@@ -0,0 +1,534 @@
import require$$0 from 'crypto';
import require$$1 from 'buffer';
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
function getDefaultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
var sha1$1 = {exports: {}};
/*
* [js-sha1]{@link https://github.com/emn178/js-sha1}
*
* @version 0.8.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
*/
(function (module) {
/*jslint bitwise: true */
(function() {
var INPUT_ERROR = 'input is invalid type';
var FINALIZE_ERROR = 'finalize already called';
var WINDOW = typeof window === 'object';
var root = WINDOW ? window : {};
if (root.JS_SHA1_NO_WINDOW) {
WINDOW = false;
}
var WEB_WORKER = !WINDOW && typeof self === 'object';
var NODE_JS = !root.JS_SHA1_NO_NODE_JS && !WINDOW && typeof process === 'object' && process.versions && process.versions.node && process.type != 'renderer';
if (NODE_JS) {
root = commonjsGlobal;
} else if (WEB_WORKER) {
root = self;
}
var COMMON_JS = !root.JS_SHA1_NO_COMMON_JS && 'object' === 'object' && module.exports;
var ARRAY_BUFFER = !root.JS_SHA1_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
var HEX_CHARS = '0123456789abcdef'.split('');
var EXTRA = [-2147483648, 8388608, 32768, 128];
var SHIFT = [24, 16, 8, 0];
var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
var blocks = [];
var isArray = Array.isArray;
if (root.JS_SHA1_NO_NODE_JS || !isArray) {
isArray = function (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
}
var isView = ArrayBuffer.isView;
if (ARRAY_BUFFER && (root.JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
isView = function (obj) {
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
};
}
// [message: string, isString: bool]
var formatMessage = function (message) {
var type = typeof message;
if (type === 'string') {
return [message, true];
}
if (type !== 'object' || message === null) {
throw new Error(INPUT_ERROR);
}
if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
};
var createOutputMethod = function (outputType) {
return function (message) {
return new Sha1(true).update(message)[outputType]();
};
};
var createMethod = function () {
var method = createOutputMethod('hex');
if (NODE_JS) {
method = nodeWrap(method);
}
method.create = function () {
return new Sha1();
};
method.update = function (message) {
return method.create().update(message);
};
for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
var type = OUTPUT_TYPES[i];
method[type] = createOutputMethod(type);
}
return method;
};
var nodeWrap = function (method) {
var crypto = require$$0;
var Buffer = require$$1.Buffer;
var bufferFrom;
if (Buffer.from && !root.JS_SHA1_NO_BUFFER_FROM) {
bufferFrom = Buffer.from;
} else {
bufferFrom = function (message) {
return new Buffer(message);
};
}
var nodeMethod = function (message) {
if (typeof message === 'string') {
return crypto.createHash('sha1').update(message, 'utf8').digest('hex');
} else {
if (message === null || message === undefined) {
throw new Error(INPUT_ERROR);
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
}
}
if (isArray(message) || isView(message) ||
message.constructor === Buffer) {
return crypto.createHash('sha1').update(bufferFrom(message)).digest('hex');
} else {
return method(message);
}
};
return nodeMethod;
};
var createHmacOutputMethod = function (outputType) {
return function (key, message) {
return new HmacSha1(key, true).update(message)[outputType]();
};
};
var createHmacMethod = function () {
var method = createHmacOutputMethod('hex');
method.create = function (key) {
return new HmacSha1(key);
};
method.update = function (key, message) {
return method.create(key).update(message);
};
for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
var type = OUTPUT_TYPES[i];
method[type] = createHmacOutputMethod(type);
}
return method;
};
function Sha1(sharedMemory) {
if (sharedMemory) {
blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
this.blocks = blocks;
} else {
this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
}
this.h0 = 0x67452301;
this.h1 = 0xEFCDAB89;
this.h2 = 0x98BADCFE;
this.h3 = 0x10325476;
this.h4 = 0xC3D2E1F0;
this.block = this.start = this.bytes = this.hBytes = 0;
this.finalized = this.hashed = false;
this.first = true;
}
Sha1.prototype.update = function (message) {
if (this.finalized) {
throw new Error(FINALIZE_ERROR);
}
var result = formatMessage(message);
message = result[0];
var isString = result[1];
var code, code2, index = 0, i, length = message.length || 0, blocks = this.blocks;
while (index < length) {
if (this.hashed) {
this.hashed = false;
blocks[0] = this.block;
this.block = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
}
if(isString) {
for (i = this.start; index < length && i < 64; ++index) {
code = message.charCodeAt(index);
if (code < 0x80) {
blocks[i >>> 2] |= code << SHIFT[i++ & 3];
} else if (code < 0x800) {
blocks[i >>> 2] |= (0xc0 | (code >>> 6)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else if (code < 0xd800 || code >= 0xe000) {
blocks[i >>> 2] |= (0xe0 | (code >>> 12)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else if (code < 0xdc00 && (code2 = message.charCodeAt(index + 1)) >= 0xdc00 && code2 < 0xe000) {
++index;
code = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
blocks[i >>> 2] |= (0xf0 | (code >>> 18)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 12) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else {
// unpaired surrogate: U+FFFD
blocks[i >>> 2] |= 0xef << SHIFT[i++ & 3];
blocks[i >>> 2] |= 0xbf << SHIFT[i++ & 3];
blocks[i >>> 2] |= 0xbd << SHIFT[i++ & 3];
}
}
} else {
for (i = this.start; index < length && i < 64; ++index) {
blocks[i >>> 2] |= message[index] << SHIFT[i++ & 3];
}
}
this.lastByteIndex = i;
this.bytes += i - this.start;
if (i >= 64) {
this.block = blocks[16];
this.start = i - 64;
this.hash();
this.hashed = true;
} else {
this.start = i;
}
}
if (this.bytes > 4294967295) {
this.hBytes += this.bytes / 4294967296 << 0;
this.bytes = this.bytes % 4294967296;
}
return this;
};
Sha1.prototype.finalize = function () {
if (this.finalized) {
return;
}
this.finalized = true;
var blocks = this.blocks, i = this.lastByteIndex;
blocks[16] = this.block;
blocks[i >>> 2] |= EXTRA[i & 3];
this.block = blocks[16];
if (i >= 56) {
if (!this.hashed) {
this.hash();
}
blocks[0] = this.block;
blocks[16] = blocks[1] = blocks[2] = blocks[3] =
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
}
blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
blocks[15] = this.bytes << 3;
this.hash();
};
Sha1.prototype.hash = function () {
var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4;
var f, j, t, blocks = this.blocks;
for(j = 16; j < 80; ++j) {
t = blocks[j - 3] ^ blocks[j - 8] ^ blocks[j - 14] ^ blocks[j - 16];
blocks[j] = (t << 1) | (t >>> 31);
}
for(j = 0; j < 20; j += 5) {
f = (b & c) | ((~b) & d);
t = (a << 5) | (a >>> 27);
e = t + f + e + 1518500249 + blocks[j] << 0;
b = (b << 30) | (b >>> 2);
f = (a & b) | ((~a) & c);
t = (e << 5) | (e >>> 27);
d = t + f + d + 1518500249 + blocks[j + 1] << 0;
a = (a << 30) | (a >>> 2);
f = (e & a) | ((~e) & b);
t = (d << 5) | (d >>> 27);
c = t + f + c + 1518500249 + blocks[j + 2] << 0;
e = (e << 30) | (e >>> 2);
f = (d & e) | ((~d) & a);
t = (c << 5) | (c >>> 27);
b = t + f + b + 1518500249 + blocks[j + 3] << 0;
d = (d << 30) | (d >>> 2);
f = (c & d) | ((~c) & e);
t = (b << 5) | (b >>> 27);
a = t + f + a + 1518500249 + blocks[j + 4] << 0;
c = (c << 30) | (c >>> 2);
}
for(; j < 40; j += 5) {
f = b ^ c ^ d;
t = (a << 5) | (a >>> 27);
e = t + f + e + 1859775393 + blocks[j] << 0;
b = (b << 30) | (b >>> 2);
f = a ^ b ^ c;
t = (e << 5) | (e >>> 27);
d = t + f + d + 1859775393 + blocks[j + 1] << 0;
a = (a << 30) | (a >>> 2);
f = e ^ a ^ b;
t = (d << 5) | (d >>> 27);
c = t + f + c + 1859775393 + blocks[j + 2] << 0;
e = (e << 30) | (e >>> 2);
f = d ^ e ^ a;
t = (c << 5) | (c >>> 27);
b = t + f + b + 1859775393 + blocks[j + 3] << 0;
d = (d << 30) | (d >>> 2);
f = c ^ d ^ e;
t = (b << 5) | (b >>> 27);
a = t + f + a + 1859775393 + blocks[j + 4] << 0;
c = (c << 30) | (c >>> 2);
}
for(; j < 60; j += 5) {
f = (b & c) | (b & d) | (c & d);
t = (a << 5) | (a >>> 27);
e = t + f + e - 1894007588 + blocks[j] << 0;
b = (b << 30) | (b >>> 2);
f = (a & b) | (a & c) | (b & c);
t = (e << 5) | (e >>> 27);
d = t + f + d - 1894007588 + blocks[j + 1] << 0;
a = (a << 30) | (a >>> 2);
f = (e & a) | (e & b) | (a & b);
t = (d << 5) | (d >>> 27);
c = t + f + c - 1894007588 + blocks[j + 2] << 0;
e = (e << 30) | (e >>> 2);
f = (d & e) | (d & a) | (e & a);
t = (c << 5) | (c >>> 27);
b = t + f + b - 1894007588 + blocks[j + 3] << 0;
d = (d << 30) | (d >>> 2);
f = (c & d) | (c & e) | (d & e);
t = (b << 5) | (b >>> 27);
a = t + f + a - 1894007588 + blocks[j + 4] << 0;
c = (c << 30) | (c >>> 2);
}
for(; j < 80; j += 5) {
f = b ^ c ^ d;
t = (a << 5) | (a >>> 27);
e = t + f + e - 899497514 + blocks[j] << 0;
b = (b << 30) | (b >>> 2);
f = a ^ b ^ c;
t = (e << 5) | (e >>> 27);
d = t + f + d - 899497514 + blocks[j + 1] << 0;
a = (a << 30) | (a >>> 2);
f = e ^ a ^ b;
t = (d << 5) | (d >>> 27);
c = t + f + c - 899497514 + blocks[j + 2] << 0;
e = (e << 30) | (e >>> 2);
f = d ^ e ^ a;
t = (c << 5) | (c >>> 27);
b = t + f + b - 899497514 + blocks[j + 3] << 0;
d = (d << 30) | (d >>> 2);
f = c ^ d ^ e;
t = (b << 5) | (b >>> 27);
a = t + f + a - 899497514 + blocks[j + 4] << 0;
c = (c << 30) | (c >>> 2);
}
this.h0 = this.h0 + a << 0;
this.h1 = this.h1 + b << 0;
this.h2 = this.h2 + c << 0;
this.h3 = this.h3 + d << 0;
this.h4 = this.h4 + e << 0;
};
Sha1.prototype.hex = function () {
this.finalize();
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4;
return HEX_CHARS[(h0 >>> 28) & 0x0F] + HEX_CHARS[(h0 >>> 24) & 0x0F] +
HEX_CHARS[(h0 >>> 20) & 0x0F] + HEX_CHARS[(h0 >>> 16) & 0x0F] +
HEX_CHARS[(h0 >>> 12) & 0x0F] + HEX_CHARS[(h0 >>> 8) & 0x0F] +
HEX_CHARS[(h0 >>> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
HEX_CHARS[(h1 >>> 28) & 0x0F] + HEX_CHARS[(h1 >>> 24) & 0x0F] +
HEX_CHARS[(h1 >>> 20) & 0x0F] + HEX_CHARS[(h1 >>> 16) & 0x0F] +
HEX_CHARS[(h1 >>> 12) & 0x0F] + HEX_CHARS[(h1 >>> 8) & 0x0F] +
HEX_CHARS[(h1 >>> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
HEX_CHARS[(h2 >>> 28) & 0x0F] + HEX_CHARS[(h2 >>> 24) & 0x0F] +
HEX_CHARS[(h2 >>> 20) & 0x0F] + HEX_CHARS[(h2 >>> 16) & 0x0F] +
HEX_CHARS[(h2 >>> 12) & 0x0F] + HEX_CHARS[(h2 >>> 8) & 0x0F] +
HEX_CHARS[(h2 >>> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
HEX_CHARS[(h3 >>> 28) & 0x0F] + HEX_CHARS[(h3 >>> 24) & 0x0F] +
HEX_CHARS[(h3 >>> 20) & 0x0F] + HEX_CHARS[(h3 >>> 16) & 0x0F] +
HEX_CHARS[(h3 >>> 12) & 0x0F] + HEX_CHARS[(h3 >>> 8) & 0x0F] +
HEX_CHARS[(h3 >>> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
HEX_CHARS[(h4 >>> 28) & 0x0F] + HEX_CHARS[(h4 >>> 24) & 0x0F] +
HEX_CHARS[(h4 >>> 20) & 0x0F] + HEX_CHARS[(h4 >>> 16) & 0x0F] +
HEX_CHARS[(h4 >>> 12) & 0x0F] + HEX_CHARS[(h4 >>> 8) & 0x0F] +
HEX_CHARS[(h4 >>> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F];
};
Sha1.prototype.toString = Sha1.prototype.hex;
Sha1.prototype.digest = function () {
this.finalize();
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4;
return [
(h0 >>> 24) & 0xFF, (h0 >>> 16) & 0xFF, (h0 >>> 8) & 0xFF, h0 & 0xFF,
(h1 >>> 24) & 0xFF, (h1 >>> 16) & 0xFF, (h1 >>> 8) & 0xFF, h1 & 0xFF,
(h2 >>> 24) & 0xFF, (h2 >>> 16) & 0xFF, (h2 >>> 8) & 0xFF, h2 & 0xFF,
(h3 >>> 24) & 0xFF, (h3 >>> 16) & 0xFF, (h3 >>> 8) & 0xFF, h3 & 0xFF,
(h4 >>> 24) & 0xFF, (h4 >>> 16) & 0xFF, (h4 >>> 8) & 0xFF, h4 & 0xFF
];
};
Sha1.prototype.array = Sha1.prototype.digest;
Sha1.prototype.arrayBuffer = function () {
this.finalize();
var buffer = new ArrayBuffer(20);
var dataView = new DataView(buffer);
dataView.setUint32(0, this.h0);
dataView.setUint32(4, this.h1);
dataView.setUint32(8, this.h2);
dataView.setUint32(12, this.h3);
dataView.setUint32(16, this.h4);
return buffer;
};
function HmacSha1(key, sharedMemory) {
var i, result = formatMessage(key);
key = result[0];
if (result[1]) {
var bytes = [], length = key.length, index = 0, code, code2;
for (i = 0; i < length; ++i) {
code = key.charCodeAt(i);
if (code < 0x80) {
bytes[index++] = code;
} else if (code < 0x800) {
bytes[index++] = (0xc0 | (code >>> 6));
bytes[index++] = (0x80 | (code & 0x3f));
} else if (code < 0xd800 || code >= 0xe000) {
bytes[index++] = (0xe0 | (code >>> 12));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
} else if (code < 0xdc00 && (code2 = key.charCodeAt(i + 1)) >= 0xdc00 && code2 < 0xe000) {
++i;
code = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
bytes[index++] = (0xf0 | (code >>> 18));
bytes[index++] = (0x80 | ((code >>> 12) & 0x3f));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
} else {
// unpaired surrogate: U+FFFD
bytes[index++] = 0xef;
bytes[index++] = 0xbf;
bytes[index++] = 0xbd;
}
}
key = bytes;
}
if (key.length > 64) {
key = (new Sha1(true)).update(key).array();
}
var oKeyPad = [], iKeyPad = [];
for (i = 0; i < 64; ++i) {
var b = key[i] || 0;
oKeyPad[i] = 0x5c ^ b;
iKeyPad[i] = 0x36 ^ b;
}
Sha1.call(this, sharedMemory);
this.update(iKeyPad);
this.oKeyPad = oKeyPad;
this.inner = true;
this.sharedMemory = sharedMemory;
}
HmacSha1.prototype = new Sha1();
HmacSha1.prototype.finalize = function () {
Sha1.prototype.finalize.call(this);
if (this.inner) {
this.inner = false;
var innerHash = this.array();
Sha1.call(this, this.sharedMemory);
this.update(this.oKeyPad);
this.update(innerHash);
Sha1.prototype.finalize.call(this);
}
};
var exports = createMethod();
exports.sha1 = exports;
exports.sha1.hmac = createHmacMethod();
if (COMMON_JS) {
module.exports = exports;
} else {
root.sha1 = exports;
}
})();
} (sha1$1));
var sha1Exports = sha1$1.exports;
var sha1 = /*@__PURE__*/getDefaultExportFromCjs(sha1Exports);
export { sha1 as default, sha1 };
+1748 -1832
View File
File diff suppressed because it is too large Load Diff
+38 -9
View File
@@ -1,21 +1,46 @@
{ {
"name": "js-sha1", "name": "js-sha1",
"version": "0.7.0", "version": "0.8.0",
"description": "A simple SHA1 hash function for JavaScript supports UTF-8 encoding.", "description": "A simple SHA1 hash function for JavaScript supports UTF-8 encoding.",
"main": "src/sha1.js", "main": "src/sha1.js",
"module": "build/sha1.mjs",
"exports": {
".": {
"types": "./index.d.ts",
"node": {
"import": "./build/sha1.node.mjs",
"require": "./src/sha1.js"
},
"import": "./build/sha1.mjs",
"require": "./src/sha1.js",
"default": "./src/sha1.js"
},
"./src/sha1.js": "./src/sha1.js",
"./build/sha1.min.js": "./build/sha1.min.js",
"./build/sha1.mjs": "./build/sha1.mjs",
"./build/sha1.min.mjs": "./build/sha1.min.mjs",
"./build/sha1.node.mjs": "./build/sha1.node.mjs",
"./package.json": "./package.json"
},
"types": "./index.d.ts",
"devDependencies": { "devDependencies": {
"@rollup/plugin-commonjs": "^29.0.3",
"@rollup/plugin-terser": "^1.0.2",
"expect.js": "~0.3.1", "expect.js": "~0.3.1",
"mocha": "~10.2.0", "mocha": "^12.0.3",
"nyc": "^15.1.0", "nyc": "^18.0.0",
"requirejs": "^2.1.22", "requirejs": "^2.3.8",
"rollup": "^4.65.0",
"tiny-worker": "^2.3.0", "tiny-worker": "^2.3.0",
"uglify-js": "^3.1.9" "uglify-js": "^3.19.3"
}, },
"scripts": { "scripts": {
"test": "nyc mocha tests/node-test.js", "test": "npm run build && npm run test:coverage",
"report": "nyc --reporter=html --reporter=text mocha tests/node-test.js", "test:runtime": "mocha tests/node-test.js && mocha tests/esm-test.mjs",
"coveralls": "nyc report --reporter=text-lcov | coveralls", "test:coverage": "nyc --reporter=text --reporter=html --reporter=lcov npm run test:runtime",
"build": "uglifyjs src/sha1.js -c -m eval --comments -o build/sha1.min.js" "report": "nyc --reporter=html --reporter=text npm run test:runtime",
"build": "rollup -c && uglifyjs src/sha1.js -c -m eval --comments -o build/sha1.min.js",
"prepack": "npm run build"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@@ -42,5 +67,9 @@
"browser": { "browser": {
"crypto": false, "crypto": false,
"buffer": false "buffer": false
},
"overrides": {
"glob": "^14.0.2",
"minimatch": "^11.0.1"
} }
} }
+48
View File
@@ -0,0 +1,48 @@
import commonjs from '@rollup/plugin-commonjs';
import terser from '@rollup/plugin-terser';
const disableNodeJs = {
name: 'disable-node-js',
transform(code, id) {
if (id.endsWith('/src/sha1.js')) {
const nodeJsDeclaration = /^\s*var NODE_JS = .*;$/m;
if (!nodeJsDeclaration.test(code)) {
this.error('Could not find the NODE_JS declaration');
}
return code.replace(nodeJsDeclaration, ' var NODE_JS = false;');
}
}
};
export default [
{
input: 'src/sha1.mjs',
external: ['crypto', 'buffer'],
output: {
file: 'build/sha1.node.mjs',
format: 'es'
},
plugins: [commonjs({
strictRequires: false
})]
},
{
input: 'src/sha1.mjs',
output: {
file: 'build/sha1.mjs',
format: 'es'
},
plugins: [disableNodeJs, commonjs({
strictRequires: false,
ignore: ['crypto', 'buffer']
})]
},
{
input: 'build/sha1.mjs',
output: {
file: 'build/sha1.min.mjs',
format: 'es'
},
plugins: [terser()]
}
];
+21 -9
View File
@@ -1,9 +1,9 @@
/* /*
* [js-sha1]{@link https://github.com/emn178/js-sha1} * [js-sha1]{@link https://github.com/emn178/js-sha1}
* *
* @version 0.7.0 * @version 0.8.0
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2024 * @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT * @license MIT
*/ */
/*jslint bitwise: true */ /*jslint bitwise: true */
@@ -18,7 +18,7 @@
WINDOW = false; WINDOW = false;
} }
var WEB_WORKER = !WINDOW && typeof self === 'object'; var WEB_WORKER = !WINDOW && typeof self === 'object';
var NODE_JS = !root.JS_SHA1_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; var NODE_JS = !root.JS_SHA1_NO_NODE_JS && !WINDOW && typeof process === 'object' && process.versions && process.versions.node && process.type != 'renderer';
if (NODE_JS) { if (NODE_JS) {
root = global; root = global;
} else if (WEB_WORKER) { } else if (WEB_WORKER) {
@@ -172,7 +172,7 @@
var result = formatMessage(message); var result = formatMessage(message);
message = result[0]; message = result[0];
var isString = result[1]; var isString = result[1];
var code, index = 0, i, length = message.length || 0, blocks = this.blocks; var code, code2, index = 0, i, length = message.length || 0, blocks = this.blocks;
while (index < length) { while (index < length) {
if (this.hashed) { if (this.hashed) {
@@ -196,12 +196,18 @@
blocks[i >>> 2] |= (0xe0 | (code >>> 12)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0xe0 | (code >>> 12)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else { } else if (code < 0xdc00 && (code2 = message.charCodeAt(index + 1)) >= 0xdc00 && code2 < 0xe000) {
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); ++index;
code = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
blocks[i >>> 2] |= (0xf0 | (code >>> 18)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0xf0 | (code >>> 18)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 12) & 0x3f)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0x80 | ((code >>> 12) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else {
// unpaired surrogate: U+FFFD
blocks[i >>> 2] |= 0xef << SHIFT[i++ & 3];
blocks[i >>> 2] |= 0xbf << SHIFT[i++ & 3];
blocks[i >>> 2] |= 0xbd << SHIFT[i++ & 3];
} }
} }
} else { } else {
@@ -438,7 +444,7 @@
var i, result = formatMessage(key); var i, result = formatMessage(key);
key = result[0]; key = result[0];
if (result[1]) { if (result[1]) {
var bytes = [], length = key.length, index = 0, code; var bytes = [], length = key.length, index = 0, code, code2;
for (i = 0; i < length; ++i) { for (i = 0; i < length; ++i) {
code = key.charCodeAt(i); code = key.charCodeAt(i);
if (code < 0x80) { if (code < 0x80) {
@@ -450,12 +456,18 @@
bytes[index++] = (0xe0 | (code >>> 12)); bytes[index++] = (0xe0 | (code >>> 12));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f)); bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f)); bytes[index++] = (0x80 | (code & 0x3f));
} else { } else if (code < 0xdc00 && (code2 = key.charCodeAt(i + 1)) >= 0xdc00 && code2 < 0xe000) {
code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff)); ++i;
code = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
bytes[index++] = (0xf0 | (code >>> 18)); bytes[index++] = (0xf0 | (code >>> 18));
bytes[index++] = (0x80 | ((code >>> 12) & 0x3f)); bytes[index++] = (0x80 | ((code >>> 12) & 0x3f));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f)); bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f)); bytes[index++] = (0x80 | (code & 0x3f));
} else {
// unpaired surrogate: U+FFFD
bytes[index++] = 0xef;
bytes[index++] = 0xbf;
bytes[index++] = 0xbd;
} }
} }
key = bytes; key = bytes;
+4
View File
@@ -0,0 +1,4 @@
import sha1 from './sha1.js';
export { sha1 };
export default sha1;
+25
View File
@@ -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');
});
+4
View File
@@ -45,6 +45,10 @@
'f495de6d0f1b5681070a024bbaed5b5f42847306': ['中文', '中文'], 'f495de6d0f1b5681070a024bbaed5b5f42847306': ['中文', '中文'],
'58891d68487ffebddba5925abedec77a5a578db2': ['aécio', 'aécio'], '58891d68487ffebddba5925abedec77a5a578db2': ['aécio', 'aécio'],
'a1816bff2dae324c283aeab564d5edb5170fbada': ['𠜎', '𠜎'] 'a1816bff2dae324c283aeab564d5edb5170fbada': ['𠜎', '𠜎']
},
'UTF8 unpaired surrogates': {
'f1166943c193e8d50d63047b45fa9e37af769de4': ['\ud800', 'a\ud83db'],
'922021b7e658c519bbc6f33d66fd8ac9935541a9': ['a\udc00b', '\ud800']
} }
} }
}; };
+49 -50
View File
@@ -73,6 +73,55 @@ define.amd = true;
require('../src/sha1.js'); require('../src/sha1.js');
unset(); 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 // webworker
WORKER = 'tests/worker.js'; WORKER = 'tests/worker.js';
SOURCE = 'src/sha1.js'; SOURCE = 'src/sha1.js';
@@ -106,53 +155,3 @@ importScripts = function () {};
sha1 = require('../src/sha1.js'); sha1 = require('../src/sha1.js');
require('./worker-test.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');
+13
View File
@@ -28,6 +28,12 @@
'ad8aae581c915fe01c4964a5e8b322cae74ee5c5': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', 'ad8aae581c915fe01c4964a5e8b322cae74ee5c5': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
'3a15ad3ce9efdd4bf982eaaaecdeda36a887a3f9': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有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': { 'special length': {
'4cdeae78e8b7285aef73e0a15eec7d5b30f3f3e3': '0123456780123456780123456780123456780123456780123456780', '4cdeae78e8b7285aef73e0a15eec7d5b30f3f3e3': '0123456780123456780123456780123456780123456780123456780',
'e657e6bb6b5d0c2bf7e929451c14a5302589a60b': '01234567801234567801234567801234567801234567801234567801', '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 () { context('when update after finalize', function () {
it('should throw error', function () { it('should throw error', function () {
expect(function () { expect(function () {