mirror of
https://github.com/gchq/CyberChef.git
synced 2025-06-14 18:25:16 -04:00
added new param descriptor and fixed lint errors in Hex.mjs
This commit is contained in:
parent
278c221f1f
commit
34fe96deae
1 changed files with 6 additions and 5 deletions
|
@ -91,6 +91,7 @@ export function toHexFast(data) {
|
||||||
* @param {string} data
|
* @param {string} data
|
||||||
* @param {string} [delim]
|
* @param {string} [delim]
|
||||||
* @param {number} [byteLen=2]
|
* @param {number} [byteLen=2]
|
||||||
|
* @param {boolean} [throwError=false]
|
||||||
* @returns {byteArray}
|
* @returns {byteArray}
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
|
@ -100,7 +101,7 @@ export function toHexFast(data) {
|
||||||
* // returns [10,20,30]
|
* // returns [10,20,30]
|
||||||
* fromHex("0a:14:1e", "Colon");
|
* fromHex("0a:14:1e", "Colon");
|
||||||
*/
|
*/
|
||||||
export function fromHex(data, delim="Auto", byteLen=2, throwError=false) { //added throwError parameter
|
export function fromHex(data, delim="Auto", byteLen=2, throwError=false) {
|
||||||
if (byteLen < 1 || Math.round(byteLen) !== byteLen)
|
if (byteLen < 1 || Math.round(byteLen) !== byteLen)
|
||||||
throw new OperationError("Byte length must be a positive integer");
|
throw new OperationError("Byte length must be a positive integer");
|
||||||
|
|
||||||
|
@ -114,11 +115,11 @@ export function fromHex(data, delim="Auto", byteLen=2, throwError=false) { //add
|
||||||
const output = [];
|
const output = [];
|
||||||
for (let i = 0; i < data.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
for (let j = 0; j < data[i].length; j += byteLen) {
|
for (let j = 0; j < data[i].length; j += byteLen) {
|
||||||
const chunk = data[i].substr(j, byteLen); //extracted substr into a chunk variable
|
const chunk = data[i].substr(j, byteLen);
|
||||||
if (throwError && /[^a-f\d]/i.test(chunk)) { //added validation check for when throwError is true
|
if (throwError && /[^a-f\d]/i.test(chunk)) {
|
||||||
throw new OperationError(`Invalid hex character in chunk "${chunk}"`); //throw on invalid hex chunk
|
throw new OperationError(`Invalid hex character in chunk "${chunk}"`);
|
||||||
}
|
}
|
||||||
output.push(parseInt(chunk, 16)); //parseInt now uses chunk
|
output.push(parseInt(chunk, 16));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue