Generalize base64 decode method, allow all formats for single method

This commit is contained in:
timvisee 2018-03-20 22:46:36 +01:00
parent 4957c98f6d
commit a7da14ec2c
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
4 changed files with 15 additions and 18 deletions

View file

@ -76,7 +76,7 @@ impl<'a> Download<'a> {
// Get the authentication nonce
// TODO: don't unwrap here, return an error
let nonce = b64::decode_standard(
let nonce = b64::decode(
response.headers()
.get_raw(HEADER_AUTH_NONCE)
.expect("missing authenticate header")
@ -115,7 +115,7 @@ impl<'a> Download<'a> {
// Get the metadata nonce
// TODO: don't unwrap here, return an error
let nonce = b64::decode_standard(
let nonce = b64::decode(
response.headers()
.get_raw(HEADER_AUTH_NONCE)
.expect("missing authenticate header")
@ -380,7 +380,7 @@ impl MetadataResponse {
// TODO: do not unwrap, return a proper error
pub fn decrypt_metadata(&self, key_set: &KeySet) -> Result<Metadata> {
// Decode the metadata
let raw = b64::decode_url(&self.meta)
let raw = b64::decode(&self.meta)
.expect("failed to decode metadata from server");
// Get the encrypted metadata, and it's tag

View file

@ -13,17 +13,14 @@ pub fn encode(input: &[u8]) -> String {
base64::encode_config(input, base64::URL_SAFE_NO_PAD)
}
/// Decode the given string as base64, with the given configuration.
pub fn decode(input: &str, config: Config) -> Result<Vec<u8>, DecodeError> {
base64::decode_config(input, config)
}
/// Decode the given string as base64, in an URL-safe manner.
pub fn decode_url(input: &str) -> Result<Vec<u8>, DecodeError> {
decode(input, base64::URL_SAFE_NO_PAD)
}
/// Decode the given string as base64, with the standaard character set.
pub fn decode_standard(input: &str) -> Result<Vec<u8>, DecodeError> {
decode(input, base64::STANDARD)
/// Decode the given string as base64.
/// Standard and URL-safe character sets are both supported,
/// padding is optional.
pub fn decode(input: &str) -> Result<Vec<u8>, DecodeError> {
base64::decode_config(
input.replace('+', "-")
.replace('/', "_")
.trim_right_matches('='),
base64::URL_SAFE_NO_PAD,
)
}

View file

@ -181,7 +181,7 @@ impl DownloadFile {
.ok_or(FileParseError::InvalidSecret)?
.get(1)
{
secret = b64::decode_url(raw.as_str().trim())
secret = b64::decode(raw.as_str().trim())
.map_err(|_| FileParseError::InvalidSecret)?
}
}

View file

@ -51,7 +51,7 @@ impl Metadata {
// TODO: use an input vector length from a constant
pub fn iv(&self) -> [u8; 12] {
// Decode the input vector
let decoded = b64::decode_url(&self.iv).unwrap();
let decoded = b64::decode(&self.iv).unwrap();
// Create a sized array
*array_ref!(decoded, 0, 12)