Begin verifying crc24 checksum for ASCII keys

parent 53b099c1
......@@ -5,6 +5,7 @@ use std::str::FromStr;
fn main() {
let data: &'static str = "-----BEGIN PGP PUBLIC KEY BLOCK-----
mQENBFXl288BCADOLAwf89kuek78E66ek+AXD8jojlaFoJcoO+Du51m8XJIWDzAF
Gnvff5NDZhqNgpC28CYHkzekfcNq3rXxeGx610a1zKI2ZRwk6ogvLWa5UoTVsWHy
712zwtLtlt7Y6AMdFyPNuSNq+dKc+6WX0R3N+kaaUYw4GnWQLi+odChkWnwklsvJ
......
......@@ -8,6 +8,10 @@ log = "0.4.1"
yaml-rust = ">= 0.3"
clap = {version = "2", features = ["yaml"]}
clippy = { version = "*", optional = true }
base64 = "~0.6.0"
crc24 = "0.1.5"
byteorder = "1"
[lib]
name = "pgpdump"
......
......@@ -4,16 +4,62 @@
#[cfg(test)]
mod tests;
extern crate base64;
extern crate byteorder;
extern crate crc24;
use std::str::FromStr;
use base64::decode;
use crc24::hash_raw;
#[derive(Debug)]
pub struct PgpDump {}
/// Strip PGP headers and end lines from an ASCII Armored PGP key.
fn strip_magic(data: &str) -> &str {
// ASCII Armored PGP keys start with headers and are followed by a blank link and then data
let start = data.find("\n\n").expect("Could not find start");
let end = data.rfind("-----END").expect("Could not find end");
data[start..end].trim_right()
}
/// A PGP data block in Radix-64 format can optionally include a CRC24 checksum consisting of an
/// equal sign followed by 4 alphanumeric characters. These 4 alphanumeric characters correspond to
/// 3 digits or 24 bits.
fn find_crc_checksum(data: &str) -> Result<Option<Vec<u8>>, base64::DecodeError> {
let end = data.len();
let r = data.as_bytes()[end - 5] as char;
match r {
'=' => match decode(&data[end - 4..end]) {
Ok(x) => Ok(Some(x)),
Err(e) => Err(e),
},
_ => Ok(None),
}
}
impl FromStr for PgpDump {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Err("Not implemented yet.")
if s.starts_with("-----BEGIN PGP ") {
let data = strip_magic(s);
// Verify that the CRC24 checksum matches if it exists
let known_crc = find_crc_checksum(data);
match known_crc {
Ok(Some(checksum)) => {
let end = data.len() - 5;
let computed_crc = hash_raw(&data[0..end].as_bytes());
println!("{:?}", computed_crc);
println!("{:?}", checksum);
}
Ok(None) => println!("No checksum found"),
Err(_) => println!("Problem base64 decoding crc24 checksum"),
}
Err("Not implemented yet.")
} else {
Err("Does not start with PGP string")
}
}
}
......
#[cfg(test)]
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment