ncr/encoding/
base64.rs

1use base64::{
2    alphabet::Alphabet,
3    engine::{general_purpose::PAD, GeneralPurpose},
4    Engine,
5};
6
7use super::Encoding;
8use crate::NcrError;
9
10/// The base64 encoding.
11///
12/// # Important
13///
14/// The character "/" is replaced with "\\" to prevent Minecraft from recognizing this as a command.
15/// See [No Chat Reports](https://github.com/HKS-HNS/No-Chat-Reports/blob/9088c8501e4259325476d0c5a751a368b96036d3/src/main/java/com/aizistral/nochatreports/encryption/Encryptor.java#L93).
16#[derive(Debug)]
17pub struct Base64Encoding;
18
19impl Encoding for Base64Encoding {
20    fn encode(text: &[u8]) -> String {
21        BASE64_ENGINE.encode(text)
22    }
23
24    fn decode(text: &str) -> Result<Vec<u8>, NcrError> {
25        BASE64_ENGINE
26            .decode(text)
27            .map_err(|_| NcrError::DecodeError)
28    }
29}
30
31const BASE64_ALPHABET: Alphabet =
32    match Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\\") {
33        Ok(alphabet) => alphabet,
34        Err(_) => panic!(),
35    };
36const BASE64_ENGINE: GeneralPurpose = GeneralPurpose::new(&BASE64_ALPHABET, PAD);