diff --git a/Cargo.lock b/Cargo.lock index 59189fa..b60aa38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -768,6 +768,18 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + [[package]] name = "hkdf" version = "0.12.4" @@ -1879,6 +1891,8 @@ dependencies = [ "blake3", "console", "eyre", + "hex", + "hex-literal", "libc", "nom", "pinentry", diff --git a/Cargo.toml b/Cargo.toml index e5c7f4e..c13b4a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,8 @@ argh = "0.1.12" blake3 = "1.5.4" console = "0.15.8" eyre = "0.6.12" +hex = "0.4.3" +hex-literal = "0.4.1" libc = "0.2.158" nom = "7.1.3" pinentry = "0.5.1" diff --git a/src/cmd/deploy.rs b/src/cmd/deploy.rs index bdae80b..72fbe9e 100644 --- a/src/cmd/deploy.rs +++ b/src/cmd/deploy.rs @@ -226,14 +226,14 @@ impl Profile { info!("start deploy templates"); use sha2::{Digest, Sha256}; - let get_hashed_id = |s: &profile::Secret| -> String { + let get_hashed_id = |s: &profile::Secret| -> Vec { let mut hasher = Sha256::new(); hasher.update(s.id.as_str()); - format!("{:X}", hasher.finalize()).to_lowercase() + hasher.finalize().to_vec() }; // new map with sha256 hashed secret id str as key, ctx as value - let hashstr_ctx_map: HashMap> = plain_map + let hashstr_ctx_map: HashMap, &Vec> = plain_map .inner_ref() .iter() .map(|(k, v)| (get_hashed_id(*k), v)) @@ -245,12 +245,12 @@ impl Profile { hashstr_ctx_map .iter() - .filter(|(k, _)| hashstrs_of_it.contains(*k)) + .filter(|(k, _)| hashstrs_of_it.contains(k)) .for_each(|(k, v)| { // render trace!("template before process: {}", template); template = template.replace( - format!("{{{{ {} }}}}", k).as_str(), + format!("{{{{ {} }}}}", hex::encode(k.as_slice())).as_str(), String::from_utf8_lossy(v).to_string().as_str(), ); trace!("processed template: {}", template); diff --git a/src/helper/template.rs b/src/helper/template.rs index 74fa3f4..f0b2531 100644 --- a/src/helper/template.rs +++ b/src/helper/template.rs @@ -31,13 +31,17 @@ fn pars<'a>(text: &'a str, res: &mut Vec<&'a str>) { } impl Template { - pub fn parse_hash_str_list(&self) -> Result> { + pub fn parse_hash_str_list(&self) -> Result>> { + use hex::decode; let text = &self.content; let mut res = vec![]; let text = format!(" {}", text); // hack pars(text.as_str(), &mut res); - Ok(res.into_iter().map(|s| String::from(s)).collect()) + Ok(res + .into_iter() + .map(|s| decode(s).expect("hex decode")) + .collect()) } } @@ -46,6 +50,8 @@ impl Template { #[cfg(test)] mod tests { use super::*; + use hex_literal::hex; + use nom::AsBytes; impl Default for Template { fn default() -> Self { @@ -72,8 +78,8 @@ mod tests { ..Template::default() }; assert_eq!( - vec!["dcd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440aa93"], - t.parse_hash_str_list().unwrap() + hex!("dcd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440aa93"), + t.parse_hash_str_list().unwrap().get(0).unwrap().as_bytes() ) } #[test] @@ -84,12 +90,14 @@ mod tests { content: String::from(str), ..Template::default() }; + let l = t.parse_hash_str_list().unwrap(); assert_eq!( - vec![ - "dcd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440aa93", - "cd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440a2a93" - ], - t.parse_hash_str_list().unwrap() + hex!("dcd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440aa93"), + l.get(0).unwrap().as_slice() + ); + assert_eq!( + hex!("cd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440a2a93"), + l.get(1).unwrap().as_slice() ) } #[test] @@ -100,9 +108,10 @@ mod tests { content: String::from(str), ..Template::default() }; + let l = t.parse_hash_str_list().unwrap(); assert_eq!( - vec!["cd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440a2a93",], - t.parse_hash_str_list().unwrap() + hex!("cd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440a2a93"), + l.get(0).unwrap().as_slice() ) } #[test] @@ -113,9 +122,10 @@ mod tests { content: String::from(str), ..Template::default() }; + let l = t.parse_hash_str_list().unwrap(); assert_eq!( - vec!["cd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440a2a93",], - t.parse_hash_str_list().unwrap() + hex!("cd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440a2a93"), + l.get(0).unwrap().as_slice() ) } #[test]