feat: v1.9.4

This commit is contained in:
2024-06-16 00:07:50 +08:00
parent 320664bfa0
commit 32ab2d3d6d
21 changed files with 60 additions and 39 deletions

View File

@@ -1,5 +1,6 @@
use base64::{DecodeError, Engine};
use base64::engine::general_purpose::{STANDARD, URL_SAFE_NO_PAD};
use rust_util::XResult;
pub fn base64_encode<T: AsRef<[u8]>>(input: T) -> String {
STANDARD.encode(input)
@@ -12,3 +13,13 @@ pub fn base64_encode_url_safe_no_pad<T: AsRef<[u8]>>(input: T) -> String {
pub fn base64_decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, DecodeError> {
STANDARD.decode(input)
}
pub fn try_decode(input: &str) -> XResult<Vec<u8>> {
return match hex::decode(input) {
Ok(v) => Ok(v),
Err(_) => match base64_decode(input) {
Ok(v) => Ok(v),
Err(e) => simple_error!("decode hex or base64 error: {}", e),
}
};
}