secure_gate/encoding/
mod.rs1#![cfg_attr(
8 not(any(feature = "encoding-hex", feature = "encoding-base64")),
9 forbid(unsafe_code)
10)]
11
12#[cfg(feature = "encoding-hex")]
13pub mod hex;
14
15#[cfg(feature = "encoding-hex")]
16use ::hex as hex_crate;
17
18#[cfg(feature = "encoding-base64")]
19use ::base64 as base64_crate;
20#[cfg(feature = "encoding-base64")]
21use base64_crate::Engine;
22#[cfg(feature = "encoding-base64")]
23pub mod base64;
24
25#[cfg(feature = "encoding-bech32")]
26pub mod bech32;
27
28#[cfg(any(feature = "encoding-hex", feature = "encoding-base64"))]
50pub trait SecureEncodingExt {
51 #[cfg(feature = "encoding-hex")]
53 fn to_hex(&self) -> alloc::string::String;
54
55 #[cfg(feature = "encoding-hex")]
57 fn to_hex_upper(&self) -> alloc::string::String;
58
59 #[cfg(feature = "encoding-base64")]
61 fn to_base64url(&self) -> alloc::string::String;
62}
63
64#[cfg(feature = "encoding-hex")]
65impl SecureEncodingExt for [u8] {
66 #[cfg(feature = "encoding-hex")]
67 #[inline(always)]
68 fn to_hex(&self) -> alloc::string::String {
69 hex_crate::encode(self)
70 }
71 #[cfg(feature = "encoding-hex")]
72 #[inline(always)]
73 fn to_hex_upper(&self) -> alloc::string::String {
74 hex_crate::encode_upper(self)
75 }
76 #[cfg(feature = "encoding-base64")]
77 #[inline(always)]
78 fn to_base64url(&self) -> alloc::string::String {
79 use ::base64::engine::general_purpose::URL_SAFE_NO_PAD;
80 URL_SAFE_NO_PAD.encode(self)
81 }
82}
83
84#[cfg(feature = "encoding-hex")]
85impl<const N: usize> SecureEncodingExt for [u8; N] {
86 #[cfg(feature = "encoding-hex")]
87 #[inline(always)]
88 fn to_hex(&self) -> alloc::string::String {
89 hex_crate::encode(self)
90 }
91
92 #[cfg(feature = "encoding-hex")]
93 #[inline(always)]
94 fn to_hex_upper(&self) -> alloc::string::String {
95 hex_crate::encode_upper(self)
96 }
97
98 #[cfg(feature = "encoding-base64")]
99 #[inline(always)]
100 fn to_base64url(&self) -> alloc::string::String {
101 use ::base64::engine::general_purpose::URL_SAFE_NO_PAD;
102 URL_SAFE_NO_PAD.encode(self)
103 }
104}