1

I am starting to look into how to implement SHA256 in JavaScript, and found this for example. It requires UTF-8 encoding it sounds like. Another one I saw required/supported only ASCII encoding and otherwise returned undefined.

This makes me wonder, how does the algorithm change based on the encoding? Where can I find more information on this? What if I wanted to use latin encoding or some other custom/private encoding, how would I support SHA256?

asked May 29, 2020 at 10:07

1 Answer 1

3

SHA256 and practically all encryption methods operate on bytes. That is often impractical, so you might have a utility for example that encrypts a utf-8 encoded string by converting it to bytes and produces the encrypted data, base-64 encoded. That’s not part of SHA itself but makes it easier to use.

For decryption, you have a double problem: The input might not be valid base-64, and the decrypted clear text bytes might not be convertible to utf-8, so you have to accept that failure is possible with arbitrary input.

And if you have another method encrypting Windows-1252 or UTF-16 data, then I would very strongly recommend to make sure that the same text is encrypted the same way, no matter what the encoding is.

answered May 29, 2020 at 11:39
1
  • 2
    "For decryption, you have a double problem" – Well, for SHA256 in particular, the much bigger problem is that it is not an encryption algorithm and thus cannot be decrypted ;-) Commented May 29, 2020 at 20:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.