Skip to content

Commit

Permalink
+ refine hex decode
Browse files Browse the repository at this point in the history
  • Loading branch information
oluceps committed Nov 9, 2024
1 parent 68462f6 commit 04d3a40
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions src/cmd/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
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<String, &Vec<u8>> = plain_map
let hashstr_ctx_map: HashMap<Vec<u8>, &Vec<u8>> = plain_map
.inner_ref()
.iter()
.map(|(k, v)| (get_hashed_id(*k), v))
Expand All @@ -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);
Expand Down
36 changes: 23 additions & 13 deletions src/helper/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>> {
pub fn parse_hash_str_list(&self) -> Result<Vec<Vec<u8>>> {
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())
}
}

Expand All @@ -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 {
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down

0 comments on commit 04d3a40

Please sign in to comment.