mirror of
https://git.proxmox.com/git/proxmox
synced 2025-05-03 13:55:06 +00:00
add hex_to_digest and digest_to_hex
This commit is contained in:
parent
d7fbee66e3
commit
f2716a0d26
@ -11,6 +11,7 @@ authors = [
|
||||
endian_trait = { version = "0.6", features = ["arrays"] }
|
||||
libc = "0.2"
|
||||
valgrind_request = { version = "1.1.0", optional = true }
|
||||
failure = "0.1"
|
||||
|
||||
[features]
|
||||
default = [ "valgrind" ]
|
||||
|
@ -1,5 +1,7 @@
|
||||
//! This is a general utility crate used by all our rust projects.
|
||||
|
||||
use failure::*;
|
||||
|
||||
pub mod io;
|
||||
pub mod vec;
|
||||
|
||||
@ -10,3 +12,44 @@ macro_rules! offsetof {
|
||||
unsafe { &(*(0 as *const $ty)).$field as *const _ as usize }
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user