Compare commits

..
4 Commits
Author SHA1 Message Date
Yi-Cyuan Chen dbb0ea401f ## v0.9.0 / 2023-08-30
### Fixed
- cSHAKE bug. #24
- dependencies and security issues.
2023-08-30 23:39:16 +08:00
dependabot[bot]GitHubdependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
5aecbd6968 Bump minimist, mocha and nyc (#28)
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 10:40:15 +08:00
Yi-Cyuan Chen b39d0910cb - Added TypeScript definitions.
- Changed throw error if update after finalize
2018-08-05 15:33:12 +08:00
Micah Zoltuandemn178 1c00d97268 Adds arrayBuffer and buffer definitions. (#16)
This is based entirely on the examples in the readme.  If the examples in the readme are incorrect then this definition is also incorrect.
2018-08-02 19:45:40 +08:00
13 changed files with 2004 additions and 1708 deletions
+12
View File
@@ -1,5 +1,17 @@
# Change Log # Change Log
## v0.9.0 / 2023-08-30
### Fixed
- cSHAKE bug. #24
- dependencies and security issues.
## v0.8.0 / 2018-08-05
### Added
- TypeScript definitions.
### Changed
- throw error if update after finalize
## v0.7.0 / 2017-12-01 ## v0.7.0 / 2017-12-01
### Added ### Added
- AMD support. - AMD support.
+1 -1
View File
@@ -1,4 +1,4 @@
Copyright 2015-2017 Chen, Yi-Cyuan Copyright 2015-2023 Chen, Yi-Cyuan
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the
+39 -17
View File
@@ -7,6 +7,7 @@
A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding. A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.
## Notice ## Notice
* v0.8.0+ will throw an error if try to update hash after finalize.
* Sha3 methods has been renamed to keccak since v0.2.0. It means that sha3 methods of v0.1.x are equal to keccak methods of v0.2.x and later. * Sha3 methods has been renamed to keccak since v0.2.0. It means that sha3 methods of v0.1.x are equal to keccak methods of v0.2.x and later.
* `buffer` method is deprecated. This maybe confuse with Buffer in node.js. Please use `arrayBuffer` instead. * `buffer` method is deprecated. This maybe confuse with Buffer in node.js. Please use `arrayBuffer` instead.
@@ -54,10 +55,11 @@ kmac128('key', 'Message to hash', 256, 'customization');
kmac256('key', 'Message to hash', 512, 'customization'); kmac256('key', 'Message to hash', 512, 'customization');
// Support ArrayBuffer output // Support ArrayBuffer output
var buffer = keccak224.buffer('Message to hash'); var arrayBuffer = keccak224.arrayBuffer('Message to hash');
// Support Array output // Support Array output
var buffer = keccak224.array('Message to hash'); var bytes = keccak224.digest('Message to hash');
var bytes = keccak224.array('Message to hash');
// update hash // update hash
sha3_512.update('Message ').update('to ').update('hash').hex(); sha3_512.update('Message ').update('to ').update('hash').hex();
@@ -82,26 +84,46 @@ var hash = cshake128.create(256, 'function name', 'customization');
// specify kmac key, output bits and customization when creating // specify kmac key, output bits and customization when creating
var hash = kmac128.create('key', 256, 'customization'); var hash = kmac128.create('key', 256, 'customization');
``` ```
### 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
sha3_512 = require('js-sha3').sha3_512; const {
sha3_384 = require('js-sha3').sha3_384; sha3_512,
sha3_256 = require('js-sha3').sha3_256; sha3_384,
sha3_224 = require('js-sha3').sha3_224; sha3_256,
keccak512 = require('js-sha3').keccak512; sha3_224,
keccak384 = require('js-sha3').keccak384; keccak512,
keccak256 = require('js-sha3').keccak256; keccak384,
keccak224 = require('js-sha3').keccak224; keccak256,
shake128 = require('js-sha3').shake128; keccak224,
shake256 = require('js-sha3').shake256; shake128,
cshake128 = require('js-sha3').cshake128; shake256,
cshake256 = require('js-sha3').cshake256; cshake128,
kmac128 = require('js-sha3').kmac128; cshake256,
kmac256 = require('js-sha3').kmac256; kmac128,
kmac25
} = require('js-sha3');
``` ```
### TypeScript
If you use TypeScript, you can import like this: If you use TypeScript, you can import like this:
```TypeScript ```TypeScript
import { sha3_512 } from 'js-sha3'; import {
sha3_512,
sha3_384,
sha3_256,
sha3_224,
keccak512,
keccak384,
keccak256,
keccak224,
shake128,
shake256,
cshake128,
cshake256,
kmac128,
kmac256
} from 'js-sha3';
``` ```
## Example ## Example
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "js-sha3", "name": "js-sha3",
"version": "0.7.0", "version": "0.9.0",
"main": ["src/sha3.js"], "main": ["src/sha3.js"],
"ignore": [ "ignore": [
"samples", "samples",
+3 -3
View File
File diff suppressed because one or more lines are too long
Vendored
+149
View File
@@ -42,6 +42,34 @@ interface Hash {
*/ */
(message: Message): string; (message: Message): string;
/**
* Hash and return hex string.
*
* @param message The message you want to hash.
*/
hex(message: Message): string;
/**
* Hash and return ArrayBuffer.
*
* @param message The message you want to hash.
*/
arrayBuffer(message: Message): ArrayBuffer;
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
*/
digest(message: Message): number[];
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
*/
array(message: Message): number[];
/** /**
* Create a hash object. * Create a hash object.
*/ */
@@ -64,10 +92,43 @@ interface ShakeHash {
*/ */
(message: Message, outputBits: number): string; (message: Message, outputBits: number): string;
/**
* Hash and return hex string.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
*/
hex(message: Message, outputBits: number): string;
/**
* Hash and return ArrayBuffer.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
*/
arrayBuffer(message: Message, outputBits: number): ArrayBuffer;
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
*/
digest(message: Message, outputBits: number): number[];
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
*/
array(message: Message, outputBits: number): number[];
/** /**
* Create a hash object. * Create a hash object.
* *
* @param outputBits The length of output. * @param outputBits The length of output.
* @param outputBits The length of output.
*/ */
create(outputBits: number): Hasher; create(outputBits: number): Hasher;
@@ -91,6 +152,54 @@ interface CshakeHash {
*/ */
(message: Message, outputBits: number, functionName: Message, customization: Message): string; (message: Message, outputBits: number, functionName: Message, customization: Message): string;
/**
* Hash and return hex string.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param functionName The function name string.
* @param customization The customization string.
*/
hex(message: Message, outputBits: number, functionName: Message, customization: Message): string;
/**
* Hash and return ArrayBuffer.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param functionName The function name string.
* @param customization The customization string.
*/
arrayBuffer(message: Message, outputBits: number, functionName: Message, customization: Message): ArrayBuffer;
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param functionName The function name string.
* @param customization The customization string.
*/
digest(message: Message, outputBits: number, functionName: Message, customization: Message): number[];
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param functionName The function name string.
* @param customization The customization string.
*/
array(message: Message, outputBits: number, functionName: Message, customization: Message): number[];
/**
* Create a hash object.
*
* @param outputBits The length of output.
* @param outputBits The length of output.
*/
create(outputBits: number): Hasher;
/** /**
* Create a hash object. * Create a hash object.
* *
@@ -122,6 +231,46 @@ interface KmacHash {
*/ */
(key: Message, message: Message, outputBits: number, customization: Message): string; (key: Message, message: Message, outputBits: number, customization: Message): string;
/**
* Hash and return hex string.
*
* @param key The key string.
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param customization The customization string.
*/
hex(key: Message, message: Message, outputBits: number, customization: Message): string;
/**
* Hash and return ArrayBuffer.
*
* @param key The key string.
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param customization The customization string.
*/
arrayBuffer(key: Message, message: Message, outputBits: number, customization: Message): ArrayBuffer;
/**
* Hash and return integer array.
*
* @param key The key string.
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param customization The customization string.
*/
digest(key: Message, message: Message, outputBits: number, customization: Message): number[];
/**
* Hash and return integer array.
*
* @param key The key string.
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param customization The customization string.
*/
array(key: Message, message: Message, outputBits: number, customization: Message): number[];
/** /**
* Create a hash object. * Create a hash object.
* *
+1759 -1667
View File
File diff suppressed because it is too large Load Diff
+5 -5
View File
@@ -1,14 +1,14 @@
{ {
"name": "js-sha3", "name": "js-sha3",
"version": "0.7.0", "version": "0.9.0",
"description": "A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.", "description": "A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.",
"main": "src/sha3.js", "main": "src/sha3.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",
+12 -11
View File
@@ -1,16 +1,17 @@
/** /**
* [js-sha3]{@link https://github.com/emn178/js-sha3} * [js-sha3]{@link https://github.com/emn178/js-sha3}
* *
* @version 0.7.0 * @version 0.9.0
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2015-2017 * @copyright Chen, Yi-Cyuan 2015-2023
* @license MIT * @license MIT
*/ */
/*jslint bitwise: true */ /*jslint bitwise: true */
(function () { (function () {
'use strict'; 'use strict';
var ERROR = 'input is invalid type'; var INPUT_ERROR = 'input is invalid type';
var FINALIZE_ERROR = 'finalize already called';
var WINDOW = typeof window === 'object'; var WINDOW = typeof window === 'object';
var root = WINDOW ? window : {}; var root = WINDOW ? window : {};
if (root.JS_SHA3_NO_WINDOW) { if (root.JS_SHA3_NO_WINDOW) {
@@ -185,22 +186,22 @@
Keccak.prototype.update = function (message) { Keccak.prototype.update = function (message) {
if (this.finalized) { if (this.finalized) {
return; throw new Error(FINALIZE_ERROR);
} }
var notString, type = typeof message; var notString, type = typeof message;
if (type !== 'string') { if (type !== 'string') {
if (type === 'object') { if (type === 'object') {
if (message === null) { if (message === null) {
throw ERROR; throw new Error(INPUT_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(INPUT_ERROR);
} }
} }
} else { } else {
throw ERROR; throw new Error(INPUT_ERROR);
} }
notString = true; notString = true;
} }
@@ -281,16 +282,16 @@
if (type !== 'string') { if (type !== 'string') {
if (type === 'object') { if (type === 'object') {
if (str === null) { if (str === null) {
throw ERROR; throw new Error(INPUT_ERROR);
} else if (ARRAY_BUFFER && str.constructor === ArrayBuffer) { } else if (ARRAY_BUFFER && str.constructor === ArrayBuffer) {
str = new Uint8Array(str); str = new Uint8Array(str);
} else if (!Array.isArray(str)) { } else if (!Array.isArray(str)) {
if (!ARRAY_BUFFER || !ArrayBuffer.isView(str)) { if (!ARRAY_BUFFER || !ArrayBuffer.isView(str)) {
throw ERROR; throw new Error(INPUT_ERROR);
} }
} }
} else { } else {
throw ERROR; throw new Error(INPUT_ERROR);
} }
notString = true; notString = true;
} }
@@ -322,7 +323,7 @@
for (var i = 0; i < strs.length; ++i) { for (var i = 0; i < strs.length; ++i) {
bytes += this.encodeString(strs[i]); bytes += this.encodeString(strs[i]);
} }
var paddingBytes = w - bytes % w; var paddingBytes = (w - bytes % w) % w;
var zeros = []; var zeros = [];
zeros.length = paddingBytes; zeros.length = paddingBytes;
this.update(zeros); this.update(zeros);
+1 -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/sha3.js')]; delete require.cache[require.resolve('../src/sha3.js')];
+7
View File
@@ -94,6 +94,13 @@
key: [0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F], key: [0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F],
s: 'My Tagged Application', s: 'My Tagged Application',
output: 'b58618f71f92e1d56c1b8c55ddd7cd188b97b4ca4d99831eb2699a837da2e4d970fbacfde50033aea585f1a2708510c32d07880801bd182898fe476876fc8965' output: 'b58618f71f92e1d56c1b8c55ddd7cd188b97b4ca4d99831eb2699a837da2e4d970fbacfde50033aea585f1a2708510c32d07880801bd182898fe476876fc8965'
},
{
input: [],
bits: 128,
key: [],
s: [8, 79, 237, 8, 185, 120, 175, 77, 125, 25, 106, 116, 70, 168, 107, 88, 0, 158, 99, 107, 97, 29, 177, 98, 17, 182, 90, 154, 173, 255, 41, 197, 8, 79, 237, 8, 185, 120, 175, 77, 125, 25, 106, 116, 70, 168, 107, 88, 0, 158, 99, 107, 97, 29, 177, 98, 17, 182, 90, 154, 173, 255, 41, 197, 8, 79, 237, 8, 185, 120, 175, 77, 125, 25, 106, 116, 70, 168, 107, 88, 0, 158, 99, 107, 97, 29, 177, 98, 17, 182, 90, 154, 173, 255, 41, 197, 8, 79, 237, 8, 185, 120, 175, 77, 125, 25, 106, 116, 70, 168, 107, 88, 0, 158, 99, 107, 97, 29, 177, 98, 17, 182, 90, 154, 173],
output: '031801b0b50ebeef772fbe7a279bc144'
} }
] ]
} }
+11 -1
View File
@@ -264,7 +264,7 @@
call: function (message) { call: function (message) {
var hash = algorithm.update(message); var hash = algorithm.update(message);
hash.hex(); hash.hex();
hash.update(message); hash.finalize();
return hash.hex(); return hash.hex();
} }
} }
@@ -321,6 +321,16 @@
}); });
}); });
}); });
context('when update after finalize', function () {
it('should throw error', function () {
expect(function () {
var hash = algorithm.update('any');
hash.hex();
hash.update('any');
}).to.throwError(/finalize already called/);
});
});
}); });
}); });
} }
+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);