pub(super) fn try_decode_b64_to_utf8(credential: &str) -> Option<String> {
if credential.len() < 8
|| credential.len() > keyhog_core::encoding::MAX_STANDARD_BASE64_INPUT_BYTES
{
return None;
}
let valid = credential.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '+' || c == '/' || c == '=' || c == '-' || c == '_'
});
if !valid {
return None;
}
use base64::engine::general_purpose::{STANDARD, STANDARD_NO_PAD, URL_SAFE, URL_SAFE_NO_PAD};
use base64::Engine;
if let Ok(bytes) = STANDARD.decode(credential) {
if let Ok(s) = std::str::from_utf8(&bytes) {
return Some(s.to_string());
}
}
if let Ok(bytes) = URL_SAFE.decode(credential) {
if let Ok(s) = std::str::from_utf8(&bytes) {
return Some(s.to_string());
}
}
if let Ok(bytes) = STANDARD_NO_PAD.decode(credential) {
if let Ok(s) = std::str::from_utf8(&bytes) {
return Some(s.to_string());
}
}
if let Ok(bytes) = URL_SAFE_NO_PAD.decode(credential) {
if let Ok(s) = std::str::from_utf8(&bytes) {
return Some(s.to_string());
}
}
None
}