diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-03-25 11:05:38 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-03-25 11:05:38 +0100 |
commit | f490240d136496684db30c6c193bcf16bf662a80 (patch) | |
tree | 9503623d5373181c22dc97c6253089312f0f5b5a /tool/src | |
parent | 6632a2b5591908d68e49fc42377c4ad6a55c840b (diff) |
tool: Use the new API.
Diffstat (limited to 'tool/src')
-rw-r--r-- | tool/src/commands/dump.rs | 42 | ||||
-rw-r--r-- | tool/src/sq.rs | 59 |
2 files changed, 16 insertions, 85 deletions
diff --git a/tool/src/commands/dump.rs b/tool/src/commands/dump.rs index 3022a478..da092a52 100644 --- a/tool/src/commands/dump.rs +++ b/tool/src/commands/dump.rs @@ -3,6 +3,7 @@ use time; extern crate sequoia_openpgp as openpgp; use openpgp::constants::SymmetricAlgorithm; +use openpgp::conversions::hex; use openpgp::{Packet, Result}; use openpgp::packet::ctb::CTB; use openpgp::packet::{Header, BodyLength, Signature}; @@ -48,7 +49,7 @@ pub fn dump(input: &mut io::Read, output: &mut io::Write, mpis: bool, hex: bool, } } let mut fields = Vec::new(); - fields.push(format!("Session key: {}", to_hex(sk, false))); + fields.push(format!("Session key: {}", hex::encode(sk))); if let Some(algo) = decrypted_with { fields.push(format!("Symmetric algo: {}", algo)); fields.push("Decryption successful".into()); @@ -68,7 +69,7 @@ pub fn dump(input: &mut io::Read, output: &mut io::Write, mpis: bool, hex: bool, let _ = pp.decrypt(algo, sk); let mut fields = Vec::new(); - fields.push(format!("Session key: {}", to_hex(sk, false))); + fields.push(format!("Session key: {}", hex::encode(sk))); if pp.decrypted() { fields.push("Decryption successful".into()); } else { @@ -230,7 +231,7 @@ impl PacketDumper { } } writeln!(output, "{} Hash prefix: {}", i, - to_hex(s.hash_prefix(), false))?; + hex::encode(s.hash_prefix()))?; write!(output, "{} Level: {} ", i, s.level())?; match s.level() { 0 => writeln!(output, "(signature over data)")?, @@ -347,7 +348,7 @@ impl PacketDumper { self.dump_s2k(output, i, s.s2k())?; if let Some(esk) = s.esk() { writeln!(output, "{} ESK: {}", i, - to_hex(esk, false))?; + hex::encode(esk))?; } }, @@ -359,13 +360,13 @@ impl PacketDumper { write!(output, "{} S2K: ", i)?; self.dump_s2k(output, i, s.s2k())?; writeln!(output, "{} IV: {}", i, - to_hex(s.aead_iv(), false))?; + hex::encode(s.aead_iv()))?; if let Some(esk) = s.esk() { writeln!(output, "{} ESK: {}", i, - to_hex(esk, false))?; + hex::encode(esk))?; } writeln!(output, "{} Digest: {}", i, - to_hex(s.aead_digest(), false))?; + hex::encode(s.aead_digest()))?; }, } }, @@ -378,9 +379,9 @@ impl PacketDumper { MDC(ref m) => { writeln!(output, "Modification Detection Code Packet")?; writeln!(output, "{} Hash: {}", - i, to_hex(m.hash(), false))?; + i, hex::encode(m.hash()))?; writeln!(output, "{} Computed hash: {}", - i, to_hex(m.computed_hash(), false))?; + i, hex::encode(m.computed_hash()))?; }, AED(ref a) => { @@ -389,7 +390,7 @@ impl PacketDumper { writeln!(output, "{} Cipher: {}", i, a.cipher())?; writeln!(output, "{} AEAD: {}", i, a.aead())?; writeln!(output, "{} Chunk size: {}", i, a.chunk_size())?; - writeln!(output, "{} IV: {}", i, to_hex(a.iv(), false))?; + writeln!(output, "{} IV: {}", i, hex::encode(a.iv()))?; }, } @@ -491,7 +492,7 @@ impl PacketDumper { write!(output, "{} Features: {:?}", i, f)?, SignatureTarget{pk_algo, hash_algo, ref digest} => write!(output, "{} Signature target: {}, {}, {}", i, - pk_algo, hash_algo, to_hex(digest, false))?, + pk_algo, hash_algo, hex::encode(digest))?, EmbeddedSignature(_) => // Embedded signature is dumped below. write!(output, "{} Embedded signature: ", i)?, @@ -532,12 +533,12 @@ impl PacketDumper { Salted { hash, ref salt } => { writeln!(output, "Salted")?; writeln!(output, "{} Hash: {}", i, hash)?; - writeln!(output, "{} Salt: {}", i, to_hex(salt, false))?; + writeln!(output, "{} Salt: {}", i, hex::encode(salt))?; }, Iterated { hash, ref salt, iterations } => { writeln!(output, "Iterated")?; writeln!(output, "{} Hash: {}", i, hash)?; - writeln!(output, "{} Salt: {}", i, to_hex(salt, false))?; + writeln!(output, "{} Salt: {}", i, hex::encode(salt))?; writeln!(output, "{} Iterations: {}", i, iterations)?; }, Private(n) => @@ -604,18 +605,3 @@ impl HexDumper { Ok(()) } } - -fn to_hex(s: &[u8], pretty: bool) -> String { - use std::fmt::Write; - - let mut result = String::new(); - for (i, b) in s.iter().enumerate() { - // Add spaces every four digits to make the output more - // readable. - if pretty && i > 0 && i % 2 == 0 { - write!(&mut result, " ").unwrap(); - } - write!(&mut result, "{:02X}", b).unwrap(); - } - result -} diff --git a/tool/src/sq.rs b/tool/src/sq.rs index 582da3c6..f0ed22d1 100644 --- a/tool/src/sq.rs +++ b/tool/src/sq.rs @@ -22,6 +22,7 @@ extern crate sequoia_net; extern crate sequoia_store; use openpgp::{armor, autocrypt, Fingerprint, TPK}; +use openpgp::conversions::hex; use openpgp::parse::Parse; use openpgp::serialize::Serialize; use sequoia_core::{Context, NetworkPolicy}; @@ -235,7 +236,7 @@ fn real_main() -> Result<(), failure::Error> { let mut output = create_or_stdout(m.value_of("output"), force)?; let session_key: Option<openpgp::crypto::SessionKey> = if let Some(sk) = m.value_of("session-key") { - Some(from_hex(sk, true)?.into()) + Some(hex::decode_pretty(sk)?.into()) } else { None }; @@ -479,62 +480,6 @@ fn format_time(t: &time::Timespec) -> String { .unwrap() // Only parse errors can happen. } -/// A helpful function for converting a hexadecimal string to binary. -/// This function skips whitespace if `pretty` is set. -pub(crate) fn from_hex(hex: &str, pretty: bool) -> openpgp::Result<Vec<u8>> { - use openpgp::Error; - const BAD: u8 = 255u8; - const X: u8 = 'x' as u8; - - let mut nibbles = hex.as_bytes().iter().filter_map(|x| { - match *x as char { - '0' => Some(0u8), - '1' => Some(1u8), - '2' => Some(2u8), - '3' => Some(3u8), - '4' => Some(4u8), - '5' => Some(5u8), - '6' => Some(6u8), - '7' => Some(7u8), - '8' => Some(8u8), - '9' => Some(9u8), - 'a' | 'A' => Some(10u8), - 'b' | 'B' => Some(11u8), - 'c' | 'C' => Some(12u8), - 'd' | 'D' => Some(13u8), - 'e' | 'E' => Some(14u8), - 'f' | 'F' => Some(15u8), - 'x' | 'X' if pretty => Some(X), - _ if pretty && x.is_ascii_whitespace() => None, - _ => Some(BAD), - } - }).collect::<Vec<u8>>(); - - if pretty && nibbles.len() >= 2 && nibbles[0] == 0 && nibbles[1] == X { - // Drop '0x' prefix. - nibbles.remove(0); - nibbles.remove(0); - } - - if nibbles.iter().any(|&b| b == BAD || b == X) { - // Not a hex character. - return - Err(Error::InvalidArgument("Invalid characters".into()).into()); - } - - // We need an even number of nibbles. - if nibbles.len() % 2 != 0 { - return - Err(Error::InvalidArgument("Odd number of nibbles".into()).into()); - } - - let bytes = nibbles.chunks(2).map(|nibbles| { - (nibbles[0] << 4) | nibbles[1] - }).collect::<Vec<u8>>(); - - Ok(bytes) -} - fn main() { if let Err(e) = real_main() { let mut cause = e.as_fail(); |