Compare commits

..
7 Commits
Author SHA1 Message Date
Yi-Cyuan Chen c31830ad54 ## v0.10.1 / 2023-08-31
### Added
- Disable webpack polyfill.
2023-08-31 09:44:37 +08:00
Yi-Cyuan Chen f490ee426a bump bower version 2023-08-30 23:34:49 +08:00
Yi-Cyuan Chen bb8ae3a623 ## 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
2023-08-30 22:58:30 +08:00
dependabot[bot]GitHubdependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
16b101f040 Bump minimist, mocha and nyc (#36)
Removes [minimist](https://github.com/minimistjs/minimist). It's no longer used after updating ancestor dependencies [minimist](https://github.com/minimistjs/minimist), [mocha](https://github.com/mochajs/mocha) and [nyc](https://github.com/istanbuljs/nyc). These dependencies need to be updated together.


Removes `minimist`

Updates `mocha` from 2.3.4 to 10.2.0
- [Release notes](https://github.com/mochajs/mocha/releases)
- [Changelog](https://github.com/mochajs/mocha/blob/master/CHANGELOG.md)
- [Commits](https://github.com/mochajs/mocha/compare/2.3.4...v10.2.0)

Updates `nyc` from 11.3.0 to 15.1.0
- [Release notes](https://github.com/istanbuljs/nyc/releases)
- [Changelog](https://github.com/istanbuljs/nyc/blob/master/CHANGELOG.md)
- [Commits](https://github.com/istanbuljs/nyc/compare/v11.3.0...v15.1.0)

---
updated-dependencies:
- dependency-name: minimist
  dependency-type: indirect
- dependency-name: mocha
  dependency-type: direct:development
- dependency-name: nyc
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-08-27 15:02:58 +08:00
Yi-Cyuan Chen 189bb9b037 Fixed
- incorrect result when first bit is 1 of bytes.

Changed
- throw error by Error oject. #13

Added
- TypeScript interfaces. #12
2017-12-18 20:03:12 +08:00
Olegandemn178 e35c679c2b Throw Errors to improve error reporting (#13) 2017-12-13 20:49:51 +08:00
shiyuesongandemn178 bf155ab82f Added "hmac" to interface Hash. (#12) 2017-12-13 20:49:21 +08:00
13 changed files with 1932 additions and 1704 deletions
+24
View File
@@ -1,5 +1,29 @@
# Change Log # Change Log
## 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
### Fixed
- incorrect result when first bit is 1 of bytes.
### Changed
- throw error by Error oject. #13
### Added
- TypeScript interfaces. #12
## v0.8.0 / 2017-11-19 ## v0.8.0 / 2017-11-19
### Added ### Added
- support for web worker. - support for web worker.
+1 -1
View File
@@ -1,4 +1,4 @@
Copyright (c) 2014-2017 Chen, Yi-Cyuan Copyright (c) 2014-2023 Chen, Yi-Cyuan
MIT License MIT License
+12 -4
View File
@@ -48,6 +48,8 @@ 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');
@@ -57,16 +59,22 @@ or
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
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "js-sha256", "name": "js-sha256",
"version": "0.8.0", "version": "0.10.1",
"main": ["src/sha256.js"], "main": ["src/sha256.js"],
"ignore": [ "ignore": [
"samples", "samples",
+3 -3
View File
File diff suppressed because one or more lines are too long
Vendored
+90
View File
@@ -34,6 +34,63 @@ interface Hasher {
array(): number[]; array(): number[];
} }
interface Hmac {
/**
* Computes a Hash-based message authentication code (HMAC) using a secret key
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
(secretKey: Message, message: Message): string;
/**
* Create a hash object using a secret key.
*
* @param secretKey The Secret Key
*/
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: Message, message: Message): Hasher;
/**
* Return hash in hex string.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
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: Message, message: Message): ArrayBuffer;
/**
* Return hash in integer array.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
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: Message, message: Message): number[];
}
interface Hash { interface Hash {
/** /**
* Hash and return hex string. * Hash and return hex string.
@@ -53,6 +110,39 @@ interface Hash {
* @param message The message you want to hash. * @param message The message you want to hash.
*/ */
update(message: Message): Hasher; update(message: Message): Hasher;
/**
* Return hash in hex string.
*
* @param message The message you want to hash.
*/
hex(message: Message): string;
/**
* Return hash in ArrayBuffer.
*
* @param message The message you want to hash.
*/
arrayBuffer(message: Message): ArrayBuffer;
/**
* Return hash in integer array.
*
* @param message The message you want to hash.
*/
digest(message: Message): number[];
/**
* Return hash in integer array.
*
* @param message The message you want to hash.
*/
array(message: Message): number[];
/**
* HMAC interface
*/
hmac: Hmac;
} }
export var sha256: Hash; export var sha256: Hash;
+1754 -1668
View File
File diff suppressed because it is too large Load Diff
+9 -5
View File
@@ -1,14 +1,14 @@
{ {
"name": "js-sha256", "name": "js-sha256",
"version": "0.8.0", "version": "0.10.1",
"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",
@@ -40,5 +40,9 @@
"exclude": [ "exclude": [
"tests" "tests"
] ]
},
"browser": {
"crypto": false,
"buffer": false
} }
} }
+22 -13
View File
@@ -1,9 +1,9 @@
/** /**
* [js-sha256]{@link https://github.com/emn178/js-sha256} * [js-sha256]{@link https://github.com/emn178/js-sha256}
* *
* @version 0.8.0 * @version 0.10.1
* @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,22 +80,30 @@
}; };
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');
} else { } else {
if (message === null || message === undefined) { if (message === null || message === undefined) {
throw ERROR; throw new Error(ERROR);
} else if (message.constructor === ArrayBuffer) { } else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message); message = new Uint8Array(message);
} }
} }
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);
} }
@@ -169,16 +177,16 @@
if (type !== 'string') { if (type !== 'string') {
if (type === 'object') { if (type === 'object') {
if (message === null) { if (message === null) {
throw ERROR; throw new Error(ERROR);
} else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) { } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
message = new Uint8Array(message); message = new Uint8Array(message);
} else if (!Array.isArray(message)) { } else if (!Array.isArray(message)) {
if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) { if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
throw ERROR; throw new Error(ERROR);
} }
} }
} else { } else {
throw ERROR; throw new Error(ERROR);
} }
notString = true; notString = true;
} }
@@ -257,7 +265,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;
} }
blocks[14] = this.hBytes << 3 | this.bytes >> 29; blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
blocks[15] = this.bytes << 3; blocks[15] = this.bytes << 3;
this.hash(); this.hash();
}; };
@@ -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;
@@ -453,16 +462,16 @@
} else { } else {
if (type === 'object') { if (type === 'object') {
if (key === null) { if (key === null) {
throw ERROR; throw new Error(ERROR);
} else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) { } else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
key = new Uint8Array(key); key = new Uint8Array(key);
} else if (!Array.isArray(key)) { } else if (!Array.isArray(key)) {
if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) { if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
throw ERROR; throw new Error(ERROR);
} }
} }
} else { } else {
throw ERROR; throw new Error(ERROR);
} }
} }
+5 -1
View File
@@ -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
View File
@@ -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]))
}; };
} }
+3
View File
@@ -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
View File
@@ -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>