Skip to content

RangeError: Input buffers must have the same byte length #5

@alex996

Description

@alex996

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions