简体   繁体   English

如何将 SHA256 object 保存到文件中?

[英]How to save SHA256 object to a file?

(I'm doing all this in python 3.10.4 using pycryptodome) (我正在使用 pycryptodome 在 python 3.10.4 中执行所有这些操作)

I'm trying to do this process:我正在尝试执行此过程:

  1. Get a hash of a file获取文件的 hash
  2. Save that hash somewhere将 hash 保存在某处
  3. Load that hash and perform RSA signing using a private key加载 hash 并使用私钥执行 RSA 签名

I'm having a problem in step 3 where to save the hash, I have to save it as a string which doesn't work in Step 3.我在第 3 步中遇到问题,在哪里保存 hash,我必须将其保存为字符串,这在第 3 步中不起作用。

I've tried using pickle but I'm getting "ctypes objects containing pointers cannot be pickled"我试过使用泡菜,但我得到“不能腌制包含指针的 ctypes 对象”

Code generating the hash:生成 hash 的代码:

sha256 = SHA256.new()
with open(fileDir, 'rb') as f:
    while True:
        data = f.read(BUF_SIZE)
        if not data:
            break
        sha256.update(data)

Code to perform the signing:执行签名的代码:

get_file(fileName + '.hash', directory)
with open(currentDir + '/client_files/downloaded/' + fileName + '.hash', 'r') as f:
    hash_data = f.read()    
with open(currentDir + '/client_files/private_key.pem', 'rb') as f:
    private_key = RSA.importKey(f.read())
print(private_key)    
signer = PKCS1_v1_5.new(private_key)
signature = signer.sign(hash_data)

The error I'm getting:我得到的错误:

Traceback (most recent call last):
  File "c:\Users\User\Documents\Coding\VSCode Projects\practiceGround\sec_cloud_project\client\client.py", line 168, in <module>
    main()
  File "c:\Users\User\Documents\Coding\VSCode Projects\practiceGround\sec_cloud_project\client\client.py", line 163, in main
    sign(fileName, 'worker_test_files')
  File "c:\Users\User\Documents\Coding\VSCode Projects\practiceGround\sec_cloud_project\client\client.py", line 120, in sign
    signature = signer.sign(hash_data)
  File "C:\Users\User\anaconda3\envs\nscc_project\lib\site-packages\Crypto\Signature\pkcs1_15.py", line 77, in sign
    em = _EMSA_PKCS1_V1_5_ENCODE(msg_hash, k)
  File "C:\Users\User\anaconda3\envs\nscc_project\lib\site-packages\Crypto\Signature\pkcs1_15.py", line 191, in _EMSA_PKCS1_V1_5_ENCODE
    digestAlgo = DerSequence([ DerObjectId(msg_hash.oid).encode() ])
AttributeError: 'str' object has no attribute 'oid'

Note that I'm currently saving the original hash as a string to a text file.请注意,我目前正在将原始 hash 作为字符串保存到文本文件中。 If I try to use pickle to save the object as a whole I get this error如果我尝试使用 pickle 将 object 作为一个整体保存,我会收到此错误

with open(currentDir + '/worker_files/sha256.pickle', 'wb') as f:
    pickle.dump(sha256, f)
Traceback (most recent call last):
  File "c:\Users\User\Documents\Coding\VSCode Projects\practiceGround\sec_cloud_project\worker\worker.py", line 188, in <module>
    main()
  File "c:\Users\User\Documents\Coding\VSCode Projects\practiceGround\sec_cloud_project\worker\worker.py", line 179, in main
    hash_file(fileName, 'worker_test_files')
  File "c:\Users\User\Documents\Coding\VSCode Projects\practiceGround\sec_cloud_project\worker\worker.py", line 55, in hash_file
    pickle.dump(sha256, f)
ValueError: ctypes objects containing pointers cannot be pickled

Thanks to @Topaco.感谢@Topaco。 Changing to using Cyptography for both hashing and signing seemed to work.更改为使用Cyptography进行散列和签名似乎有效。

Hashing with Cryptography , dumping to a file with pickle , then load and sign with Cryptography again.使用Cryptography散列,使用pickle转储到文件,然后再次加载并使用Cryptography签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM