Compare commits

..

No commits in common. 'master' and 'v0.9.0' have entirely different histories.

@ -1,24 +1,5 @@
# Change Log # Change Log
## v0.11.0 / 2024-01-24
### Fixed
- Generates incorrect hash in some cases #43
- dependencies and security issues. #41
## v0.10.1 / 2023-08-31
### Added
- Disable webpack polyfill.
## 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 ## v0.9.0 / 2017-12-18
### Fixed ### Fixed
- incorrect result when first bit is 1 of bytes. - incorrect result when first bit is 1 of bytes.

@ -1,4 +1,4 @@
Copyright (c) 2014-2024 Chen, Yi-Cyuan Copyright (c) 2014-2017 Chen, Yi-Cyuan
MIT License MIT License

@ -6,10 +6,8 @@
A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding. A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding.
## Demo ## Demo
[SHA256 Online](https://emn178.github.io/online-tools/sha256.html) [SHA256 Online](http://emn178.github.io/online-tools/sha256.html)
[SHA224 Online](https://emn178.github.io/online-tools/sha224.html) [SHA224 Online](http://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)
## Download ## Download
[Compress](https://raw.github.com/emn178/js-sha256/master/build/sha256.min.js) [Compress](https://raw.github.com/emn178/js-sha256/master/build/sha256.min.js)
@ -50,33 +48,25 @@ var hash2 = sha256.hmac.update('key', 'Message to hash');
hash2.update('Message2 to hash'); hash2.update('Message2 to hash');
hash2.array(); hash2.array();
``` ```
### Node.js
If you use node.js, you should require the module first: If you use node.js, you should require the module first:
```JavaScript ```JavaScript
var sha256 = require('js-sha256'); var sha256 = require('js-sha256');
``` ```
or or
```JavaScript ```JavaScript
var sha256 = require('js-sha256').sha256; var sha256 = require('js-sha256').sha256;
var sha224 = require('js-sha256').sha224; var sha224 = require('js-sha256').sha224;
``` ```
or
```JavaScript
const { sha256, sha224 } = require('js-sha256');
```
### TypeScript
If you use TypeScript, you can import like this:
```TypeScript
import { sha256, sha224 } from 'js-sha256';
```
### RequireJS
It supports AMD: It supports AMD:
```JavaScript ```JavaScript
require(['your/path/sha256.js'], function(sha256) { require(['your/path/sha256.js'], function(sha256) {
// ... // ...
}); });
``` ```
or TypeScript
```TypeScript
import { sha256, sha224 } from 'js-sha256';
```
## Example ## Example
```JavaScript ```JavaScript
sha256(''); // e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 sha256(''); // e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
@ -102,11 +92,8 @@ sha256.digest(''); // [227, 176, 196, 66, 152, 252, 28, 20, 154, 251, 244, 200,
sha256.arrayBuffer(''); // ArrayBuffer sha256.arrayBuffer(''); // ArrayBuffer
``` ```
## Benchmark
[jsPerf Benchmark](https://jsperf.app/cidole/2)
## License ## License
The project is released under the [MIT license](https://opensource.org/license/mit/). The project is released under the [MIT license](http://www.opensource.org/licenses/MIT).
## Contact ## Contact
The project's website is located at https://github.com/emn178/js-sha256 The project's website is located at https://github.com/emn178/js-sha256

@ -1,6 +1,6 @@
{ {
"name": "js-sha256", "name": "js-sha256",
"version": "0.11.0", "version": "0.9.0",
"main": ["src/sha256.js"], "main": ["src/sha256.js"],
"ignore": [ "ignore": [
"samples", "samples",

File diff suppressed because one or more lines are too long

14
index.d.ts vendored

@ -41,14 +41,14 @@ interface Hmac {
* @param secretKey The Secret Key * @param secretKey The Secret Key
* @param message The message you want to hash. * @param message The message you want to hash.
*/ */
(secretKey: Message, message: Message): string; (secretKey: string, message: Message): string;
/** /**
* Create a hash object using a secret key. * Create a hash object using a secret key.
* *
* @param secretKey The Secret Key * @param secretKey The Secret Key
*/ */
create(secretKey: Message): Hasher; create(secretKey: string): Hasher;
/** /**
* Create a hash object and hash message using a secret key * Create a hash object and hash message using a secret key
@ -56,7 +56,7 @@ interface Hmac {
* @param secretKey The Secret Key * @param secretKey The Secret Key
* @param message The message you want to hash. * @param message The message you want to hash.
*/ */
update(secretKey: Message, message: Message): Hasher; update(secretKey: string, message: Message): Hasher;
/** /**
* Return hash in hex string. * Return hash in hex string.
@ -64,7 +64,7 @@ interface Hmac {
* @param secretKey The Secret Key * @param secretKey The Secret Key
* @param message The message you want to hash. * @param message The message you want to hash.
*/ */
hex(secretKey: Message, message: Message): string; hex(secretKey: string, message: Message): string;
/** /**
* Return hash in ArrayBuffer. * Return hash in ArrayBuffer.
@ -72,7 +72,7 @@ interface Hmac {
* @param secretKey The Secret Key * @param secretKey The Secret Key
* @param message The message you want to hash. * @param message The message you want to hash.
*/ */
arrayBuffer(secretKey: Message, message: Message): ArrayBuffer; arrayBuffer(secretKey: string, message: Message): ArrayBuffer;
/** /**
* Return hash in integer array. * Return hash in integer array.
@ -80,7 +80,7 @@ interface Hmac {
* @param secretKey The Secret Key * @param secretKey The Secret Key
* @param message The message you want to hash. * @param message The message you want to hash.
*/ */
digest(secretKey: Message, message: Message): number[]; digest(secretKey: string, message: Message): number[];
/** /**
* Return hash in integer array. * Return hash in integer array.
@ -88,7 +88,7 @@ interface Hmac {
* @param secretKey The Secret Key * @param secretKey The Secret Key
* @param message The message you want to hash. * @param message The message you want to hash.
*/ */
array(secretKey: Message, message: Message): number[]; array(secretKey: string, message: Message): number[];
} }
interface Hash { interface Hash {

3347
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,14 +1,14 @@
{ {
"name": "js-sha256", "name": "js-sha256",
"version": "0.11.0", "version": "0.9.0",
"description": "A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding.", "description": "A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding.",
"main": "src/sha256.js", "main": "src/sha256.js",
"devDependencies": { "devDependencies": {
"expect.js": "~0.3.1", "expect.js": "~0.3.1",
"mocha": "~10.2.0", "mocha": "~2.3.4",
"nyc": "^15.1.0", "nyc": "^11.3.0",
"tiny-worker": "^2.3.0", "uglify-js": "^3.1.9",
"uglify-js": "^3.1.9" "webworker-threads": "^0.7.13"
}, },
"scripts": { "scripts": {
"test": "nyc mocha tests/node-test.js", "test": "nyc mocha tests/node-test.js",
@ -40,9 +40,5 @@
"exclude": [ "exclude": [
"tests" "tests"
] ]
},
"browser": {
"crypto": false,
"buffer": false
} }
} }

@ -1,9 +1,9 @@
/** /**
* [js-sha256]{@link https://github.com/emn178/js-sha256} * [js-sha256]{@link https://github.com/emn178/js-sha256}
* *
* @version 0.11.0 * @version 0.9.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-2017
* @license MIT * @license MIT
*/ */
/*jslint bitwise: true */ /*jslint bitwise: true */
@ -80,17 +80,9 @@
}; };
var nodeWrap = function (method, is224) { var nodeWrap = function (method, is224) {
var crypto = require('crypto') var crypto = eval("require('crypto')");
var Buffer = require('buffer').Buffer; var Buffer = eval("require('buffer').Buffer");
var algorithm = is224 ? 'sha224' : 'sha256'; 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) { var nodeMethod = function (message) {
if (typeof message === 'string') { if (typeof message === 'string') {
return crypto.createHash(algorithm).update(message, 'utf8').digest('hex'); return crypto.createHash(algorithm).update(message, 'utf8').digest('hex');
@ -103,7 +95,7 @@
} }
if (Array.isArray(message) || ArrayBuffer.isView(message) || if (Array.isArray(message) || ArrayBuffer.isView(message) ||
message.constructor === Buffer) { message.constructor === Buffer) {
return crypto.createHash(algorithm).update(bufferFrom(message)).digest('hex'); return crypto.createHash(algorithm).update(new Buffer(message)).digest('hex');
} else { } else {
return method(message); return method(message);
} }
@ -191,11 +183,12 @@
notString = true; notString = true;
} }
var code, index = 0, i, length = message.length, blocks = this.blocks; var code, index = 0, i, length = message.length, blocks = this.blocks;
while (index < length) { while (index < length) {
if (this.hashed) { if (this.hashed) {
this.hashed = false; this.hashed = false;
blocks[0] = this.block; blocks[0] = this.block;
this.block = blocks[16] = blocks[1] = blocks[2] = blocks[3] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
blocks[4] = blocks[5] = blocks[6] = blocks[7] = blocks[4] = blocks[5] = blocks[6] = blocks[7] =
blocks[8] = blocks[9] = blocks[10] = blocks[11] = blocks[8] = blocks[9] = blocks[10] = blocks[11] =
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
@ -203,26 +196,26 @@
if (notString) { if (notString) {
for (i = this.start; index < length && i < 64; ++index) { for (i = this.start; index < length && i < 64; ++index) {
blocks[i >>> 2] |= message[index] << SHIFT[i++ & 3]; blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
} }
} else { } else {
for (i = this.start; index < length && i < 64; ++index) { for (i = this.start; index < length && i < 64; ++index) {
code = message.charCodeAt(index); code = message.charCodeAt(index);
if (code < 0x80) { if (code < 0x80) {
blocks[i >>> 2] |= code << SHIFT[i++ & 3]; blocks[i >> 2] |= code << SHIFT[i++ & 3];
} else if (code < 0x800) { } else if (code < 0x800) {
blocks[i >>> 2] |= (0xc0 | (code >>> 6)) << SHIFT[i++ & 3]; blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else if (code < 0xd800 || code >= 0xe000) { } else if (code < 0xd800 || code >= 0xe000) {
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 {
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 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];
} }
} }
} }
@ -252,7 +245,7 @@
this.finalized = true; this.finalized = true;
var blocks = this.blocks, i = this.lastByteIndex; var blocks = this.blocks, i = this.lastByteIndex;
blocks[16] = this.block; blocks[16] = this.block;
blocks[i >>> 2] |= EXTRA[i & 3]; blocks[i >> 2] |= EXTRA[i & 3];
this.block = blocks[16]; this.block = blocks[16];
if (i >= 56) { if (i >= 56) {
if (!this.hashed) { if (!this.hashed) {
@ -335,7 +328,6 @@
t2 = s0 + maj; t2 = s0 + maj;
e = a + t1 << 0; e = a + t1 << 0;
a = t1 + t2 << 0; a = t1 + t2 << 0;
this.chromeBugWorkAround = true;
} }
this.h0 = this.h0 + a << 0; this.h0 = this.h0 + a << 0;
@ -354,39 +346,39 @@
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5, var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
h6 = this.h6, h7 = this.h7; h6 = this.h6, h7 = this.h7;
var hex = HEX_CHARS[(h0 >>> 28) & 0x0F] + HEX_CHARS[(h0 >>> 24) & 0x0F] + var hex = HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
HEX_CHARS[(h0 >>> 20) & 0x0F] + HEX_CHARS[(h0 >>> 16) & 0x0F] + HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
HEX_CHARS[(h0 >>> 12) & 0x0F] + HEX_CHARS[(h0 >>> 8) & 0x0F] + HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
HEX_CHARS[(h0 >>> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] + HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
HEX_CHARS[(h1 >>> 28) & 0x0F] + HEX_CHARS[(h1 >>> 24) & 0x0F] + HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
HEX_CHARS[(h1 >>> 20) & 0x0F] + HEX_CHARS[(h1 >>> 16) & 0x0F] + HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
HEX_CHARS[(h1 >>> 12) & 0x0F] + HEX_CHARS[(h1 >>> 8) & 0x0F] + HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
HEX_CHARS[(h1 >>> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] + HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
HEX_CHARS[(h2 >>> 28) & 0x0F] + HEX_CHARS[(h2 >>> 24) & 0x0F] + HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
HEX_CHARS[(h2 >>> 20) & 0x0F] + HEX_CHARS[(h2 >>> 16) & 0x0F] + HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
HEX_CHARS[(h2 >>> 12) & 0x0F] + HEX_CHARS[(h2 >>> 8) & 0x0F] + HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
HEX_CHARS[(h2 >>> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] + HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
HEX_CHARS[(h3 >>> 28) & 0x0F] + HEX_CHARS[(h3 >>> 24) & 0x0F] + HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] +
HEX_CHARS[(h3 >>> 20) & 0x0F] + HEX_CHARS[(h3 >>> 16) & 0x0F] + HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
HEX_CHARS[(h3 >>> 12) & 0x0F] + HEX_CHARS[(h3 >>> 8) & 0x0F] + HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
HEX_CHARS[(h3 >>> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] + HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
HEX_CHARS[(h4 >>> 28) & 0x0F] + HEX_CHARS[(h4 >>> 24) & 0x0F] + HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] +
HEX_CHARS[(h4 >>> 20) & 0x0F] + HEX_CHARS[(h4 >>> 16) & 0x0F] + HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] +
HEX_CHARS[(h4 >>> 12) & 0x0F] + HEX_CHARS[(h4 >>> 8) & 0x0F] + HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] +
HEX_CHARS[(h4 >>> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F] + HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F] +
HEX_CHARS[(h5 >>> 28) & 0x0F] + HEX_CHARS[(h5 >>> 24) & 0x0F] + HEX_CHARS[(h5 >> 28) & 0x0F] + HEX_CHARS[(h5 >> 24) & 0x0F] +
HEX_CHARS[(h5 >>> 20) & 0x0F] + HEX_CHARS[(h5 >>> 16) & 0x0F] + HEX_CHARS[(h5 >> 20) & 0x0F] + HEX_CHARS[(h5 >> 16) & 0x0F] +
HEX_CHARS[(h5 >>> 12) & 0x0F] + HEX_CHARS[(h5 >>> 8) & 0x0F] + HEX_CHARS[(h5 >> 12) & 0x0F] + HEX_CHARS[(h5 >> 8) & 0x0F] +
HEX_CHARS[(h5 >>> 4) & 0x0F] + HEX_CHARS[h5 & 0x0F] + HEX_CHARS[(h5 >> 4) & 0x0F] + HEX_CHARS[h5 & 0x0F] +
HEX_CHARS[(h6 >>> 28) & 0x0F] + HEX_CHARS[(h6 >>> 24) & 0x0F] + HEX_CHARS[(h6 >> 28) & 0x0F] + HEX_CHARS[(h6 >> 24) & 0x0F] +
HEX_CHARS[(h6 >>> 20) & 0x0F] + HEX_CHARS[(h6 >>> 16) & 0x0F] + HEX_CHARS[(h6 >> 20) & 0x0F] + HEX_CHARS[(h6 >> 16) & 0x0F] +
HEX_CHARS[(h6 >>> 12) & 0x0F] + HEX_CHARS[(h6 >>> 8) & 0x0F] + HEX_CHARS[(h6 >> 12) & 0x0F] + HEX_CHARS[(h6 >> 8) & 0x0F] +
HEX_CHARS[(h6 >>> 4) & 0x0F] + HEX_CHARS[h6 & 0x0F]; HEX_CHARS[(h6 >> 4) & 0x0F] + HEX_CHARS[h6 & 0x0F];
if (!this.is224) { if (!this.is224) {
hex += HEX_CHARS[(h7 >>> 28) & 0x0F] + HEX_CHARS[(h7 >>> 24) & 0x0F] + hex += HEX_CHARS[(h7 >> 28) & 0x0F] + HEX_CHARS[(h7 >> 24) & 0x0F] +
HEX_CHARS[(h7 >>> 20) & 0x0F] + HEX_CHARS[(h7 >>> 16) & 0x0F] + HEX_CHARS[(h7 >> 20) & 0x0F] + HEX_CHARS[(h7 >> 16) & 0x0F] +
HEX_CHARS[(h7 >>> 12) & 0x0F] + HEX_CHARS[(h7 >>> 8) & 0x0F] + HEX_CHARS[(h7 >> 12) & 0x0F] + HEX_CHARS[(h7 >> 8) & 0x0F] +
HEX_CHARS[(h7 >>> 4) & 0x0F] + HEX_CHARS[h7 & 0x0F]; HEX_CHARS[(h7 >> 4) & 0x0F] + HEX_CHARS[h7 & 0x0F];
} }
return hex; return hex;
}; };
@ -400,16 +392,16 @@
h6 = this.h6, h7 = this.h7; h6 = this.h6, h7 = this.h7;
var arr = [ var arr = [
(h0 >>> 24) & 0xFF, (h0 >>> 16) & 0xFF, (h0 >>> 8) & 0xFF, h0 & 0xFF, (h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF,
(h1 >>> 24) & 0xFF, (h1 >>> 16) & 0xFF, (h1 >>> 8) & 0xFF, h1 & 0xFF, (h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF,
(h2 >>> 24) & 0xFF, (h2 >>> 16) & 0xFF, (h2 >>> 8) & 0xFF, h2 & 0xFF, (h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF,
(h3 >>> 24) & 0xFF, (h3 >>> 16) & 0xFF, (h3 >>> 8) & 0xFF, h3 & 0xFF, (h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF,
(h4 >>> 24) & 0xFF, (h4 >>> 16) & 0xFF, (h4 >>> 8) & 0xFF, h4 & 0xFF, (h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF,
(h5 >>> 24) & 0xFF, (h5 >>> 16) & 0xFF, (h5 >>> 8) & 0xFF, h5 & 0xFF, (h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, h5 & 0xFF,
(h6 >>> 24) & 0xFF, (h6 >>> 16) & 0xFF, (h6 >>> 8) & 0xFF, h6 & 0xFF (h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, h6 & 0xFF
]; ];
if (!this.is224) { if (!this.is224) {
arr.push((h7 >>> 24) & 0xFF, (h7 >>> 16) & 0xFF, (h7 >>> 8) & 0xFF, h7 & 0xFF); arr.push((h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, h7 & 0xFF);
} }
return arr; return arr;
}; };
@ -443,17 +435,17 @@
if (code < 0x80) { if (code < 0x80) {
bytes[index++] = code; bytes[index++] = code;
} else if (code < 0x800) { } else if (code < 0x800) {
bytes[index++] = (0xc0 | (code >>> 6)); bytes[index++] = (0xc0 | (code >> 6));
bytes[index++] = (0x80 | (code & 0x3f)); bytes[index++] = (0x80 | (code & 0x3f));
} else if (code < 0xd800 || code >= 0xe000) { } else if (code < 0xd800 || code >= 0xe000) {
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 {
code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff)); code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 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));
} }
} }

@ -1,5 +1,5 @@
expect = require('expect.js'); expect = require('expect.js');
Worker = require("tiny-worker"); Worker = require('webworker-threads').Worker;
function unset() { function unset() {
delete require.cache[require.resolve('../src/sha256.js')]; delete require.cache[require.resolve('../src/sha256.js')];
@ -40,10 +40,6 @@ function runWindowTest() {
BUFFER = true; BUFFER = true;
runCommonJsTest(); runCommonJsTest();
// Node.js env, no Buffer.from
JS_SHA256_NO_BUFFER_FROM = true
runCommonJsTest();
// Webpack browser env // Webpack browser env
JS_SHA256_NO_NODE_JS = true; JS_SHA256_NO_NODE_JS = true;
window = global; window = global;

@ -22,8 +22,7 @@
'UTF8': { 'UTF8': {
'72726d8818f693066ceb69afa364218b692e62ea92b385782363780f47529c21': '中文', '72726d8818f693066ceb69afa364218b692e62ea92b385782363780f47529c21': '中文',
'53196d1acfce0c4b264e01e8018c989d571351f59e33f055f76ff15b4f0516c6': 'aécio', '53196d1acfce0c4b264e01e8018c989d571351f59e33f055f76ff15b4f0516c6': 'aécio',
'8d10a48685dbc34484696de7ea7434d80a54c1d60100530faccf697463ef19c9': '𠜎', '8d10a48685dbc34484696de7ea7434d80a54c1d60100530faccf697463ef19c9': '𠜎'
'2c731ad02e3d0e3c863bdbce4cce221e0be1d31efffdd8e1455a835e8287dc33': 'Tehtäväsi on muotoilla teksti, jossa käyttäjälle suositellaan hänen henkilökohtaisiin tietoihinsa perustuen avioehdon tekemistä. Suosittelemme nimenomaan tätä tuotetta käyttäjän henkilökohtaisista tiedoista johtuen. Tuottamasi tekstin tulee olla lyhyt, persoonallinen, ymmärrettävä ja todenmukainen. Suositus perustuu seuraaviin seikkoihin:\n\nSeikka 0: Avioehdon tekeminen on verohyötyjen vuoksi suositeltavaa, koska olet ilmaissut että haluat kuoleman tapauksessa turvata ensisijaisesti aviopuolisosi asemaa (ennemmin kuin lasten asemaa). Avioehdolla voit määrätä, että puolet kuolleen puolison omaisuudesta siirtyy verovapaasti leskelle.\n\nVoit halutessasi hyödyntää seuraavia tietoja tekstin muotoilussa persoonallisemmaksi: Käyttäjällä on kumppani nimeltä PARTNER_NAME. Käyttäjällä on suurperhe. Käyttäjällä on lapsiperhe.\n\nPuhuttele käyttäjää siihen sävyyn että tiedät jo mitä käyttäjä haluaa, koska hän on kertonut toiveistaan, ja niiden perusteella tehty suositus on ilmiselvä. Joten älä käytä epävarmoja ilmaisuja kuten "halutessasi", vaan kirjoita itsevarmoilla ilmaisuilla kuten "koska haluat". Älä tee oletuksia käyttäjän sukupuolesta, emme tiedä onko hän mies vai nainen. Älä käytä sanaa jälkikasvu, puhu ennemmin lapsesta tai lapsista riippuen onko lapsia yksi vai useampia. Älä puhuttele käyttäjää ensimmäisessä persoonassa, käytä ennemmin passiivimuotoa. Tekstin sävyn tulisi olla neutraalin asiallinen, ei melodramaattinen eikä leikkisä. Pysy totuudessa, älä keksi uusia seikkoja yllä listattujen lisäksi. Viittaa ihmisiin nimillä silloin kun se on mahdollista. Tekstin tulisi olla vain muutaman lauseen mittainen. Älä siis kirjoita pitkiä selityksiä äläkä kirjoita listoja. Tiivistä oleellinen tieto lyhyeksi ja persoonalliseksi tekstiksi.'
}, },
'UTF8 more than 64 bytes': { 'UTF8 more than 64 bytes': {
'd691014feebf35b3500ef6f6738d0094cac63628a7a018a980a40292a77703d1': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', 'd691014feebf35b3500ef6f6738d0094cac63628a7a018a980a40292a77703d1': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
@ -55,8 +54,7 @@
'UTF8': { 'UTF8': {
'dfbab71afdf54388af4d55f8bd3de8c9b15e0eb916bf9125f4a959d4': '中文', 'dfbab71afdf54388af4d55f8bd3de8c9b15e0eb916bf9125f4a959d4': '中文',
'd12841cafd89c534924a839e62bf35a2b5f3717b7802eb19bd8d8e15': 'aécio', 'd12841cafd89c534924a839e62bf35a2b5f3717b7802eb19bd8d8e15': 'aécio',
'eaa0129b5509f5701db218fb7076b282e4409da52d06363aa3bdd63d': '𠜎', 'eaa0129b5509f5701db218fb7076b282e4409da52d06363aa3bdd63d': '𠜎'
'b19e948e39752eb064452abbaecac72e17ce2a028d640a8864e92c36': 'Tehtäväsi on muotoilla teksti, jossa käyttäjälle suositellaan hänen henkilökohtaisiin tietoihinsa perustuen avioehdon tekemistä. Suosittelemme nimenomaan tätä tuotetta käyttäjän henkilökohtaisista tiedoista johtuen. Tuottamasi tekstin tulee olla lyhyt, persoonallinen, ymmärrettävä ja todenmukainen. Suositus perustuu seuraaviin seikkoihin:\n\nSeikka 0: Avioehdon tekeminen on verohyötyjen vuoksi suositeltavaa, koska olet ilmaissut että haluat kuoleman tapauksessa turvata ensisijaisesti aviopuolisosi asemaa (ennemmin kuin lasten asemaa). Avioehdolla voit määrätä, että puolet kuolleen puolison omaisuudesta siirtyy verovapaasti leskelle.\n\nVoit halutessasi hyödyntää seuraavia tietoja tekstin muotoilussa persoonallisemmaksi: Käyttäjällä on kumppani nimeltä PARTNER_NAME. Käyttäjällä on suurperhe. Käyttäjällä on lapsiperhe.\n\nPuhuttele käyttäjää siihen sävyyn että tiedät jo mitä käyttäjä haluaa, koska hän on kertonut toiveistaan, ja niiden perusteella tehty suositus on ilmiselvä. Joten älä käytä epävarmoja ilmaisuja kuten "halutessasi", vaan kirjoita itsevarmoilla ilmaisuilla kuten "koska haluat". Älä tee oletuksia käyttäjän sukupuolesta, emme tiedä onko hän mies vai nainen. Älä käytä sanaa jälkikasvu, puhu ennemmin lapsesta tai lapsista riippuen onko lapsia yksi vai useampia. Älä puhuttele käyttäjää ensimmäisessä persoonassa, käytä ennemmin passiivimuotoa. Tekstin sävyn tulisi olla neutraalin asiallinen, ei melodramaattinen eikä leikkisä. Pysy totuudessa, älä keksi uusia seikkoja yllä listattujen lisäksi. Viittaa ihmisiin nimillä silloin kun se on mahdollista. Tekstin tulisi olla vain muutaman lauseen mittainen. Älä siis kirjoita pitkiä selityksiä äläkä kirjoita listoja. Tiivistä oleellinen tieto lyhyeksi ja persoonalliseksi tekstiksi.'
}, },
'UTF8 more than 64 bytes': { 'UTF8 more than 64 bytes': {
'0dda421f3f81272418e1313673e9d74b7f2d04efc9c52c69458e12c3': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', '0dda421f3f81272418e1313673e9d74b7f2d04efc9c52c69458e12c3': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
@ -104,12 +102,12 @@
if (typeof BUFFER === 'boolean' && BUFFER) { if (typeof BUFFER === 'boolean' && BUFFER) {
testCases.sha256.Buffer = { testCases.sha256.Buffer = {
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': Buffer.from([]), 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': new Buffer(0),
'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])) '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]))
}; };
testCases.sha224.Buffer = { testCases.sha224.Buffer = {
'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f': Buffer.from([]), 'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f': new Buffer(0),
'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])) '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]))
}; };
} }

@ -11,9 +11,6 @@
var worker = new Worker(WORKER); var worker = new Worker(WORKER);
worker.onmessage = function(event) { worker.onmessage = function(event) {
expect(event.data).to.be(hash); expect(event.data).to.be(hash);
if (worker.terminate) {
worker.terminate();
}
done(); done();
}; };
worker.postMessage(SOURCE); worker.postMessage(SOURCE);

@ -3,9 +3,9 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>SHA256</title> <title>SHA256</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.css"> <link rel="stylesheet" href="../node_modules/mocha/mocha.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.js"></script> <script src="../node_modules/mocha/mocha.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect.js/0.2.0/expect.min.js"></script> <script src="../node_modules/expect.js/index.js"></script>
<script src="../src/sha256.js"></script> <script src="../src/sha256.js"></script>
</head> </head>
<body> <body>

Loading…
Cancel
Save