summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2018-02-14 12:33:41 +0100
committerNeal H. Walfield <neal@pep.foundation>2018-02-14 12:33:41 +0100
commit4dfcec7bdc249a0a46be4d1573740e41f0d22ab0 (patch)
treec0da95e7e5f20b6fe1edfc75fded0b7a858d373a
parent5b1545ff0f02c066410424b595d0b801a93ecfce (diff)
openpgp: Make to_hex a top-level utility function.
-rw-r--r--openpgp/src/lib.rs17
-rw-r--r--openpgp/src/parse/parse.rs12
-rw-r--r--openpgp/src/s2k.rs13
3 files changed, 20 insertions, 22 deletions
diff --git a/openpgp/src/lib.rs b/openpgp/src/lib.rs
index fe98f338..72df45fd 100644
--- a/openpgp/src/lib.rs
+++ b/openpgp/src/lib.rs
@@ -94,6 +94,23 @@ pub enum Error {
#[fail(display = "{}", _0)]
Io(#[cause] io::Error),
}
+
+// A helpful debugging function.
+#[allow(dead_code)]
+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
+}
/// The OpenPGP packet tags as defined in [Section 4.3 of RFC 4880].
///
diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs
index 9767495a..8df01a3d 100644
--- a/openpgp/src/parse/parse.rs
+++ b/openpgp/src/parse/parse.rs
@@ -751,16 +751,6 @@ fn skesk_parser_test() {
},
];
- fn to_hex(s: &[u8]) -> String {
- use std::fmt::Write;
-
- let mut result = String::new();
- for b in s.iter() {
- write!(&mut result, "{:02X}", b).unwrap();
- }
- result
- }
-
for test in tests.iter() {
let path = path_to(test.filename);
let mut f = File::open(&path).expect(&path.to_string_lossy());
@@ -782,7 +772,7 @@ fn skesk_parser_test() {
let key = skesk.decrypt(test.password);
if let Some((_symm_algo, key)) = key {
- let key = to_hex(&key[..]);
+ let key = to_hex(&key[..], false);
assert_eq!(&key[..], &test.key_hex[..]);
} else {
panic!("Session key: None!");
diff --git a/openpgp/src/s2k.rs b/openpgp/src/s2k.rs
index 5aef2aad..8eb2d88e 100644
--- a/openpgp/src/s2k.rs
+++ b/openpgp/src/s2k.rs
@@ -93,6 +93,7 @@ impl S2K {
mod test {
use super::*;
+ use to_hex;
use Tag;
use SymmetricAlgo;
use SKESK;
@@ -217,16 +218,6 @@ mod test {
},
];
- fn to_hex(s: &[u8]) -> String {
- use std::fmt::Write;
-
- let mut result = String::new();
- for b in s.iter() {
- write!(&mut result, "{:02X}", b).unwrap();
- }
- result
- }
-
for test in tests.iter() {
let path = path_to(test.filename);
let mut f = File::open(&path).expect(&path.to_string_lossy());
@@ -252,7 +243,7 @@ mod test {
SymmetricAlgo::from_numeric(skesk.symm_algo).unwrap())
.unwrap());
if let Some(key) = key {
- let key = to_hex(&key[..]);
+ let key = to_hex(&key[..], false);
assert_eq!(key, test.key_hex);
} else {
panic!("Session key: None!");