openssl rust crate:ECDSA 签名大小不是 64 字节?
openssl rust crate: ECDSA signature size is not 64 bytes?
我想使用OpenSSL rust crate to perform cryptography operations, specifically using the ECDSA算法。
我使用以下代码生成 ECDSA 密钥(椭圆曲线 P-256)并使用它来签署数据并获得签名:
use openssl::sign::{Signer, Verifier};
use openssl::ec::{EcKey, EcGroup};
use openssl::pkey::PKey;
use openssl::hash::MessageDigest;
use openssl::nid::Nid;
// ec key
let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
let keypair = EcKey::generate(&group).unwrap();
let keypair = PKey::from_ec_key(keypair).unwrap();
// data to sign
let data = b"hello, world!";
// hash: sha-256
let mut signer = Signer::new(MessageDigest::sha256(), &keypair).unwrap();
let buf_size = signer.len().unwrap(); // Computes an upper bound on the signature length.
println!("buffer size {}", buf_size); // 72
let mut buf: [u8; 72] = [0; 72];
// sign
let exact_bytes = signer.sign_oneshot(&mut buf, data).unwrap(); //the number of bytes written.
println!("{}", exact_bytes); // 70
我不明白为什么 exact_bytes
是 70。在我的理解中,it should be 64。
ECDSA signatures are 2 times longer than the signer's private key for the curve used during the signing process. For example, for 256-bit elliptic curves (like secp256k1) the ECDSA signature is 512 bits (64 bytes) and for 521-bit curves (like secp521r1) the signature is 1042 bits.
有什么帮助吗?谢谢!
从here看来,它似乎也取决于签名使用的编码,这可能会增加长度。
另外,说明了你对签名长度的理解,和你说的不太一样
我想使用OpenSSL rust crate to perform cryptography operations, specifically using the ECDSA算法。
我使用以下代码生成 ECDSA 密钥(椭圆曲线 P-256)并使用它来签署数据并获得签名:
use openssl::sign::{Signer, Verifier};
use openssl::ec::{EcKey, EcGroup};
use openssl::pkey::PKey;
use openssl::hash::MessageDigest;
use openssl::nid::Nid;
// ec key
let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
let keypair = EcKey::generate(&group).unwrap();
let keypair = PKey::from_ec_key(keypair).unwrap();
// data to sign
let data = b"hello, world!";
// hash: sha-256
let mut signer = Signer::new(MessageDigest::sha256(), &keypair).unwrap();
let buf_size = signer.len().unwrap(); // Computes an upper bound on the signature length.
println!("buffer size {}", buf_size); // 72
let mut buf: [u8; 72] = [0; 72];
// sign
let exact_bytes = signer.sign_oneshot(&mut buf, data).unwrap(); //the number of bytes written.
println!("{}", exact_bytes); // 70
我不明白为什么 exact_bytes
是 70。在我的理解中,it should be 64。
ECDSA signatures are 2 times longer than the signer's private key for the curve used during the signing process. For example, for 256-bit elliptic curves (like secp256k1) the ECDSA signature is 512 bits (64 bytes) and for 521-bit curves (like secp521r1) the signature is 1042 bits.
有什么帮助吗?谢谢!
从here看来,它似乎也取决于签名使用的编码,这可能会增加长度。
另外,说明了你对签名长度的理解,和你说的不太一样