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
pull/35/head
legobeat 2 years ago committed by GitHub
parent f293b5d508
commit d6325442bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

2
build/sha3.min.js vendored

File diff suppressed because one or more lines are too long

@ -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]';
}; }
} : Array.isArray;
var formatMessage = function (message) { var isView = (ARRAY_BUFFER && (root.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView))
var notString, type = typeof message; ? function (obj) {
if (type !== 'string') { return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
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; : ArrayBuffer.isView;
// [message: string, isString: bool]
var formatMessage = function (message) {
var type = typeof message;
if (type === 'string') {
return [message, true];
}
if (type !== 'object' || message === null) {
throw new Error(INPUT_ERROR);
}
if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
} }
return [message, notString]; 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);

Loading…
Cancel
Save