Skip to content

Commit

Permalink
types: use simple_asn1 crate for OIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
csssuf committed Feb 23, 2018
1 parent 22e725e commit 45f1f53
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ categories = ["cryptography", "parser-implementations"]
license = "Apache-2.0"

[dependencies]
simple_asn1 = "0.1"
byteorder = "1.2"
digest = "0.7"
failure = "0.1"
failure_derive = "0.1"
gcrypt = "0.5"
md-5 = "0.7"
num = "0.1.40"
ripemd160 = "0.7"
sha-1 = "0.7"
sha2 = "0.7"

[dependencies.asn1]
git = "https://github.com/csssuf/rust-asn1"
branch = "null"

[dependencies.nom]
version = "^3.2"
features = ["verbose-errors"]
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
//! [`Packet::to_bytes`]: enum.Packet.html#method.to_bytes
//! [`Packet::from_bytes`]: enum.Packet.html#method.from_bytes
//! [`SignaturePacket`]: struct.SignaturePacket.html
extern crate asn1;
#[macro_use]
extern crate simple_asn1;
extern crate byteorder;
extern crate digest;
#[macro_use]
Expand All @@ -28,6 +29,7 @@ extern crate gcrypt;
extern crate md5;
#[macro_use]
extern crate nom;
extern crate num;
extern crate ripemd160;
extern crate sha1;
extern crate sha2;
Expand Down
24 changes: 12 additions & 12 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use asn1::ObjectIdentifier;
use simple_asn1::OID;
use digest::Digest;
use failure::Error;
use num::BigUint;

/// Type for public key algorithms supported by OpenPGP.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -101,20 +102,19 @@ macro_rules! hash {
}

impl HashAlgorithm {
pub fn asn1_oid(&self) -> Result<ObjectIdentifier, Error> {
let oid_vec = match *self {
HashAlgorithm::Md5 => vec![1, 2, 840, 113549, 2, 5],
HashAlgorithm::Sha1 => vec![1, 3, 14, 3, 2, 26],
HashAlgorithm::Ripemd160 => vec![1, 3, 36, 3, 2, 1],
HashAlgorithm::Sha256 => vec![2, 16, 840, 1, 101, 3, 4, 2, 1],
HashAlgorithm::Sha384 => vec![2, 16, 840, 1, 101, 3, 4, 2, 2],
HashAlgorithm::Sha512 => vec![2, 16, 840, 1, 101, 3, 4, 2, 3],
HashAlgorithm::Sha224 => vec![2, 16, 840, 1, 101, 3, 4, 2, 4],
pub fn asn1_oid(&self) -> Result<OID, Error> {
let oid = match *self {
HashAlgorithm::Md5 => oid![1, 2, 840, 113549, 2, 5],
HashAlgorithm::Sha1 => oid![1, 3, 14, 3, 2, 26],
HashAlgorithm::Ripemd160 => oid![1, 3, 36, 3, 2, 1],
HashAlgorithm::Sha256 => oid![2, 16, 840, 1, 101, 3, 4, 2, 1],
HashAlgorithm::Sha384 => oid![2, 16, 840, 1, 101, 3, 4, 2, 2],
HashAlgorithm::Sha512 => oid![2, 16, 840, 1, 101, 3, 4, 2, 3],
HashAlgorithm::Sha224 => oid![2, 16, 840, 1, 101, 3, 4, 2, 4],
HashAlgorithm::Unknown => bail!(AlgorithmError::HashAlgorithmError),
};

ObjectIdentifier::new(oid_vec)
.ok_or(AlgorithmError::HashAlgorithmError.into())
Ok(oid)
}

pub fn hash<T: AsRef<[u8]>>(&self, contents: T) -> Result<Vec<u8>, Error> {
Expand Down

0 comments on commit 45f1f53

Please sign in to comment.