diff --git a/CHANGELOG.md b/CHANGELOG.md index 95466c0..6b67530 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 118be94..5a8adfe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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"] } @@ -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"] diff --git a/src/crypto/codec/base64/mod.rs b/src/crypto/codec/base64.rs similarity index 100% rename from src/crypto/codec/base64/mod.rs rename to src/crypto/codec/base64.rs diff --git a/src/crypto/codec/hex.rs b/src/crypto/codec/hex.rs new file mode 100644 index 0000000..111b65d --- /dev/null +++ b/src/crypto/codec/hex.rs @@ -0,0 +1,29 @@ +use crate::*; + +#[inline(always)] +pub fn encode>(orig: T) -> String { + hex::encode(orig) +} + +#[inline(always)] +pub fn decode(encoded: &str) -> Result> { + decode_generic(encoded).c(d!()) +} + +#[inline(always)] +pub fn decode_generic>(encoded: T) -> Result> { + 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()); + } +} diff --git a/src/crypto/codec/mod.rs b/src/crypto/codec/mod.rs index 39abdce..20c5369 100644 --- a/src/crypto/codec/mod.rs +++ b/src/crypto/codec/mod.rs @@ -1 +1,2 @@ pub mod base64; +pub mod hex;