You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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):
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
Buffer
s, because theirlength
is the same as theirbyteLength
, andBuffer.from
simply creates a copy.However, it doesn't work if the arguments are
String
s. A string in JavaScript is encoded in UTF-16, so itslength
is 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
hiddenStr
andinputStr
tohidden
andinput
):Note how
Buffer.byteLength(string)
also defaults toutf8
encoding.The text was updated successfully, but these errors were encountered: