From f4bfbdf97d926e22eeefd8d956e8a75ee8f1e885 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 19 Dec 2022 17:30:31 +0100 Subject: [PATCH] chore: Upgrade base64 --- Cargo.lock | 8 +++--- Cargo.toml | 2 +- .../matrix-sdk-crypto-ffi/src/verification.rs | 12 ++++++-- crates/matrix-sdk-crypto/src/utilities.rs | 12 ++++++-- .../matrix-sdk-indexeddb/src/safe_encode.rs | 28 +++++++++++-------- 5 files changed, 40 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7b42bcd66..544ed2899 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2672,7 +2672,7 @@ dependencies = [ "assert_matches", "async-trait", "atomic", - "base64 0.13.1", + "base64 0.20.0", "bs58", "byteorder", "cfg-if", @@ -2709,7 +2709,7 @@ name = "matrix-sdk-crypto-ffi" version = "0.1.0" dependencies = [ "anyhow", - "base64 0.13.1", + "base64 0.20.0", "futures-util", "hmac", "http", @@ -2807,7 +2807,7 @@ version = "0.2.0" dependencies = [ "anyhow", "async-trait", - "base64 0.13.1", + "base64 0.20.0", "dashmap", "derive_builder", "getrandom 0.2.8", @@ -2849,7 +2849,7 @@ dependencies = [ name = "matrix-sdk-qrcode" version = "0.4.0" dependencies = [ - "base64 0.13.1", + "base64 0.20.0", "byteorder", "image 0.23.14", "qrcode", diff --git a/Cargo.toml b/Cargo.toml index 02521bb9a..5c5799da4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ rust-version = "1.65" anyhow = "1.0.68" async-stream = "0.3.3" async-trait = "0.1.60" -base64 = "0.13.0" +base64 = "0.20.0" byteorder = "1.4.3" ctor = "0.1.26" dashmap = "5.2.0" diff --git a/bindings/matrix-sdk-crypto-ffi/src/verification.rs b/bindings/matrix-sdk-crypto-ffi/src/verification.rs index b6a9810c0..6eafbb889 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/verification.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/verification.rs @@ -1,6 +1,9 @@ use std::sync::Arc; -use base64::{decode_config, encode_config, STANDARD_NO_PAD}; +use base64::{ + alphabet, decode_engine, encode_engine, + engine::fast_portable::{self, FastPortable}, +}; use futures_util::{Stream, StreamExt}; use matrix_sdk_crypto::{ matrix_sdk_qrcode::QrVerificationData, CancelInfo as RustCancelInfo, QrVerification as InnerQr, @@ -13,6 +16,9 @@ use tokio::runtime::Handle; use crate::{CryptoStoreError, OutgoingVerificationRequest, SignatureUploadRequest}; +const STANDARD_NO_PAD: FastPortable = + FastPortable::from(&alphabet::STANDARD, fast_portable::NO_PAD); + /// Listener that will be passed over the FFI to report changes to a SAS /// verification. pub trait SasListener: Send { @@ -401,7 +407,7 @@ impl QrCode { /// decoded on the other side before it can be put through a QR code /// generator. pub fn generate_qr_code(&self) -> Option { - self.inner.to_bytes().map(|data| encode_config(data, STANDARD_NO_PAD)).ok() + self.inner.to_bytes().map(|data| encode_engine(data, &STANDARD_NO_PAD)).ok() } /// Set a listener for changes in the QrCode verification process. @@ -703,7 +709,7 @@ impl VerificationRequest { /// * `data` - The data that was extracted from the scanned QR code as an /// base64 encoded string, without padding. pub fn scan_qr_code(&self, data: &str) -> Option { - let data = decode_config(data, STANDARD_NO_PAD).ok()?; + let data = decode_engine(data, &STANDARD_NO_PAD).ok()?; let data = QrVerificationData::from_bytes(data).ok()?; if let Some(qr) = self.runtime.block_on(self.inner.scan_qr_code(data)).ok()? { diff --git a/crates/matrix-sdk-crypto/src/utilities.rs b/crates/matrix-sdk-crypto/src/utilities.rs index b8f2804e3..032dbc5cc 100644 --- a/crates/matrix-sdk-crypto/src/utilities.rs +++ b/crates/matrix-sdk-crypto/src/utilities.rs @@ -13,16 +13,22 @@ // limitations under the License. pub use base64::DecodeError; -use base64::{decode_config, encode_config, STANDARD_NO_PAD}; +use base64::{ + alphabet, decode_engine, encode_engine, + engine::fast_portable::{self, FastPortable}, +}; + +const STANDARD_NO_PAD: FastPortable = + FastPortable::from(&alphabet::STANDARD, fast_portable::NO_PAD); /// Decode the input as base64 with no padding. pub fn decode(input: impl AsRef<[u8]>) -> Result, DecodeError> { - decode_config(input, STANDARD_NO_PAD) + decode_engine(input, &STANDARD_NO_PAD) } /// Encode the input as base64 with no padding. pub fn encode(input: impl AsRef<[u8]>) -> String { - encode_config(input, STANDARD_NO_PAD) + encode_engine(input, &STANDARD_NO_PAD) } #[cfg(test)] diff --git a/crates/matrix-sdk-indexeddb/src/safe_encode.rs b/crates/matrix-sdk-indexeddb/src/safe_encode.rs index e26486088..4838bda40 100644 --- a/crates/matrix-sdk-indexeddb/src/safe_encode.rs +++ b/crates/matrix-sdk-indexeddb/src/safe_encode.rs @@ -1,5 +1,8 @@ #![allow(dead_code)] -use base64::{encode_config as base64_encode, STANDARD_NO_PAD}; +use base64::{ + alphabet, encode_engine as base64_encode, + engine::fast_portable::{self, FastPortable}, +}; use matrix_sdk_store_encryption::StoreCipher; use ruma::{ events::{ @@ -22,6 +25,9 @@ pub const RANGE_END: &str = "\u{001E}"; /// (though super unlikely) pub const ESCAPED: &str = "\u{001E}\u{001D}"; +const STANDARD_NO_PAD: FastPortable = + FastPortable::from(&alphabet::STANDARD, fast_portable::NO_PAD); + /// Encode value as String/JsValue/IdbKeyRange for the JS APIs in a /// safe, escaped manner. /// @@ -54,7 +60,7 @@ pub trait SafeEncode { fn as_secure_string(&self, table_name: &str, store_cipher: &StoreCipher) -> String { base64_encode( store_cipher.hash_key(table_name, self.as_encoded_string().as_bytes()), - STANDARD_NO_PAD, + &STANDARD_NO_PAD, ) } @@ -116,12 +122,12 @@ where [ &base64_encode( store_cipher.hash_key(table_name, self.0.as_encoded_string().as_bytes()), - STANDARD_NO_PAD, + &STANDARD_NO_PAD, ), KEY_SEPARATOR, &base64_encode( store_cipher.hash_key(table_name, self.1.as_encoded_string().as_bytes()), - STANDARD_NO_PAD, + &STANDARD_NO_PAD, ), ] .concat() @@ -151,17 +157,17 @@ where [ &base64_encode( store_cipher.hash_key(table_name, self.0.as_encoded_string().as_bytes()), - STANDARD_NO_PAD, + &STANDARD_NO_PAD, ), KEY_SEPARATOR, &base64_encode( store_cipher.hash_key(table_name, self.1.as_encoded_string().as_bytes()), - STANDARD_NO_PAD, + &STANDARD_NO_PAD, ), KEY_SEPARATOR, &base64_encode( store_cipher.hash_key(table_name, self.2.as_encoded_string().as_bytes()), - STANDARD_NO_PAD, + &STANDARD_NO_PAD, ), ] .concat() @@ -194,22 +200,22 @@ where [ &base64_encode( store_cipher.hash_key(table_name, self.0.as_encoded_string().as_bytes()), - STANDARD_NO_PAD, + &STANDARD_NO_PAD, ), KEY_SEPARATOR, &base64_encode( store_cipher.hash_key(table_name, self.1.as_encoded_string().as_bytes()), - STANDARD_NO_PAD, + &STANDARD_NO_PAD, ), KEY_SEPARATOR, &base64_encode( store_cipher.hash_key(table_name, self.2.as_encoded_string().as_bytes()), - STANDARD_NO_PAD, + &STANDARD_NO_PAD, ), KEY_SEPARATOR, &base64_encode( store_cipher.hash_key(table_name, self.3.as_encoded_string().as_bytes()), - STANDARD_NO_PAD, + &STANDARD_NO_PAD, ), ] .concat()