Skip to content

Commit

Permalink
## v0.9.1 / 2023-08-31
Browse files Browse the repository at this point in the history
### Fixed
- cSHAKE empty Array bug. #24
  • Loading branch information
emn178 committed Aug 31, 2023
1 parent dbb0ea4 commit f293b5d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 41 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## v0.9.1 / 2023-08-31
### Fixed
- cSHAKE empty Array bug. #24

## v0.9.0 / 2023-08-30
### Fixed
- cSHAKE bug. #24
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-sha3",
"version": "0.9.0",
"version": "0.9.1",
"main": ["src/sha3.js"],
"ignore": [
"samples",
Expand Down
4 changes: 2 additions & 2 deletions build/sha3.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-sha3",
"version": "0.9.0",
"version": "0.9.1",
"description": "A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.",
"main": "src/sha3.js",
"devDependencies": {
Expand Down
69 changes: 33 additions & 36 deletions src/sha3.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* [js-sha3]{@link https://github.com/emn178/js-sha3}
*
* @version 0.9.0
* @version 0.9.1
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2015-2023
* @license MIT
Expand Down Expand Up @@ -58,6 +58,31 @@
};
}

var formatMessage = function (message) {
var notString, type = typeof message;
if (type !== 'string') {
if (type === 'object') {
if (message === null) {
throw new Error(INPUT_ERROR);
} else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
} else if (!Array.isArray(message)) {
if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
throw new Error(INPUT_ERROR);
}
}
} else {
throw new Error(INPUT_ERROR);
}
notString = true;
}
return [message, notString];
}

var empty = function (message) {
return formatMessage(message)[0].length === 0;
};

var createOutputMethod = function (bits, padding, outputType) {
return function (message) {
return new Keccak(bits, padding, bits).update(message)[outputType]();
Expand Down Expand Up @@ -116,7 +141,7 @@
var w = CSHAKE_BYTEPAD[bits];
var method = createCshakeOutputMethod(bits, padding, 'hex');
method.create = function (outputBits, n, s) {
if (!n && !s) {
if (empty(n) && empty(s)) {
return methods['shake' + bits].create(outputBits);
} else {
return new Keccak(bits, padding, outputBits).bytepad([n, s], w);
Expand Down Expand Up @@ -188,23 +213,9 @@
if (this.finalized) {
throw new Error(FINALIZE_ERROR);
}
var notString, type = typeof message;
if (type !== 'string') {
if (type === 'object') {
if (message === null) {
throw new Error(INPUT_ERROR);
} else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
} else if (!Array.isArray(message)) {
if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
throw new Error(INPUT_ERROR);
}
}
} else {
throw new Error(INPUT_ERROR);
}
notString = true;
}
var result = formatMessage(message);
message = result[0];
var notString = result[1];
var blocks = this.blocks, byteCount = this.byteCount, length = message.length,
blockCount = this.blockCount, index = 0, s = this.s, i, code;

Expand Down Expand Up @@ -278,23 +289,9 @@
};

Keccak.prototype.encodeString = function (str) {
var notString, type = typeof str;
if (type !== 'string') {
if (type === 'object') {
if (str === null) {
throw new Error(INPUT_ERROR);
} else if (ARRAY_BUFFER && str.constructor === ArrayBuffer) {
str = new Uint8Array(str);
} else if (!Array.isArray(str)) {
if (!ARRAY_BUFFER || !ArrayBuffer.isView(str)) {
throw new Error(INPUT_ERROR);
}
}
} else {
throw new Error(INPUT_ERROR);
}
notString = true;
}
var result = formatMessage(str);
str = result[0];
var notString = result[1];
var bytes = 0, length = str.length;
if (notString) {
bytes = length;
Expand Down
21 changes: 21 additions & 0 deletions tests/test-cshake.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@
n: '',
s: '',
output: '7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26'
},
{
input: [],
bits: 256,
n: [],
s: [],
output: '7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26'
},
{
input: [],
bits: 256,
n: new ArrayBuffer(0),
s: new ArrayBuffer(0),
output: '7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26'
},
{
input: [],
bits: 256,
n: new Uint8Array([]),
s: new Uint8Array([]),
output: '7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26'
}
]
},
Expand Down

0 comments on commit f293b5d

Please sign in to comment.