Struct openssl::dsa::DsaSig

source ·
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§

Returns a new DsaSig by setting the r and s values associated with an DSA signature.

This corresponds to DSA_SIG_set0.

Decodes a DER-encoded DSA signature.

This corresponds to d2i_DSA_SIG.

Methods from Deref<Target = DsaSigRef>§

Serializes the DSA signature into a DER-encoded DSASignature structure.

This corresponds to i2d_DSA_SIG.

Returns internal component r of an DsaSig.

This corresponds to DSA_SIG_get0.

Examples found in repository?
src/dsa.rs (line 429)
427
428
429
430
431
432
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DsaSig")
            .field("r", self.r())
            .field("s", self.s())
            .finish()
    }

Returns internal component s of an DsaSig.

This corresponds to DSA_SIG_get0.

Examples found in repository?
src/dsa.rs (line 430)
427
428
429
430
431
432
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DsaSig")
            .field("r", self.r())
            .field("s", self.s())
            .finish()
    }

Trait Implementations§

Converts this type into a shared reference of the (usually inferred) input type.
Immutably borrows from an owned value. Read more
Formats the value using the given formatter. Read more
The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
Executes the destructor for this type. Read more
The raw C type.
The type representing a reference to this type.
Constructs an instance of this type from its raw type.
Returns a raw pointer to the wrapped value.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.