0

I'm trying to write a code that will calculate 1000 SHA256 hashes from 3 randomly generated numbers( x1,x2,x3) Ideally it should be in the loop from 1 to 1000 and all i need is to store the values somewhere in a list or array and analyze them later. When i'm running the code below, even though the random numbers are changing evry time, print(digest.finalize()) always returns the same hash

import random
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
while i<1000:
        digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
        x1=random.randrange(2,256)
        x2=random.randrange(2,256)
        x3=random.randrange(2,256)
        print("x1,x2,x3 are ",x1,x2,x3)
        digest.update(b'x1')
        digest.update(b'x2')
        digest.update(b'x3')
        print(digest.finalize())
        del digest
        #print(digest.finalize())

        i=i+1
Ge Mi
  • 13
  • 2
  • 3
    You are hashing the values of x1, x2, and x3, instead you are hashing the bytes object `b'x1x2x3'`. – President James K. Polk Oct 22 '16 at 02:33
  • not sure i understand. What do i have to change to make it work? Thanks – Ge Mi Oct 22 '16 at 02:37
  • 2
    By learning the basics of python. In what programming language, a variable ```x``` is accessed by a string ```"x"```? :-) – sascha Oct 22 '16 at 03:17
  • By learning the basics of python??? Please @sascha leave the condescending tone for other places. If you want to educate someone start with yourself. you might find this link useful https://cryptography.io/en/latest/hazmat/primitives/cryptographic-hashes/ – Ge Mi Oct 22 '16 at 06:19
  • Possible duplicate of [Converting int to bytes in Python 3](http://stackoverflow.com/questions/21017698/converting-int-to-bytes-in-python-3) – Artjom B. Oct 22 '16 at 06:34
  • @GeMi You understand, that **your link presents an example** and most crypto-based examples are working with some human-readable string. A String is not a variable! Even your code shows the difference somehow: **Hint:** the print-line. – sascha Oct 22 '16 at 11:20
  • Sorry, I had a typo. My comment should have started out "you are *not* hashing the values of x1, x2, and x3". – President James K. Polk Oct 22 '16 at 11:25

1 Answers1

1

Consider the difference between digest.update(x1) and digest.update(b'x1'). The latter simply supplies the characters b'x' and b'1' to the update method and has nothing at all to do with the variable x1.

To hash the value of the variable x1 in

import random
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
x1=random.randrange(2,256)

you would need something like

digest.update(x1)

however, that is still incorrect because update() requires a bytes object for its argument, not an integer. Therefore the correct call is something like

digest.update(x1.to_bytes(1, 'big')) 

Please read carefully the document for the to_bytes method of int.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125