ECDSA.recover solidity怎么用
时间: 2024-05-11 20:14:35 浏览: 356
ECDSA.recover是Solidity中用于恢复签名者地址的内置函数,其使用方法如下:
1. 导入ECDSA库
在合约开头导入ECDSA库,如下所示:
```
pragma solidity ^0.8.0;
import "hardhat/console.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
contract MyContract {
using ECDSA for bytes32;
// ...
}
```
2. 调用ECDSA.recover函数
在需要恢复签名者地址的地方,调用ECDSA.recover函数,传入以下参数:
- messageHash:要恢复地址的消息哈希值
- v、r、s:签名的三个值
如下所示:
```
function verify(bytes32 messageHash, uint8 v, bytes32 r, bytes32 s) public view returns (address) {
address signer = messageHash.recover(v, r, s);
return signer;
}
```
其中,signer即为恢复出的签名者地址。
需要注意的是,messageHash必须是对原始消息进行了哈希后的值,而不是原始消息本身。如果要对字符串进行哈希,可以使用Solidity中的keccak256函数。
相关问题
ECDSA.recover
ECDSA.recover is a function in the ECDSA (Elliptic Curve Digital Signature Algorithm) cryptographic system that allows a user to recover the public key from a given signature and message. This function is useful in situations where the public key is unknown but the signature and message are available.
The ECDSA algorithm involves three steps: key generation, signature generation, and signature verification. In the key generation step, a private key is generated using a random number generator, and the corresponding public key is derived from the private key. In the signature generation step, a message is hashed and signed using the private key to generate a signature. In the signature verification step, the signature is verified using the public key to ensure that it was generated by the owner of the private key.
In some cases, the public key may not be available, but the signature and message are known. In such cases, the ECDSA.recover function can be used to recover the public key from the signature and message. The function takes three inputs: the message, the signature, and the recovery parameter. The recovery parameter is a number between 0 and 3 that specifies which of the four possible public keys should be recovered from the signature. Once the public key is recovered, it can be used to verify the signature and authenticate the message.
Overall, ECDSA.recover is a useful function in the ECDSA cryptographic system that allows for public key recovery in situations where it is unknown but the signature and message are available.
import os import ecdsa import hashlib from Crypto.Util.number import * from Crypto.Util.strxor import strxor as xor import secret p = getPrime(256) gen = lambda: p + getPrime(16) pad = lambda m: m + os.urandom(32 - len(m) % 32) key = os.urandom(30) sk = ecdsa.SigningKey.from_secret_exponent( secexp=bytes_to_long(key), curve=ecdsa.SECP256k1 ) sig1 = sk.sign(data=b'This is the first message.', k=gen()).hex() sig2 = sk.sign(data=b'Here is another message.', k=gen()).hex() enc = xor(hashlib.sha512(key).digest(), pad(secret.flag)).hex() print(f"{sig1 = }\n{sig2 = }\n{enc = }") ''' sig1 = '3f4a6f288e35a4397201d246a98c1f9cfa463e67717fbbdcbd26d7fac75f875855455c2bfb355f7f593ffbe4c4bd1fc729cc129976b56905639100c8ac716b37' sig2 = '9f563b21f0ee31b2f7a1a8c6edc8ff23b63e0a9d5dd4a699ecc3164871b4982df51bb2feb4bc06c578afd21d3e6227231dd5fe1d8440f3dcd025fd3ea68f5516' enc = 'cc66d251bfa54954890c81dc1c607bae716573949f327db18aa1f4c0f420b8d29dc7e7ff9edb17b90306bd2aa753fc3fd4dafb9cc4b771cbdd79000ef05a40c0' '''
这是一个使用 ECDSA 签名和 XOR 加密的 Python 代码,主要分为以下几个步骤:
1. 生成一个 256 位的质数 p,然后再随机生成一个 16 位的质数,将两个相加得到一个 272 位的数作为 ECDSA 的生成元。
2. 随机生成一个 30 字节的密钥 key,使用 SHA-512 哈希函数对其进行哈希,得到一个 64 字节的哈希值作为加密密钥。
3. 使用 ECDSA 签名算法对两个明文消息 "This is the first message." 和 "Here is another message." 进行签名,其中 k 是一个随机数,生成的签名值 sig1 和 sig2 分别为 136 字节和 139 字节的十六进制字符串。
4. 对一个名为 secret.flag 的字符串进行填充,使其长度为 32 的倍数,然后使用 XOR 加密算法和上一步得到的加密密钥进行加密,得到一个 128 字节的密文 enc。
该代码的作用是将 secret.flag 加密并对其进行签名,然后输出签名值和密文。
阅读全文