Ash*_*inc 0 python pickle python-3.x
当使用pickle lib和我创建的一些类时,输出对于用户来说相当容易阅读.例如,如果我有一个名为"存储"的填充,并将我的所有类数据保存到其中的.save文件中,则在使用文本编辑器打开文件时,您可以模糊地查看所有变量并且不需要太多挣扎,将它们更改为期望的结果.
下面是我用pickle创建的保存文件的片段(这是一个游戏):
S'Strength'
p4
I5
sS'Health'
p8
I100
Run Code Online (Sandbox Code Playgroud)
在此,'Health'的值为100,'Strength'为5,如果用户要编辑保存文件(因为它将在本地保存),他们可以轻松更改他们喜欢的任何变量,以便欺骗游戏.
因为我正在创建一个游戏,保存游戏是我计划实现的功能之一,这已经成为一个问题.
我确实考虑过使用加密,但是使用第二个外部库是最后的手段,因为它可能非常繁琐,所以我想知道是否还有其他方法可以做到这一点,或者如果pickle带有内置功能这(研究后我还没有看到).
您可以简单地对数据进行签名,然后在读取时对其进行身份验证,而不是尝试使您的数据不可读.
这里hmac用来计算哈希值.然后我们将哈希与数据一起保存:
import hmac, pickle
# pickle the data
pickled = pickle.dumps(data)
digest = hmac.new("some-shared-key", pickled,
digestmod=<my choice of hasher>
).hexdigest()
# now save the hashed digest and the pickled data
with open("some-file", "wb") as f:
# save these in some way you can distinguish them when you read them
print(digest, file=f)
print(pickled, file=f)
Run Code Online (Sandbox Code Playgroud)
为了验证数据,我们重新计算了pickle数据的摘要,并将其与保存在其中的摘要进行比较.
import hmac, pickle
with open("some-file", "rb") as f:
digest = f.readline()
pickled = f.read()
# confirm integrity
recomputed = hmac.new("some-shared-key", pickle,
digestmod=<my choice of hasher>
).hexdigest()
if not compare_digest(digest, recomputed):
raise SomeOneIsCheatingOhNoException
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
212 次 |
| 最近记录: |