pub enum DigestAlgorithm {
    Sha1,
    Sha256,
    Sha384,
    Sha512,
}
Expand description

A hashing algorithm used for digesting data.

Instances can be converted to and from Oid via From/Into implementations.

They can also be converted to and from The ASN.1 AlgorithmIdentifier, which is commonly used to represent them in X.509 certificates.

Instances can be converted into a digest::Context capable of computing digests via From/Into.

Variants§

§

Sha1

SHA-1.

Corresponds to OID 1.3.14.3.2.26.

§

Sha256

SHA-256.

Corresponds to OID 2.16.840.1.101.3.4.2.1.

§

Sha384

SHA-384.

Corresponds to OID 2.16.840.1.101.3.4.2.2.

§

Sha512

SHA-512.

Corresponds to OID 2.16.840.1.101.3.4.2.3.

Implementations§

Obtain an object that can be used to digest content using this algorithm.

Examples found in repository?
src/algorithm.rs (line 209)
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    pub fn digest_data(&self, data: &[u8]) -> Vec<u8> {
        let mut h = self.digester();
        h.update(data);
        h.finish().as_ref().to_vec()
    }

    /// Digest content from a reader.
    pub fn digest_reader<R: std::io::Read>(&self, fh: &mut R) -> Result<Vec<u8>, std::io::Error> {
        let mut h = self.digester();

        loop {
            let mut buffer = [0u8; 16384];
            let count = fh.read(&mut buffer)?;

            h.update(&buffer[0..count]);

            if count < buffer.len() {
                break;
            }
        }

        Ok(h.finish().as_ref().to_vec())
    }
More examples
Hide additional examples
src/certificate.rs (line 335)
329
330
331
332
333
334
335
336
337
338
339
    pub fn fingerprint(
        &self,
        algorithm: DigestAlgorithm,
    ) -> Result<ring::digest::Digest, std::io::Error> {
        let raw = self.encode_der()?;

        let mut h = algorithm.digester();
        h.update(&raw);

        Ok(h.finish())
    }

Digest a slice of data.

Examples found in repository?
src/algorithm.rs (line 250)
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
    pub fn rsa_pkcs1_encode(
        &self,
        message: &[u8],
        target_length_in_bytes: usize,
    ) -> Result<Vec<u8>, Error> {
        let digest = self.digest_data(message);

        let digest_info = DigestInfo {
            algorithm: (*self).into(),
            digest: OctetString::new(digest.into()),
        };
        let mut digest_info_der = vec![];
        digest_info.write_encoded(bcder::Mode::Der, &mut digest_info_der)?;

        let encoded_digest_len = digest_info_der.len();

        // At least 8 bytes of padding are required. And there's a 2 byte header plus NULL
        // termination of the padding. So the target length must be 11+ bytes longer than
        // the encoded digest.
        if encoded_digest_len + 11 > target_length_in_bytes {
            return Err(Error::PkcsEncodeTooShort);
        }

        let pad_len = target_length_in_bytes - encoded_digest_len - 3;

        let mut res = vec![0xff; target_length_in_bytes];
        // Constant header.
        res[0] = 0x00;
        // Private key block type.
        res[1] = 0x01;
        // Padding bytes are already filled in.
        // NULL terminate padding.
        res[2 + pad_len] = 0x00;

        let digest_destination = &mut res[3 + pad_len..];
        digest_destination.copy_from_slice(&digest_info_der);

        Ok(res)
    }

Digest content from a reader.

Examples found in repository?
src/algorithm.rs (line 234)
233
234
235
    pub fn digest_path(&self, path: &std::path::Path) -> Result<Vec<u8>, std::io::Error> {
        self.digest_reader(&mut std::fs::File::open(path)?)
    }

Digest the content of a path.

EMSA-PKCS1-v1_5 padding procedure.

As defined by https://tools.ietf.org/html/rfc3447#section-9.2.

message is the message to digest and encode.

target_length_in_bytes is the target length of the padding. This should match the RSA key length. e.g. 2048 bit keys are length 256.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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.

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 resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
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.