Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the encoding used when hashing strings #1007

Open
veigaribo opened this issue Oct 13, 2023 · 1 comment
Open

Document the encoding used when hashing strings #1007

veigaribo opened this issue Oct 13, 2023 · 1 comment

Comments

@veigaribo
Copy link
Contributor

The README mentions

Per bcrypt implementation, only the first 72 bytes of a string are used. Any extra bytes are ignored when matching passwords. Note that this is not the first 72 characters. It is possible for a string to contain less than 72 characters, while taking up more than 72 bytes (e.g. a UTF-8 encoded string containing emojis).

But that is of little value because this library accepts JavaScript strings as parameters, e.g. for hash, so how are those strings being represented as bytes? Does it use UTF-8, like in the example? My investigation and tests suggest so.

This is some very important information that I can't find written anywhere. The README even seems to dodge this question since it also mentions

Compatibility with hashes generated by other languages is not 100% guaranteed due to difference in character encodings. However, it should not be an issue for most cases.

Again keeping it vague.

These are the lines I think are the most relevant and confirm that UTF-8 is being used, in case it saves time for someone.

std::string data = info[0].IsBuffer()

https://github.com/nodejs/node-addon-api/blob/7e1aa06132558fcc3de4ef5f4f6b84ff10c32502/napi-inl.h#L1105

@recrsn
Copy link
Collaborator

recrsn commented Nov 2, 2023

Yes we use utf-8, you are welcome to add an entry to the README to make it explicit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants