### 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:
Yi-Cyuan Chen
2026-07-27 14:30:34 +08:00
parent b766562f6a
commit a6d0f636aa
40 changed files with 1910 additions and 3363 deletions
+37 -17
View File
@@ -9,23 +9,45 @@ A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding.
[SHA256 Online](https://emn178.github.io/online-tools/sha256.html)
[SHA224 Online](https://emn178.github.io/online-tools/sha224.html)
[SHA256 File Online](https://emn178.github.io/online-tools/sha256_checksum.html)
[SHA225 File Online](https://emn178.github.io/online-tools/sha224_checksum.html)
[SHA224 File Online](https://emn178.github.io/online-tools/sha224_checksum.html)
## Download
[Compress](https://raw.github.com/emn178/js-sha256/master/build/sha256.min.js)
[Uncompress](https://raw.github.com/emn178/js-sha256/master/src/sha256.js)
[Uncompress](https://raw.github.com/emn178/js-sha256/master/build/sha256.js)
## Installation
You can also install js-sha256 by using Bower.
bower install js-sha256
For node.js, you can use this command to install:
For Node.js, you can use this command to install:
npm install js-sha256
## Usage
You could use like this:
### Browser
Load the minified UMD build to expose `sha256` and `sha224` as globals:
```HTML
<script src="sha256.min.js"></script>
<script>
console.log(sha256('Message to hash'));
</script>
```
### ES Modules
```JavaScript
import sha256, { sha224 } from 'js-sha256';
```
### CommonJS
```JavaScript
const sha256 = require('js-sha256');
const { sha224 } = sha256;
```
### API
```JavaScript
sha256('Message to hash');
sha224('Message to hash');
@@ -52,28 +74,26 @@ hash2.array();
```
### Node.js
If you use node.js, you should require the module first:
Node.js automatically uses its native `crypto` implementation with the same API:
```JavaScript
var sha256 = require('js-sha256');
```
or
```JavaScript
var sha256 = require('js-sha256').sha256;
var sha224 = require('js-sha256').sha224;
```
or
```JavaScript
const { sha256, sha224 } = require('js-sha256');
import sha256, { sha224 } from 'js-sha256';
const hash = sha256.create();
hash.update('Message');
hash.update(' to hash');
console.log(hash.hex());
```
### TypeScript
If you use TypeScript, you can import like this:
```TypeScript
import { sha256, sha224 } from 'js-sha256';
```
### RequireJS
It supports AMD:
The UMD browser build supports AMD:
```JavaScript
require(['your/path/sha256.js'], function(sha256) {
require(['your/path/sha256.min.js'], function(sha256) {
// ...
});
```