summaryrefslogtreecommitdiffstats
path: root/openpgp/src/fingerprint.rs
diff options
context:
space:
mode:
authorKai Michaelis <kai@sequoia-pgp.org>2019-02-26 17:22:12 +0100
committerKai Michaelis <kai@sequoia-pgp.org>2019-02-26 18:13:21 +0100
commit8f71fe5d0a6dc142bdabdc9a3a25c3868e398645 (patch)
treea922c63a948af69917f19b5749120f652ae2a92a /openpgp/src/fingerprint.rs
parente07bb25de0b2291088f88dc5360f3d04f702049c (diff)
openpgp: Add Fingerprint::to_icao
Adds a function to convert the hex fingerprint to ICAO spelling.
Diffstat (limited to 'openpgp/src/fingerprint.rs')
-rw-r--r--openpgp/src/fingerprint.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/openpgp/src/fingerprint.rs b/openpgp/src/fingerprint.rs
index 58c0fbe2..7b494f21 100644
--- a/openpgp/src/fingerprint.rs
+++ b/openpgp/src/fingerprint.rs
@@ -135,4 +135,56 @@ impl Fingerprint {
}
}
}
+
+ /// Converts the hex representation of the fingerprint to a phrase in the
+ /// ICAO alphabet.
+ pub fn to_icao(&self) -> String {
+ let mut ret = String::default();
+
+ for ch in self.to_hex().chars() {
+ let word = match ch {
+ '0' => "Zero",
+ '1' => "One",
+ '2' => "Two",
+ '3' => "Three",
+ '4' => "Four",
+ '5' => "Five",
+ '6' => "Six",
+ '7' => "Seven",
+ '8' => "Eight",
+ '9' => "Niner",
+ 'A' => "Alpha",
+ 'B' => "Bravo",
+ 'C' => "Charlie",
+ 'D' => "Delta",
+ 'E' => "Echo",
+ 'F' => "Foxtrot",
+ _ => { continue; }
+ };
+
+ if !ret.is_empty() {
+ ret.push_str(" ");
+ }
+ ret.push_str(word);
+ }
+
+ ret
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn icao() {
+ let fpr = Fingerprint::from_hex(
+ "0123 4567 89AB CDEF 0123 4567 89AB CDEF 0123 4567").unwrap();
+ let expected = "\
+Zero One Two Three Four Five Six Seven Eight Niner Alpha Bravo Charlie Delta \
+Echo Foxtrot Zero One Two Three Four Five Six Seven Eight Niner Alpha Bravo \
+Charlie Delta Echo Foxtrot Zero One Two Three Four Five Six Seven";
+
+ assert_eq!(fpr.to_icao(), expected);
+ }
}