app_store_server_library/
utils.rs1use base64::engine::general_purpose::STANDARD;
2use base64::{DecodeError, Engine};
3
4pub(crate) fn base64_url_to_base64(encoded_string: &str) -> String {
16 let replaced_string = encoded_string.replace('/', "+").replace('_', "-");
17
18 if replaced_string.len() % 4 != 0 {
19 return replaced_string.clone() + &"=".repeat(4 - replaced_string.len() % 4);
20 }
21
22 replaced_string
23}
24
25pub trait StringExt {
27 fn as_der_bytes(&self) -> Result<Vec<u8>, DecodeError>;
39}
40
41impl StringExt for String {
42 fn as_der_bytes(&self) -> Result<Vec<u8>, DecodeError> {
43 STANDARD.decode(self)
44 }
45}
46
47impl StringExt for &str {
48 fn as_der_bytes(&self) -> Result<Vec<u8>, DecodeError> {
49 STANDARD.decode(self)
50 }
51}
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn test_base64_url_to_base64() {
59 let encoded_string = "aGVsbG8gd29ybGQh";
61 let result = base64_url_to_base64(encoded_string);
62 assert_eq!(result, "aGVsbG8gd29ybGQh");
63
64 let encoded_string_padding = "aGVsbG8gd29ybz";
66 let result_padding = base64_url_to_base64(encoded_string_padding);
67 assert_eq!(result_padding, "aGVsbG8gd29ybz==");
68 }
69}