ffsend_api/crypto/b64.rs
1//! A simple module for encoding or decoding a base64 string from or to a
2//! byte array.
3//!
4//! This module uses an URL-safe scheme, and doesn't add additional padding
5//! to the encoded strings.
6
7extern crate base64;
8
9pub use self::base64::{CharacterSet, Config, DecodeError};
10
11/// Encode the given byte slice using base64,
12/// in an URL-safe manner without padding.
13pub fn encode(input: &[u8]) -> String {
14 base64::encode_config(input, base64::URL_SAFE_NO_PAD)
15}
16
17/// Decode the given string as base64.
18/// Standard and URL-safe character sets are both supported,
19/// padding is optional.
20pub fn decode(input: &str) -> Result<Vec<u8>, DecodeError> {
21 base64::decode_config(
22 input
23 .replace('+', "-")
24 .replace('/', "_")
25 .trim_end_matches('='),
26 base64::URL_SAFE_NO_PAD,
27 )
28}