Hash sha256 definition?

Hallo,
ich schreib das auf Deutsch, weil ich es einfach besser kann, obwohl dann wahrscheinlich weniger Antworten kommen werden. Englische Antworten sind auch ok.

Ich hatte zuerst gedacht, dass es nur an Python 2.7 liegt, aber dann habe ich das gleiche auch bei online-Hashern festgestellt.

Ist der sha2 nur fuer 7 Bit-Woerter definiert???

Wenn ich das achte setze, macht mir Python 2.7 eine Fehlermeldung und beim onlineberechnen hat es keinen Einfluss.
Aber es ist doch ein riesen Unterschied, was dieses Bit macht.

Bitte um Aufklaerung, weil ich dazu im Netz ueberhaupt nichts gefunden habe.

Frueher habe ich ausserdem noch festgestellt, dass man um auf das richtige Ergebinss zu kommen, die Null als Negativ ansehen muss. Koennte das auch mit dem 8.ten Bit zu tun haben?

Vielen Dank

Gruesse

Hy,
should I write en English?
If there is the same hash SHA256, neither bit 8 set or not, this could be a security-leak.

Please help.

Thank you very much.

No, SHA256 does not ignore the upper bit of bytes. A detailed breakdown of the SHA256 algo (.e.g. here) you can see the bit shifting will use the upper bit as well.

I’m seeing differences when setting the 8th bit. E.g. entering hex input using this online version:
input: 7F
hash:620bfdaa346b088fb49998d92f19a7eaf6bfc2fb0aee015753966da1028cb731

input: FF
hash:a8100ae6aa1940d0b663bb31cd466142ebbdbd5187131b92d93818987832eb89

If the MSB (most significant bit) was ignored, the hashes would be the same.

If you’d like more feedback on “If I set the eighth, Python 2.7 gives me an error message” you’ll have to post some code showing the issue.

1 Like

Hello,
thank you very much for the replay.
Here is the code:

import hashlib

def encrypt_string(hash_string):
    sha_signature = \
        hashlib.sha256(hash_string.encode()).hexdigest()
    return sha_signature

And here the Python 2.7 error:
Traceback (most recent call last):
File “…py”, line 31, in
print encrypt_string(chr(255))
File “…py”, line 11, in encrypt_string
hashlib.sha256(hash_string.encode()).hexdigest()
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xff in position 0: ordinal not in range(128)


Thank you

Regards

That has nothing to do with SHA256, and everything with encoding a str object into bytes (the hash_string.encode() part). Apparently in Python 2.7 the default encoding is ascii, and your string contains non-ASCII characters (e.g. umlauts).

That said: Stop using Python 2.7. It has been unsupported for a long time, and doesn’t even receive security updates any more.

This isn’t an issue of hashlib or sha256, but of python strings.

One of the limitations of python 2.x (solved in python 3.x) is the distinction between ascii and unicode strings.

The default in python 2.x is “ascii”, which is limited to values 0…127. Thus the error ‘ascii’ codec can’t decode byte 0xff : 255 is outside the ascii range!

If you’re using python 2.x, and want to hash strings, you’ll want to consider using a unicode string. The following version does so and gives me the same answer with 2.x and 3.x:

import hashlib

def encrypt_string(hash_string):
    sha_signature = \
        hashlib.sha256(hash_string.encode()).hexdigest()
    return sha_signature

print(encrypt_string(u'255'))

[Using 3.x, your original version works fine, because unicode strings are the default in 3.x.]

Note that you will get a different hash value for u’255’ than the value I showed for 0xFF in my previous reply. That’s because the string isn’t quite the same as the byte value.

The following (works in python 2.x) gives the same answer as shown previously:

print hashlib.sha256(b'\xFF').hexdigest()

That is the byte sequence 0xFF, and not a string.

And finally, for arbitrary unicode strings, you’ll need to tell python 2.x about the file encoding, and encode properly inside encrypt_string:

# coding=utf-8
import hashlib

def encrypt_string(hash_string):
    sha_signature = \
        hashlib.sha256(hash_string.encode("utf-8")).hexdigest()
    return sha_signature

print encrypt_string(u'Schönen Tag')
1 Like

Hello,
first of all, thank you very much all for the repays.
You helped me very much.
Will use Python 3 in the future.
Seems that german is known :slight_smile:

Regards

1 Like