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(any(feature = "encoding-hex", feature = "encoding-base64"))]
45pub trait SecureEncodingExt {
46 #[cfg(feature = "encoding-hex")]
48 fn to_hex(&self) -> alloc::string::String;
49
50 #[cfg(feature = "encoding-hex")]
52 fn to_hex_upper(&self) -> alloc::string::String;
53
54 #[cfg(feature = "encoding-base64")]
56 fn to_base64url(&self) -> alloc::string::String;
57}
58
59#[cfg(feature = "encoding-hex")]
60impl SecureEncodingExt for [u8] {
61 #[cfg(feature = "encoding-hex")]
62 #[inline(always)]
63 fn to_hex(&self) -> alloc::string::String {
64 hex_crate::encode(self)
65 }
66 #[cfg(feature = "encoding-hex")]
67 #[inline(always)]
68 fn to_hex_upper(&self) -> alloc::string::String {
69 hex_crate::encode_upper(self)
70 }
71 #[cfg(feature = "encoding-base64")]
72 #[inline(always)]
73 fn to_base64url(&self) -> alloc::string::String {
74 use ::base64::engine::general_purpose::URL_SAFE_NO_PAD;
75 URL_SAFE_NO_PAD.encode(self)
76 }
77}
78
79#[cfg(feature = "encoding-hex")]
80impl<const N: usize> SecureEncodingExt for [u8; N] {
81 #[cfg(feature = "encoding-hex")]
82 #[inline(always)]
83 fn to_hex(&self) -> alloc::string::String {
84 hex_crate::encode(self)
85 }
86
87 #[cfg(feature = "encoding-hex")]
88 #[inline(always)]
89 fn to_hex_upper(&self) -> alloc::string::String {
90 hex_crate::encode_upper(self)
91 }
92
93 #[cfg(feature = "encoding-base64")]
94 #[inline(always)]
95 fn to_base64url(&self) -> alloc::string::String {
96 use ::base64::engine::general_purpose::URL_SAFE_NO_PAD;
97 URL_SAFE_NO_PAD.encode(self)
98 }
99}