Rex Rex - 3 months ago 19
Python Question

sha256 result dosen't change

First time using sha256. I'm using windows and puthon2.7.

With this code, the result always equal to "4aa6892909e369933b9f1babc10519121e2dfd1042551f6b9bdd4eae51f1f0c2"

what is wrong?

def signning(self,D_path):
BUF_SIZE = 65536
hashed = hashlib.sha256()

with open(D_path, 'rb') as f:
while True:
data = f.read(BUF_SIZE)
if not data:
break
hashed.update(data)

hashed_D = hashed.hexdigest()
print hashed_D

BPL BPL
Answer

Yeah, there is nothing wrong with your code, here's a little example showing that hashlib.sha256 is deterministic:

import random
import string
import hashlib

random.seed(1)

for i in range(5):
    data = ''.join(random.choice(string.ascii_uppercase + string.digits)
                   for _ in range(10))

    hashed = hashlib.sha256()
    hashed.update(data)
    print data, "->", hashed.hexdigest()

Run this script over and over and you'll be getting the same output cos the input is always the same ;)