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