我为python中的每个json文件获得了相同的sha256



我正处于一场巨大的哈希危机中。使用芯片-0007的默认格式,我生成了新的JSON文件。使用这些文件,我一直在尝试生成sha256哈希值。我希望每个文件都有一个唯一的散列值。

然而,python代码并没有这么做。我认为JSON文件可能有一些问题,但事实并非如此。这与sha256代码有关。

所有json文件->

JSON文件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文件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文件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" } }

示例CSV文件

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代码

TODO 2:以CHIP-0007的默认格式为团队工作表中的每个条目生成一个JSON文件

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()

如何为每个JSON生成唯一的sha256哈希?

我试着在字节块中读取。这也是行不通的。经过多次考验,我哪儿也去不了。共享每个JSON文件的意外输出:

[所有哈希都相同]

意外的SHA256输出:

e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

预期:

唯一哈希值。彼此不同的

由于输出缓冲,在向文件写入任何内容之前,您将调用sha256_gen(filepath),因此您将获得一个空文件的哈希。您应该在with之外执行此操作,以便关闭JSON文件并刷新缓冲区。

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)

相关内容

最新更新