Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
scroll top