diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-08-29 12:54:58 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-08-29 17:11:58 +0200 |
commit | 05182027ce3e79f78f21c0fe195ab98d32872e5c (patch) | |
tree | 4534b0109e3fd3a0e2924e729db07390731f9cf0 /openpgp/src/keyid.rs | |
parent | 5629e9625f821f614580a401e16eb41229c8528d (diff) |
openpgp: Add conversion from KeyIDs to u64.
Diffstat (limited to 'openpgp/src/keyid.rs')
-rw-r--r-- | openpgp/src/keyid.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/openpgp/src/keyid.rs b/openpgp/src/keyid.rs index 3065398a..2767afa8 100644 --- a/openpgp/src/keyid.rs +++ b/openpgp/src/keyid.rs @@ -1,5 +1,6 @@ use std::fmt; +use Error; use Fingerprint; use KeyID; use Result; @@ -45,6 +46,24 @@ impl KeyID { Self::from_bytes(&bytes[..]) } + /// Converts the KeyID to a u64 if possible. + pub fn as_u64(&self) -> Result<u64> { + match &self { + KeyID::V4(ref b) => + Ok(0u64 + | ((b[0] as u64) << (7 * 8)) + | ((b[1] as u64) << (6 * 8)) + | ((b[2] as u64) << (5 * 8)) + | ((b[3] as u64) << (4 * 8)) + | ((b[4] as u64) << (3 * 8)) + | ((b[5] as u64) << (2 * 8)) + | ((b[6] as u64) << (1 * 8)) + | ((b[7] as u64) << (0 * 8))), + KeyID::Invalid(_) => + Err(Error::InvalidArgument("Invalid KeyID".into()).into()), + } + } + /// Reads a binary key ID. pub fn from_bytes(raw: &[u8]) -> KeyID { if raw.len() == 8 { @@ -137,3 +156,13 @@ impl KeyID { String::from_utf8(output).unwrap() } } + +#[cfg(test)] +mod test { + use super::*; + quickcheck! { + fn u64_roundtrip(id: u64) -> bool { + KeyID::new(id).as_u64().unwrap() == id + } + } +} |