Skip to content

Commit

Permalink
## v0.10.0 / 2023-08-30
Browse files Browse the repository at this point in the history
### Fixed
- Chrome bug by workaround. #40
- deprecated `new Buffer`, replace with `Buffer.from`. #34
- dependencies and security issues. #32, #36

### Changed
- TypeScript interface, secretKey can be bytes like message. #23, #25
- remove `eval` and use `require` directly. #18, #26
  • Loading branch information
emn178 committed Aug 30, 2023
1 parent 16b101f commit bb8ae3a
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 58 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Change Log

## v0.10.0 / 2023-08-30
### Fixed
- Chrome bug by workaround. #40
- deprecated `new Buffer`, replace with `Buffer.from`. #34
- dependencies and security issues. #32, #36

### Changed
- TypeScript interface, secretKey can be bytes like message. #23, #25
- remove `eval` and use `require` directly. #18, #26

## v0.9.0 / 2017-12-18
### Fixed
- incorrect result when first bit is 1 of bytes.
Expand Down
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,33 @@ var hash2 = sha256.hmac.update('key', 'Message to hash');
hash2.update('Message2 to hash');
hash2.array();
```

### Node.js
If you use node.js, you should require the module first:
```JavaScript
var sha256 = require('js-sha256');
```
or
or
```JavaScript
var sha256 = require('js-sha256').sha256;
var sha224 = require('js-sha256').sha224;
```
It supports AMD:
or
```JavaScript
require(['your/path/sha256.js'], function(sha256) {
// ...
});
const { sha256, sha224 } = require('js-sha256');
```
### TypeScript
or TypeScript
```TypeScript
import { sha256, sha224 } from 'js-sha256';
```
### RequireJS
It supports AMD:
```JavaScript
require(['your/path/sha256.js'], function(sha256) {
// ...
});
```
## Example
```JavaScript
sha256(''); // e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
Expand Down
6 changes: 3 additions & 3 deletions build/sha256.min.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,54 +41,54 @@ interface Hmac {
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
(secretKey: string, message: Message): string;
(secretKey: Message, message: Message): string;

/**
* Create a hash object using a secret key.
*
* @param secretKey The Secret Key
*/
create(secretKey: string): Hasher;
create(secretKey: Message): Hasher;

/**
* Create a hash object and hash message using a secret key
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
update(secretKey: string, message: Message): Hasher;
update(secretKey: Message, message: Message): Hasher;

/**
* Return hash in hex string.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
hex(secretKey: string, message: Message): string;
hex(secretKey: Message, message: Message): string;

/**
* Return hash in ArrayBuffer.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
arrayBuffer(secretKey: string, message: Message): ArrayBuffer;
arrayBuffer(secretKey: Message, message: Message): ArrayBuffer;

/**
* Return hash in integer array.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
digest(secretKey: string, message: Message): number[];
digest(secretKey: Message, message: Message): number[];

/**
* Return hash in integer array.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
array(secretKey: string, message: Message): number[];
array(secretKey: Message, message: Message): number[];
}

interface Hash {
Expand Down
47 changes: 20 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "js-sha256",
"version": "0.9.0",
"version": "0.10.0",
"description": "A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding.",
"main": "src/sha256.js",
"devDependencies": {
"expect.js": "~0.3.1",
"mocha": "~10.2.0",
"nyc": "^15.1.0",
"uglify-js": "^3.1.9",
"webworker-threads": "^0.7.13"
"tiny-worker": "^2.3.0",
"uglify-js": "^3.1.9"
},
"scripts": {
"test": "nyc mocha tests/node-test.js",
Expand Down
19 changes: 14 additions & 5 deletions src/sha256.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* [js-sha256]{@link https://github.com/emn178/js-sha256}
*
* @version 0.9.0
* @version 0.10.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2017
* @copyright Chen, Yi-Cyuan 2014-2023
* @license MIT
*/
/*jslint bitwise: true */
Expand Down Expand Up @@ -80,9 +80,17 @@
};

var nodeWrap = function (method, is224) {
var crypto = eval("require('crypto')");
var Buffer = eval("require('buffer').Buffer");
var crypto = require('crypto')
var Buffer = require('buffer').Buffer;
var algorithm = is224 ? 'sha224' : 'sha256';
var bufferFrom;
if (Buffer.from && !root.JS_SHA256_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(algorithm).update(message, 'utf8').digest('hex');
Expand All @@ -95,7 +103,7 @@
}
if (Array.isArray(message) || ArrayBuffer.isView(message) ||
message.constructor === Buffer) {
return crypto.createHash(algorithm).update(new Buffer(message)).digest('hex');
return crypto.createHash(algorithm).update(bufferFrom(message)).digest('hex');
} else {
return method(message);
}
Expand Down Expand Up @@ -328,6 +336,7 @@
t2 = s0 + maj;
e = a + t1 << 0;
a = t1 + t2 << 0;
this.chromeBugWorkAround = true;
}

this.h0 = this.h0 + a << 0;
Expand Down
6 changes: 5 additions & 1 deletion tests/node-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
expect = require('expect.js');
Worker = require('webworker-threads').Worker;
Worker = require("tiny-worker");

function unset() {
delete require.cache[require.resolve('../src/sha256.js')];
Expand Down Expand Up @@ -40,6 +40,10 @@ function runWindowTest() {
BUFFER = true;
runCommonJsTest();

// Node.js env, no Buffer.from
JS_SHA256_NO_BUFFER_FROM = true
runCommonJsTest();

// Webpack browser env
JS_SHA256_NO_NODE_JS = true;
window = global;
Expand Down
8 changes: 4 additions & 4 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@

if (typeof BUFFER === 'boolean' && BUFFER) {
testCases.sha256.Buffer = {
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': new Buffer(0),
'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592': 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]))
'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': new Buffer(0),
'730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525': 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]))
'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]))
};
}

Expand Down
3 changes: 3 additions & 0 deletions tests/worker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
var worker = new Worker(WORKER);
worker.onmessage = function(event) {
expect(event.data).to.be(hash);
if (worker.terminate) {
worker.terminate();
}
done();
};
worker.postMessage(SOURCE);
Expand Down
6 changes: 3 additions & 3 deletions tests/worker.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<head>
<meta charset="UTF-8">
<title>SHA256</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>
<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>
Expand Down

0 comments on commit bb8ae3a

Please sign in to comment.