小编Jor*_*y10的帖子

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

我需要使用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 …
Run Code Online (Sandbox Code Playgroud)

python ecdsa

13
推荐指数
1
解决办法
2万
查看次数

如何正确使用python socket.settimeout()

据我所知,当您调用socket.settimeout(value)并且您将浮点值设置为大于0.0时,例如,当调用时,该套接字将提高scocket.timeoutsocket.recv必须等待比指定值更长的时间。

但是想象一下,我必须接收大量数据,并且我必须打电话 recv()多次,那么settimeout会如何影响它?

给出以下代码:

to_receive = # an integer representing the bytes we want to receive
socket = # a connected socket

socket.settimeout(20)
received = 0
received_data = b""

while received < to_receive:
    tmp = socket.recv(4096)
    if len(tmp) == 0:
        raise Exception()
    received += len(tmp)
    received_data += tmp

socket.settimeout(None)
Run Code Online (Sandbox Code Playgroud)

代码的第三行将套接字的超时设置为20秒。超时是否重置每次迭代?仅当这些迭代之一花费超过20秒时才会增加超时吗?

A)如果要花费所有超过20秒的时间来接收所有期望的数据,我该如何对其进行重新编码,以便引发异常?

B)如果在读取所有数据后没有将超时设置为“无”,会发生什么不好的事情吗?(连接保持活动状态,将来可能会请求更多数据)。

python sockets tcp

7
推荐指数
1
解决办法
2万
查看次数

如何异步执行几次函数并得到第一个结果

我有一个get_data(request)向服务器请求一些数据的函数.每次调用此函数时,它都会向不同的服务器请求数据.所有这些都应该返回相同的响应.

我想尽快得到答复.我需要创建一个get_data多次调用的函数,并返回它获得的第一个响应.

编辑:

我提出了使用multithreading.Pipe()的想法,但我觉得这是解决它的一种非常糟糕的方式,你怎么看?:

def get_data(request, pipe):
    data = # makes the request to a server, this can take a random amount of time
    pipe.send(data)

def multiple_requests(request, num_servers):
    my_pipe, his_pipe = multithreading.Pipe()

    for i in range(num_servers):
        Thread(target = get_data, args = (request,his_pipe)).start()

    return my_pipe.recv()

multiple_requests("the_request_string", 6)
Run Code Online (Sandbox Code Playgroud)

我认为这是一种糟糕的方式,因为你将相同的管道传递给所有线程,我真的不知道,但我想这必须是非常不安全的.

python multithreading multiprocessing threadpool

6
推荐指数
1
解决办法
167
查看次数