kopia lustrzana https://github.com/robinmoisson/staticrypt
Update browser version of HexEncoder parse()
rodzic
8f8c023fef
commit
9d04b3519a
|
@ -69,17 +69,22 @@ const HexEncoder = isNode
|
||||||
if (!hexString) {
|
if (!hexString) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (hexString.length % 2 !== 0) throw "Invalid hexString";
|
|
||||||
const arrayBuffer = new Uint8Array(hexString.length / 2);
|
if (hexString.length % 2 !== 0) {
|
||||||
|
throw new Error("Invalid hex string length");
|
||||||
|
}
|
||||||
|
|
||||||
|
const bytes = new Uint8Array(hexString.length / 2);
|
||||||
|
|
||||||
for (let i = 0; i < hexString.length; i += 2) {
|
for (let i = 0; i < hexString.length; i += 2) {
|
||||||
const byteValue = parseInt(hexString.substring(i, i + 2), 16);
|
const byte = parseInt(hexString.substring(i, i + 2), 16);
|
||||||
if (isNaN(byteValue)) {
|
if (isNaN(byte)) {
|
||||||
throw "Invalid hexString";
|
throw new Error("Invalid character in hex string.");
|
||||||
}
|
}
|
||||||
arrayBuffer[i / 2] = byteValue;
|
bytes[i / 2] = byte;
|
||||||
}
|
}
|
||||||
return arrayBuffer;
|
|
||||||
|
return bytes;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -91,16 +96,10 @@ const HexEncoder = isNode
|
||||||
if (!bytes) {
|
if (!bytes) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const hexBytes = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < bytes.length; ++i) {
|
return Array.from(bytes)
|
||||||
let byteString = bytes[i].toString(16);
|
.map((byte) => byte.toString(16).padStart(2, "0"))
|
||||||
if (byteString.length < 2) {
|
.join("");
|
||||||
byteString = "0" + byteString;
|
|
||||||
}
|
|
||||||
hexBytes.push(byteString);
|
|
||||||
}
|
|
||||||
return hexBytes.join("");
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
exports.HexEncoder = HexEncoder;
|
exports.HexEncoder = HexEncoder;
|
||||||
|
|
Ładowanie…
Reference in New Issue