am getting identical sha256 for each json file in python

Question:

I am in a huge hashing crisis. Using the chip-0007’s default format I generatedfew JSON files. Using these files I have been trying to generate sha256 hash value. And I expect a unique hash value for each file.

However, python code isn’t doing so. I thought there might be some issue with JSON file but, it is not. Something is to do with sha256 code.

All the json files ->

JSON File 1

{ "format": "CHIP-0007", "name": "adewale-the-amebo", "description": "Adewale always wants to be in everyone's business.", "attributes": [ { "trait_type": "Gender", "value": "male" } ], "collection": { "name": "adewale-the-amebo Collection", "id": "1" } }

JSON File 2

{ "format": "CHIP-0007", "name": "alli-the-queeny", "description": "Alli is an LGBT Stan.", "attributes": [ { "trait_type": "Gender", "value": "male" } ], "collection": { "name": "alli-the-queeny Collection", "id": "2" } }

JSON File 3

{ "format": "CHIP-0007", "name": "aminat-the-snnobish", "description": "Aminat never really wants to talk to anyone.", "attributes": [ { "trait_type": "Gender", "value": "female" } ], "collection": { "name": "aminat-the-snnobish Collection", "id": "3" } }

Sample CSV File:

Series Number,Filename,Description,Gender
1,adewale-the-amebo,Adewale always wants to be in everyone's business.,male
2,alli-the-queeny,Alli is an LGBT Stan.,male
3,aminat-the-snnobish,Aminat never really wants to talk to anyone.,female

Python CODE

TODO 2 : Generate a JSON file per entry in team’s sheet in CHIP-0007’s default format

                new_jsonFile = f"{row[1]}.json"
                json_data = {}

                json_data["format"] = "CHIP-0007"
                json_data["name"] = row[1]
                json_data["description"] = row[2]

                attribute_data = {}
                attribute_data["trait_type"] = "Gender"  # gender
                attribute_data["value"] = row[3]  # "value/male/female"

                json_data["attributes"] = [attribute_data]

                collection_data = {}
                collection_data["name"] = f"{row[1]} Collection"
                collection_data["id"] = row[0]  # "ID of the NFT collection"

                json_data["collection"] = collection_data

                filepath = f"Json_Files/{new_jsonFile}"
                with open(filepath, 'w') as f:
                    json.dump(json_data, f, indent=2)
                    C += 1
                    sha256_hash = sha256_gen(filepath)
                    temp.append(sha256_hash)

                    NEW.append(temp)


# TODO 3 : Calculate sha256 of the each entry
def sha256_gen(fn):
    return hashlib.sha256(open(fn, 'rb').read()).hexdigest()

How can I generate a unique sha256 hash for each JSON?

I tried reading in byte blocks. That is also not working out. After many trials, I am going nowhere. Sharing the unexpected outputs of each JSON file:

[ All hashes are identical ]

Unexpected SHA256 output:

e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

Expected:

Unique Hash value. Different from each other

Asked By: codebae

||

Answers:

Because of output buffering, you’re calling sha256_gen(filepath) before anything is written to the file, so you’re getting the hash of an empty file. You should do that outside the with, so that the JSON file is closed and the buffer is flushed.

                with open(filepath, 'w') as f:
                    json.dump(json_data, f, indent=2)
                C += 1
                sha256_hash = sha256_gen(filepath)
                temp.append(sha256_hash)

                NEW.append(temp)
Answered By: Barmar
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.