mwc_util/
hex.rs

1// Copyright 2019 The Grin Developers
2// Copyright 2024 The MWC Developers
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16/// Implements hex-encoding from bytes to string and decoding of strings
17/// to bytes. Given that rustc-serialize is deprecated and serde doesn't
18/// provide easy hex encoding, hex is a bit in limbo right now in Rust-
19/// land. It's simple enough that we can just have our own.
20use std::fmt::Write;
21
22/// Encode the provided bytes into a hex string
23pub fn to_hex(bytes: &[u8]) -> String {
24	let mut s = String::with_capacity(bytes.len() * 2);
25	for byte in bytes {
26		write!(&mut s, "{:02x}", byte).expect("Unable to write hex");
27	}
28	s
29}
30
31/// Convert to hex
32pub trait ToHex {
33	/// convert to hex
34	fn to_hex(&self) -> String;
35}
36
37impl<T: AsRef<[u8]>> ToHex for T {
38	fn to_hex(&self) -> String {
39		to_hex(self.as_ref())
40	}
41}
42
43/// Decode a hex string into bytes.
44pub fn from_hex(hex: &str) -> Result<Vec<u8>, String> {
45	let hex = hex.trim().trim_start_matches("0x");
46	if hex.len() % 2 != 0 {
47		Err(hex.to_string())
48	} else {
49		(0..hex.len())
50			.step_by(2)
51			.map(|i| u8::from_str_radix(&hex[i..i + 2], 16).map_err(|_| hex.to_string()))
52			.collect()
53	}
54}
55
56#[cfg(test)]
57mod test {
58	use super::*;
59
60	#[test]
61	fn test_to_hex() {
62		assert_eq!(vec![0, 0, 0, 0].to_hex(), "00000000");
63		assert_eq!(vec![10, 11, 12, 13].to_hex(), "0a0b0c0d");
64		assert_eq!([0, 0, 0, 255].to_hex(), "000000ff");
65	}
66
67	#[test]
68	fn test_to_hex_trait() {
69		assert_eq!(vec![0, 0, 0, 0].to_hex(), "00000000");
70		assert_eq!(vec![10, 11, 12, 13].to_hex(), "0a0b0c0d");
71		assert_eq!([0, 0, 0, 255].to_hex(), "000000ff");
72	}
73
74	#[test]
75	fn test_from_hex() {
76		assert_eq!(from_hex(""), Ok(vec![]));
77		assert_eq!(from_hex("00000000"), Ok(vec![0, 0, 0, 0]));
78		assert_eq!(from_hex("0a0b0c0d"), Ok(vec![10, 11, 12, 13]));
79		assert_eq!(from_hex("000000ff"), Ok(vec![0, 0, 0, 255]));
80		assert_eq!(from_hex("0x000000ff"), Ok(vec![0, 0, 0, 255]));
81		assert_eq!(from_hex("0x000000fF"), Ok(vec![0, 0, 0, 255]));
82		assert_eq!(from_hex("0x000000fg"), Err("000000fg".to_string()));
83		assert_eq!(
84			from_hex("not a hex string"),
85			Err("not a hex string".to_string())
86		);
87		assert_eq!(from_hex("0"), Err("0".to_string()));
88	}
89}