pub struct DsaSig(_);
Expand description
Object representing DSA signature.
DSA signatures consist of two components: r
and s
.
Examples
use std::convert::TryInto;
use openssl::bn::BigNum;
use openssl::dsa::{Dsa, DsaSig};
use openssl::hash::MessageDigest;
use openssl::pkey::PKey;
use openssl::sign::{Signer, Verifier};
const TEST_DATA: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let dsa_ref = Dsa::generate(1024).unwrap();
let pub_key: PKey<_> = dsa_ref.clone().try_into().unwrap();
let priv_key: PKey<_> = dsa_ref.try_into().unwrap();
let mut signer = if let Ok(signer) = Signer::new(MessageDigest::sha256(), &priv_key) {
signer
} else {
// DSA signing is not supported (eg. BoringSSL)
return;
};
signer.update(TEST_DATA).unwrap();
let signature = signer.sign_to_vec().unwrap();
// Parse DER-encoded DSA signature
let signature = DsaSig::from_der(&signature).unwrap();
// Extract components `r` and `s`
let r = BigNum::from_slice(&signature.r().to_vec()).unwrap();
let s = BigNum::from_slice(&signature.s().to_vec()).unwrap();
// Construct new DSA signature from components
let signature = DsaSig::from_private_components(r, s).unwrap();
// Serialize DSA signature to DER
let signature = signature.to_der().unwrap();
let mut verifier = Verifier::new(MessageDigest::sha256(), &pub_key).unwrap();
verifier.update(TEST_DATA).unwrap();
assert!(verifier.verify(&signature[..]).unwrap());
Implementations§
source§impl DsaSig
impl DsaSig
sourcepub fn from_private_components(r: BigNum, s: BigNum) -> Result<Self, ErrorStack>
pub fn from_private_components(r: BigNum, s: BigNum) -> Result<Self, ErrorStack>
Returns a new DsaSig
by setting the r
and s
values associated with an DSA signature.
This corresponds to DSA_SIG_set0
.
sourcepub fn from_der(der: &[u8]) -> Result<DsaSig, ErrorStack>
pub fn from_der(der: &[u8]) -> Result<DsaSig, ErrorStack>
Decodes a DER-encoded DSA signature.
This corresponds to d2i_DSA_SIG
.
Methods from Deref<Target = DsaSigRef>§
sourcepub fn to_der(&self) -> Result<Vec<u8>, ErrorStack>
pub fn to_der(&self) -> Result<Vec<u8>, ErrorStack>
Serializes the DSA signature into a DER-encoded DSASignature
structure.
This corresponds to i2d_DSA_SIG
.
sourcepub fn r(&self) -> &BigNumRef
pub fn r(&self) -> &BigNumRef
Returns internal component r
of an DsaSig
.
This corresponds to DSA_SIG_get0
.
sourcepub fn s(&self) -> &BigNumRef
pub fn s(&self) -> &BigNumRef
Returns internal component s
of an DsaSig
.
This corresponds to DSA_SIG_get0
.