src/tools.rs: move hex_to_digest and digest_to_hex to proxmox::tools
This commit is contained in:
40
src/tools.rs
40
src/tools.rs
@ -567,46 +567,6 @@ pub fn get_hardware_address() -> Result<String, Error> {
|
||||
Ok(format!("{:0x}", digest))
|
||||
}
|
||||
|
||||
const HEX_CHARS: &'static [u8; 16] = b"0123456789abcdef";
|
||||
|
||||
pub fn digest_to_hex(digest: &[u8]) -> String {
|
||||
let mut buf = Vec::<u8>::with_capacity(digest.len()*2);
|
||||
|
||||
for i in 0..digest.len() {
|
||||
buf.push(HEX_CHARS[(digest[i] >> 4) as usize]);
|
||||
buf.push(HEX_CHARS[(digest[i] & 0xf) as usize]);
|
||||
}
|
||||
|
||||
unsafe { String::from_utf8_unchecked(buf) }
|
||||
}
|
||||
|
||||
pub fn hex_to_digest(hex: &str) -> Result<[u8; 32], Error> {
|
||||
let mut digest = [0u8; 32];
|
||||
|
||||
let bytes = hex.as_bytes();
|
||||
|
||||
if bytes.len() != 64 { bail!("got wrong digest length."); }
|
||||
|
||||
let val = |c| {
|
||||
if c >= b'0' && c <= b'9' { return Ok(c - b'0'); }
|
||||
if c >= b'a' && c <= b'f' { return Ok(c - b'a' + 10); }
|
||||
if c >= b'A' && c <= b'F' { return Ok(c - b'A' + 10); }
|
||||
bail!("found illegal hex character.");
|
||||
};
|
||||
|
||||
let mut pos = 0;
|
||||
for pair in bytes.chunks(2) {
|
||||
if pos >= digest.len() { bail!("hex digest too long."); }
|
||||
let h = val(pair[0])?;
|
||||
let l = val(pair[1])?;
|
||||
digest[pos] = (h<<4)|l;
|
||||
pos +=1;
|
||||
}
|
||||
|
||||
if pos != digest.len() { bail!("hex digest too short."); }
|
||||
|
||||
Ok(digest)
|
||||
}
|
||||
|
||||
pub fn assert_if_modified(digest1: &str, digest2: &str) -> Result<(), Error> {
|
||||
if digest1 != digest2 {
|
||||
|
Reference in New Issue
Block a user