如何将SHA256对象保存到文件中?

发布于 2025-01-22 17:15:44 字数 2631 浏览 5 评论 0原文

(我正在使用pycryptodome在python 3.10.4中执行所有操作)

我正在尝试执行此过程:

  1. 获取一个文件保存的hash hash
  2. save hash hash
  3. load load hosh and hosh and hash and hash and rsa签名,并使用私钥执行RSA

签名在第3步中的问题中保存哈希的问题,我必须将其保存为一根字符串,该字符串在第3步中不起作用

。 “不能腌制的包含指针的CTYPES对象”

生成哈希:

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

执行签名的代码:

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)

我遇到的错误:

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'

请注意,我当前将原始哈希作为字符串保存为文本文件。如果我尝试使用泡菜来保存对象,我会得到此错误

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

(I'm doing all this in python 3.10.4 using pycryptodome)

I'm trying to do this process:

  1. Get a hash of a file
  2. Save that hash somewhere
  3. Load that hash and perform RSA signing using a private key

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.

I've tried using pickle but I'm getting
"ctypes objects containing pointers cannot be pickled"

Code generating the 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. If I try to use pickle to save the object as a whole I get this error

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2025-01-29 17:15:44

感谢@topaco。更改为cyptography用于哈希和签名似乎都起作用。

使用加密哈希,将泡菜倾倒到文件中,然后加载并用cryptography签名。

Thanks to @Topaco. Changing to using Cyptography for both hashing and signing seemed to work.

Hashing with Cryptography, dumping to a file with pickle, then load and sign with Cryptography again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文