1

Or, in general, any arbitrary number or bits that is not a multiple of 8.

According to hashlib.py there is one constructor method named for SHA256. Then I use sha256() to create a SHA256 hash object. I can now feed this object with arbitrary strings using the update() method, and at any point I can ask it for the digest of the concatenation of the strings fed to it so far using the digest() or hexdigest() methods.

Well. I would like to feed 1 bit to SHA256 in line with this link "What is the SHA-256 hash of a single '1' bit?"

In Python 2.7, of course.

So, what is the procedure to hash 1-bit long input consisting of the bit "1"? (not the 8-bit long byte[] { 1 } input)?

5
  • Yes, but the padding is done based on the input's length in bits. There are examples in other languages, there is C code available in [section 8][1] of [RFC 4634][2] to compute the hash of data that is not necessarily a multiple of 8 bits. See the methods whose names are SHA*FinalBits(...). [1]: tools.ietf.org/html/rfc4634#section-8 [2]: tools.ietf.org/html/rfc4634 Commented Sep 14, 2017 at 11:48
  • yes, BUT THERE IS NO 1-BIT TYPE IN PYTHON Commented Sep 14, 2017 at 11:53
  • Of course there is, it is called boolean. Commented Sep 14, 2017 at 12:07
  • @allo: care to demonstrate? Commented Sep 14, 2017 at 12:24
  • @DIUUSIULIUS This sounds like a beginning of an XY problem. Are you trying to achieve something specific, or is it just curiosity? Most real world protocols either pad the number, or have a specific encoding for the arbitrary length integers and hash that instead (like BER). Commented Sep 14, 2017 at 23:30

2 Answers 2

2

Neither the hashlib API nor the underlying C module support anything but "buffers of bytes".

Since the SHA standard prescribes adding something to any message, regardless of length, you cannot even "pre-pad" your input in python to get around that restriction.

To demonstrate (taken from https://www.rfc-editor.org/rfc/rfc4634#section-4.1):

>>> sha256('abcde').hexdigest()
'36bbe50ed96841d10443bcb670d6554f0a34b761be67ec9c4a8ad2c0c44ca42c'

>>> sha256('abcde\x80' + 57*'\x00' + '\x28').hexdigest()
'45cb103e6385e1330c892d1566d4d82f0c1c256947e54206704973c6c2adf4f6'

Although calculating the hash of a message with a length that is not a multiple of 8 might technically be allowed, I would very much doubt that there are real world use cases for this (that require the use of only the python standard lib, no less).

Sign up to request clarification or add additional context in comments.

Comments

1

You can use https://pypi.org/project/sha256bit/

Same interface as hashlib except that the update function has an optional argument bitlen to support bit granularity.

>>> from sha256bit import Sha256bit
>>> h=Sha256bit()
>>> h.update(b'\x00',bitlen=1)
>>> h.hexdigest()
'bd4f9e98beb68c6ead3243b1b4c7fed75fa4feaab1f84795cbd8a98676a2a375'

1 Comment

Finally, after 6 years, someone has provided the answer. Thank you!

Your Answer

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.