Skip to content

Commit

Permalink
add hex utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmlm committed Aug 11, 2023
1 parent 4d0ba9c commit c7d8dba
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#### v5.x

- Add more crypto utils
- Add more crypto/codec utils
- Remove features related to `no_std` and `wasm32`

#### v4.x
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ruc"
version = "5.0.13"
version = "5.1.0"
authors = ["rust-util-collections", "hui.fan@mail.ru"]
edition = "2021"
description = "Rust Util Collections"
Expand All @@ -21,6 +21,7 @@ ed25519-zebra = { version = "4.0.0", optional = true }
reference-trie = { version = "0.29.0", optional = true }
rand = { version = "0.8", optional = true }
base64 = {version = "0.21.2", optional = true }
hex = {version = "0.4.3", optional = true }

nix = { version = "0.26", optional = true }
time = { version = "0.3", features = ["formatting"] }
Expand All @@ -36,7 +37,7 @@ compact = []
uau = ["nix","rand"]
cmd = []
ssh = ["ssh2"]
crypto = ["reference-trie", "sha3", "ed25519-zebra", "rand", "base64"]
crypto = ["reference-trie", "sha3", "ed25519-zebra", "rand", "base64", "hex"]
SerDe = ["serde"]

full = ["uau", "cmd", "ssh", "crypto", "SerDe"]
File renamed without changes.
29 changes: 29 additions & 0 deletions src/crypto/codec/hex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::*;

#[inline(always)]
pub fn encode<T: AsRef<[u8]>>(orig: T) -> String {
hex::encode(orig)
}

#[inline(always)]
pub fn decode(encoded: &str) -> Result<Vec<u8>> {
decode_generic(encoded).c(d!())
}

#[inline(always)]
pub fn decode_generic<T: AsRef<[u8]>>(encoded: T) -> Result<Vec<u8>> {
hex::decode(encoded).c(d!())
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn ende() {
let msg = "alajflajfljaljflajfaljlaksjr22142";
let encoded = encode(msg);
let decoded = decode(&encoded).unwrap();
assert_eq!(decoded.as_slice(), msg.as_bytes());
}
}
1 change: 1 addition & 0 deletions src/crypto/codec/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod base64;
pub mod hex;

0 comments on commit c7d8dba

Please sign in to comment.