Skip to content

Commit cdef103

Browse files
committed
spki: implement Hash for AlgorithmIdentifier
This allows to build an hashmap with various hashing algorithm when verifying objects.
1 parent 2712580 commit cdef103

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

spki/src/algorithm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use der::asn1::Any;
2121
///
2222
/// [RFC 5280 Section 4.1.1.2]: https://tools.ietf.org/html/rfc5280#section-4.1.1.2
2323
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
24-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
24+
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
2525
pub struct AlgorithmIdentifier<Params> {
2626
/// Algorithm OID, i.e. the `algorithm` field in the `AlgorithmIdentifier`
2727
/// ASN.1 schema.

spki/tests/spki.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use hex_literal::hex;
55
use spki::SubjectPublicKeyInfoRef;
66

77
#[cfg(feature = "alloc")]
8-
use der::Encode;
8+
use {
9+
der::Encode,
10+
spki::{AlgorithmIdentifier, AlgorithmIdentifierOwned},
11+
};
912

1013
#[cfg(feature = "pem")]
1114
use der::{pem::LineEnding, EncodePem};
@@ -159,3 +162,20 @@ fn encode_rsa_2048_pem() {
159162
let pk_encoded = pk.to_pem(LineEnding::LF).unwrap();
160163
assert_eq!(RSA_2048_PEM_EXAMPLE, pk_encoded);
161164
}
165+
166+
#[test]
167+
#[cfg(feature = "alloc")]
168+
fn build_hashset_of_digests() {
169+
const SHA1: AlgorithmIdentifierOwned = AlgorithmIdentifier {
170+
oid: ObjectIdentifier::new_unwrap("1.3.14.3.2.26"),
171+
parameters: None,
172+
};
173+
const SHA256: AlgorithmIdentifierOwned = AlgorithmIdentifier {
174+
oid: ObjectIdentifier::new_unwrap("2.16.840.1.101.3.4.2.1"),
175+
parameters: None,
176+
};
177+
178+
let mut hashes = std::collections::HashSet::new();
179+
hashes.insert(SHA1);
180+
hashes.insert(SHA256);
181+
}

0 commit comments

Comments
 (0)