gpt4 book ai didi

python - 如何在 python 中使用 ecdsa 签名和验证签名

转载 作者:太空狗 更新时间:2023-10-29 17:25:47 28 4
gpt4 key购买 nike

我需要使用 256 位的私钥使用 ECDSA 对 256 位的哈希进行签名,就像比特币一样,由于 python 中缺少 ecdsa 文档,我感到绝望。

我在互联网上找到了很多代码,但是没有什么比 ecdsa.sign(msg, privkey) 或类似的代码更简单了,我找到的都是很多数学代码我不明白,但是他们使用 ecdsa 库(我不知道他们为什么不在将用于签名的库中添加签名功能,而是在使用时需要一页代码图书馆?)。

这是迄今为止我找到的最好的代码:

def ecdsa_sign(val, secret_exponent):
"""Return a signature for the provided hash, using the provided
random nonce. It is absolutely vital that random_k be an unpredictable
number in the range [1, self.public_key.point.order()-1]. If
an attacker can guess random_k, he can compute our private key from a
single signature. Also, if an attacker knows a few high-order
bits (or a few low-order bits) of random_k, he can compute our private
key from many signatures. The generation of nonces with adequate
cryptographic strength is very difficult and far beyond the scope
of this comment.

May raise RuntimeError, in which case retrying with a new
random value k is in order.
"""
G = ecdsa.SECP256k1
n = G.order()
k = deterministic_generate_k(n, secret_exponent, val)
p1 = k * G
r = p1.x()
if r == 0: raise RuntimeError("amazingly unlucky random number r")
s = ( ecdsa.numbertheory.inverse_mod( k, n ) * ( val + ( secret_exponent * r ) % n ) ) % n
if s == 0: raise RuntimeError("amazingly unlucky random number s")

return signature_to_der(r, s)

def deterministic_generate_k(generator_order, secret_exponent, val, hash_f=hashlib.sha256):
"""
Generate K value according to https://tools.ietf.org/html/rfc6979
"""
n = generator_order
order_size = (bit_length(n) + 7) // 8
hash_size = hash_f().digest_size
v = b'\x01' * hash_size
k = b'\x00' * hash_size
priv = intbytes.to_bytes(secret_exponent, length=order_size)
shift = 8 * hash_size - bit_length(n)
if shift > 0:
val >>= shift
if val > n:
val -= n
h1 = intbytes.to_bytes(val, length=order_size)
k = hmac.new(k, v + b'\x00' + priv + h1, hash_f).digest()
v = hmac.new(k, v, hash_f).digest()
k = hmac.new(k, v + b'\x01' + priv + h1, hash_f).digest()
v = hmac.new(k, v, hash_f).digest()

while 1:
t = bytearray()

while len(t) < order_size:
v = hmac.new(k, v, hash_f).digest()
t.extend(v)

k1 = intbytes.from_bytes(bytes(t))

k1 >>= (len(t)*8 - bit_length(n))
if k1 >= 1 and k1 < n:
return k1

k = hmac.new(k, v + b'\x00', hash_f).digest()
v = hmac.new(k, v, hash_f).digest()

但我不能相信这样的代码,因为我不知道它的作用。此外,ecdsa_sign 中的评论说返回给定值的签名、 secret 指数、和随机数。它说拥有随机数非常重要,但我就是想不通那个随机数在哪里。

是否有任何简单的单行方法来使用 Windows 上 python 中的任何受信任库来签署和验证 ECDSA 签名?

最佳答案

您可以尝试使用 python ecdsa 包,使用 Python3:

pip3 install ecdsa

用法:

import ecdsa

# SECP256k1 is the Bitcoin elliptic curve
sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
vk = sk.get_verifying_key()
sig = sk.sign(b"message")
vk.verify(sig, b"message") # True

使用公钥验证现有签名:

import ecdsa
from hashlib import sha256
message = b"message"
public_key = '98cedbb266d9fc38e41a169362708e0509e06b3040a5dfff6e08196f8d9e49cebfb4f4cb12aa7ac34b19f3b29a17f4e5464873f151fd699c2524e0b7843eb383'
sig = '740894121e1c7f33b174153a7349f6899d0a1d2730e9cc59f674921d8aef73532f63edb9c5dba4877074a937448a37c5c485e0d53419297967e95e9b1bef630d'

vk = ecdsa.VerifyingKey.from_string(bytes.fromhex(public_key), curve=ecdsa.SECP256k1, hashfunc=sha256) # the default is sha1
vk.verify(bytes.fromhex(sig), message) # True

该包也与 Python 2 兼容

关于python - 如何在 python 中使用 ecdsa 签名和验证签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34451214/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com