Skip to content

Commit

Permalink
loose with padding in indy-utils (#1055)
Browse files Browse the repository at this point in the history
Signed-off-by: George Mulhearn <[email protected]>
  • Loading branch information
gmulhearn-anonyome authored Nov 12, 2023
1 parent d4e0bb1 commit baf941f
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions libvdrtools/indy-utils/src/crypto/base64/rust_base64.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
use base64::{engine::general_purpose, Engine};
use base64::{
alphabet,
engine::{general_purpose, DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig},
Engine,
};
use indy_api_types::errors::prelude::*;

/// Default general purpose configuration, but padding decode mode of 'indifferent' (will decode
/// either)
const ANY_PADDING: GeneralPurposeConfig =
GeneralPurposeConfig::new().with_decode_padding_mode(DecodePaddingMode::Indifferent);
/// Standard Base64 URL Safe decoding and encoding, with indifference for padding mode when decoding
const URL_SAFE_ANY_PADDING: GeneralPurpose = GeneralPurpose::new(&alphabet::URL_SAFE, ANY_PADDING);

pub fn encode(doc: &[u8]) -> String {
general_purpose::STANDARD.encode(doc)
}
Expand All @@ -16,7 +27,7 @@ pub fn encode_urlsafe(doc: &[u8]) -> String {
}

pub fn decode_urlsafe(doc: &str) -> Result<Vec<u8>, IndyError> {
general_purpose::URL_SAFE.decode(doc).map_err(|e| {
URL_SAFE_ANY_PADDING.decode(doc).map_err(|e| {
e.to_indy(
IndyErrorKind::InvalidStructure,
"Invalid base64URL_SAFE sequence",
Expand Down Expand Up @@ -55,4 +66,13 @@ mod tests {
assert!(result.is_ok(), "Got error");
assert_eq!(&[1, 2, 3], &result.unwrap()[..]);
}

#[test]
fn decode_urlsafe_works_with_or_without_padding() {
let result = decode_urlsafe("YWJjZA==");
assert_eq!(vec![97, 98, 99, 100], result.unwrap());

let result = decode_urlsafe("YWJjZA");
assert_eq!(vec![97, 98, 99, 100], result.unwrap());
}
}

0 comments on commit baf941f

Please sign in to comment.