mirror of
https://github.com/emn178/js-sha256.git
synced 2026-08-12 20:32:16 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb8ae3a623 | ||
|
|
16b101f040 |
@@ -1,5 +1,15 @@
|
|||||||
# Change Log
|
# 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
|
## 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.
|
||||||
|
|||||||
@@ -48,25 +48,33 @@ 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
|
||||||
|
or TypeScript
|
||||||
|
```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
|
||||||
|
|||||||
Vendored
+3
-3
File diff suppressed because one or more lines are too long
Vendored
+7
-7
@@ -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: string, message: Message): string;
|
(secretKey: Message, 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: string): Hasher;
|
create(secretKey: Message): 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: string, message: Message): Hasher;
|
update(secretKey: Message, 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: string, message: Message): string;
|
hex(secretKey: Message, 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: string, message: Message): ArrayBuffer;
|
arrayBuffer(secretKey: Message, 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: string, message: Message): number[];
|
digest(secretKey: Message, 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: string, message: Message): number[];
|
array(secretKey: Message, message: Message): number[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Hash {
|
interface Hash {
|
||||||
|
|||||||
Generated
+1754
-1668
File diff suppressed because it is too large
Load Diff
+5
-5
@@ -1,14 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "js-sha256",
|
"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.",
|
"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": "~2.3.4",
|
"mocha": "~10.2.0",
|
||||||
"nyc": "^11.3.0",
|
"nyc": "^15.1.0",
|
||||||
"uglify-js": "^3.1.9",
|
"tiny-worker": "^2.3.0",
|
||||||
"webworker-threads": "^0.7.13"
|
"uglify-js": "^3.1.9"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "nyc mocha tests/node-test.js",
|
"test": "nyc mocha tests/node-test.js",
|
||||||
|
|||||||
+14
-5
@@ -1,9 +1,9 @@
|
|||||||
/**
|
/**
|
||||||
* [js-sha256]{@link https://github.com/emn178/js-sha256}
|
* [js-sha256]{@link https://github.com/emn178/js-sha256}
|
||||||
*
|
*
|
||||||
* @version 0.9.0
|
* @version 0.10.0
|
||||||
* @author Chen, Yi-Cyuan [emn178@gmail.com]
|
* @author Chen, Yi-Cyuan [emn178@gmail.com]
|
||||||
* @copyright Chen, Yi-Cyuan 2014-2017
|
* @copyright Chen, Yi-Cyuan 2014-2023
|
||||||
* @license MIT
|
* @license MIT
|
||||||
*/
|
*/
|
||||||
/*jslint bitwise: true */
|
/*jslint bitwise: true */
|
||||||
@@ -80,9 +80,17 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
var nodeWrap = function (method, is224) {
|
var nodeWrap = function (method, is224) {
|
||||||
var crypto = eval("require('crypto')");
|
var crypto = require('crypto')
|
||||||
var Buffer = eval("require('buffer').Buffer");
|
var Buffer = 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');
|
||||||
@@ -95,7 +103,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(new Buffer(message)).digest('hex');
|
return crypto.createHash(algorithm).update(bufferFrom(message)).digest('hex');
|
||||||
} else {
|
} else {
|
||||||
return method(message);
|
return method(message);
|
||||||
}
|
}
|
||||||
@@ -328,6 +336,7 @@
|
|||||||
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;
|
||||||
|
|||||||
+5
-1
@@ -1,5 +1,5 @@
|
|||||||
expect = require('expect.js');
|
expect = require('expect.js');
|
||||||
Worker = require('webworker-threads').Worker;
|
Worker = require("tiny-worker");
|
||||||
|
|
||||||
function unset() {
|
function unset() {
|
||||||
delete require.cache[require.resolve('../src/sha256.js')];
|
delete require.cache[require.resolve('../src/sha256.js')];
|
||||||
@@ -40,6 +40,10 @@ 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;
|
||||||
|
|||||||
+4
-4
@@ -102,12 +102,12 @@
|
|||||||
|
|
||||||
if (typeof BUFFER === 'boolean' && BUFFER) {
|
if (typeof BUFFER === 'boolean' && BUFFER) {
|
||||||
testCases.sha256.Buffer = {
|
testCases.sha256.Buffer = {
|
||||||
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': new Buffer(0),
|
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': Buffer.from([]),
|
||||||
'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]))
|
'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 = {
|
testCases.sha224.Buffer = {
|
||||||
'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f': new Buffer(0),
|
'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f': Buffer.from([]),
|
||||||
'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]))
|
'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]))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,9 @@
|
|||||||
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
-3
@@ -3,9 +3,9 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>SHA256</title>
|
<title>SHA256</title>
|
||||||
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.css">
|
||||||
<script src="../node_modules/mocha/mocha.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.js"></script>
|
||||||
<script src="../node_modules/expect.js/index.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>
|
<script src="../src/sha256.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user