Compare commits

..
2 Commits
Author SHA1 Message Date
Yi-Cyuan Chen 59caf17eca bump version 2023-09-16 21:32:37 +08:00
legobeatandGitHub d6325442bd refactor: simplify formatMessage internal logic (#34)
* fix: don't modify global Array and ArrayBuffer prototypes

* wip: simplify formatMessage

* wip: simplify formatMessage internal logic

* wip: simplify formatMessage internal logic

* wip: simplify formatMessage internal logic

* refactor: simplify formatMessage internal logic

* formatMessage: remove double-negation: now returns istring instead of notString

* refactor: remove double-negation of isString

* refactor: remove double-negation of isString

* add comment on formatMessage return type
2023-09-16 21:23:54 +08:00
6 changed files with 48 additions and 45 deletions
+5
View File
@@ -1,5 +1,10 @@
# Change Log # Change Log
## v0.9.2 / 2023-09-16
### Fixed
- don't modify global Array and ArrayBuffer prototypes. #33
- refactor: simplify formatMessage internal logic. #34
## v0.9.1 / 2023-08-31 ## v0.9.1 / 2023-08-31
### Fixed ### Fixed
- cSHAKE empty Array bug. #24 - cSHAKE empty Array bug. #24
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "js-sha3", "name": "js-sha3",
"version": "0.9.1", "version": "0.9.2",
"main": ["src/sha3.js"], "main": ["src/sha3.js"],
"ignore": [ "ignore": [
"samples", "samples",
+2 -2
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "js-sha3", "name": "js-sha3",
"version": "0.9.1", "version": "0.9.2",
"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": {
+37 -39
View File
@@ -1,7 +1,7 @@
/** /**
* [js-sha3]{@link https://github.com/emn178/js-sha3} * [js-sha3]{@link https://github.com/emn178/js-sha3}
* *
* @version 0.9.1 * @version 0.9.2
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2015-2023 * @copyright Chen, Yi-Cyuan 2015-2023
* @license MIT * @license MIT
@@ -46,37 +46,35 @@
'256': 136 '256': 136
}; };
if (root.JS_SHA3_NO_NODE_JS || !Array.isArray) {
Array.isArray = function (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
}
if (ARRAY_BUFFER && (root.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) { var isArray = root.JS_SHA3_NO_NODE_JS || !Array.isArray
ArrayBuffer.isView = function (obj) { ? function (obj) {
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer; return Object.prototype.toString.call(obj) === '[object Array]';
};
}
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; : Array.isArray;
var isView = (ARRAY_BUFFER && (root.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView))
? function (obj) {
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
}
: ArrayBuffer.isView;
// [message: string, isString: bool]
var formatMessage = function (message) {
var type = typeof message;
if (type === 'string') {
return [message, true];
} }
return [message, notString]; if (type !== 'object' || message === null) {
throw new Error(INPUT_ERROR);
}
if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
} }
var empty = function (message) { var empty = function (message) {
@@ -215,7 +213,7 @@
} }
var result = formatMessage(message); var result = formatMessage(message);
message = result[0]; message = result[0];
var notString = result[1]; var isString = result[1];
var blocks = this.blocks, byteCount = this.byteCount, length = message.length, var blocks = this.blocks, byteCount = this.byteCount, length = message.length,
blockCount = this.blockCount, index = 0, s = this.s, i, code; blockCount = this.blockCount, index = 0, s = this.s, i, code;
@@ -227,11 +225,7 @@
blocks[i] = 0; blocks[i] = 0;
} }
} }
if (notString) { if (isString) {
for (i = this.start; index < length && i < byteCount; ++index) {
blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
}
} else {
for (i = this.start; index < length && i < byteCount; ++index) { for (i = this.start; index < length && i < byteCount; ++index) {
code = message.charCodeAt(index); code = message.charCodeAt(index);
if (code < 0x80) { if (code < 0x80) {
@@ -251,6 +245,10 @@
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} }
} }
} else {
for (i = this.start; index < length && i < byteCount; ++index) {
blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
}
} }
this.lastByteIndex = i; this.lastByteIndex = i;
if (i >= byteCount) { if (i >= byteCount) {
@@ -291,11 +289,9 @@
Keccak.prototype.encodeString = function (str) { Keccak.prototype.encodeString = function (str) {
var result = formatMessage(str); var result = formatMessage(str);
str = result[0]; str = result[0];
var notString = result[1]; var isString = result[1];
var bytes = 0, length = str.length; var bytes = 0, length = str.length;
if (notString) { if (isString) {
bytes = length;
} else {
for (var i = 0; i < str.length; ++i) { for (var i = 0; i < str.length; ++i) {
var code = str.charCodeAt(i); var code = str.charCodeAt(i);
if (code < 0x80) { if (code < 0x80) {
@@ -309,6 +305,8 @@
bytes += 4; bytes += 4;
} }
} }
} else {
bytes = length;
} }
bytes += this.encode(bytes * 8); bytes += this.encode(bytes * 8);
this.update(str); this.update(str);
+2 -2
View File
@@ -185,8 +185,8 @@
if (typeof BUFFER === 'boolean' && BUFFER) { if (typeof BUFFER === 'boolean' && BUFFER) {
testCases.sha3_512.Buffer = { testCases.sha3_512.Buffer = {
'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26': new Buffer(0), 'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26': Buffer.from([]),
'01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450': 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])) '01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450': 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]))
}; };
} }