This library doesn't work with non-ASCII characters. Here's example code:
const tsse = require("tsse");
console.log("a".length, "ä".length); // 1 1
console.log(Buffer.from("a").length, Buffer.from("ä").length); // 1 2
console.log(tsse("a", "ä")); // RangeError
The problem is on this line:
if (hiddenStr.length !== inputStr.length) {
and these two lines:
const hiddenBuff = Buffer.from(hiddenStr);
const inputBuff = Buffer.from(inputStr);
This code works if both arguments are Buffers, because their length is the same as their byteLength, and Buffer.from simply creates a copy.
However, it doesn't work if the arguments are Strings. A string in JavaScript is encoded in UTF-16, so its length is the number of UTF-16 code units, rather than bytes. However, when you call Buffer.from(hiddenStr), the encoding defaults to utf8.
I think something like this would work better (I renamed hiddenStr and inputStr to hidden and input):
const hiddenLen = typeof hidden === "string" ? Buffer.byteLength(hidden) : hidden.length;
const inputLen = typeof input === "string" ? Buffer.byteLength(input) : input.length;
if (hiddenLen !== inputLen) {
Note how Buffer.byteLength(string) also defaults to utf8 encoding.
This library doesn't work with non-ASCII characters. Here's example code:
The problem is on this line:
and these two lines:
This code works if both arguments are
Buffers, because theirlengthis the same as theirbyteLength, andBuffer.fromsimply creates a copy.However, it doesn't work if the arguments are
Strings. A string in JavaScript is encoded in UTF-16, so itslengthis the number of UTF-16 code units, rather than bytes. However, when you callBuffer.from(hiddenStr), the encoding defaults toutf8.I think something like this would work better (I renamed
hiddenStrandinputStrtohiddenandinput):Note how
Buffer.byteLength(string)also defaults toutf8encoding.