# Single string producing two different hash results with SHA-256

Is it possible that a single string produces two different hash results after going through the same SHA-256 hash function? The code I'm working on right now is doing this, and I assumed this should not be possible. Any help?

• Most likely it's an uninitialized variable, or out-of-bound assignment. If you need help, there's loads of SHA256 implementations on the Internet you can reference. A good reference implementation can be found at IETF website RFC 6234. – DannyNiu Dec 26 '17 at 4:07
• You may want to note that the property that "if I put in the same thing twice I get the same result both times", is called determinism. – SEJPM Dec 26 '17 at 11:17
• Any help? – Yes, your implementation is faulty. Check your sourcecode and correct your errors accordingly. – e-sushi Dec 26 '17 at 11:50
• In which language you are writing the code? try analyzing the length of both the strings and also try comparing both strings char by char or xor both strings and see where it has 1's. you may find the difference. – abraza Dec 27 '17 at 21:31

However, what you are likely are talking about is a string of text. Now textual strings are strings of characters. To map characters to bytes the text needs to be encoded using a character-encoding. An example encoding is US-ASCII or the - largely compatible - UTF-8. However, there are also others, for instance .NET uses UTF-16 as default encoding (usually UTF-16LE, to be precise, mistakenly called Unicode in .NET). Java uses different character encodings as default depending on the platform that you are on.
Besides character encoding, there may also be other tiny differences. In bash, it is often forgotten that echo by default adds a newline, for instance. Windows, Linux and Apple all use different line endings. Unicode may have different representations for the same character w.r.t. diacritics, so you may need to normalize Unicode character strings. Unicode strings may also contain an (invisible) byte order mark at the start of the string. And of course there are the invisible special, non-printable characters that may be hiding in a string.