Created
October 7, 2017 07:25
-
-
Save JFreegman/05e18d36947874a46ad3e46f73ab0caf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts a string representation of a key into a byte array. The supplied | |
* string must contain 64 hex-characters. | |
* | |
* Returns a byte array of length 32 on success. | |
* Returns None on failure. | |
*/ | |
pub fn key_string_to_bytes(s: &str) -> Option<[u8; 32]> { | |
if s.len() < 64 { | |
return None; | |
} | |
let mut result = [0; 32]; | |
let mut idx = 0; | |
for i in 0..result.len() { | |
match u8::from_str_radix(&s[idx..idx+2], 16) { | |
Ok(byte) => result[i] = byte, | |
Err(e) => { | |
error!("Failed to convert string: {}; err={:?}", s, e); | |
return None; | |
}, | |
}; | |
idx += 2; | |
} | |
return Some(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment