From fe5077b1a5ef20cd7c3e9a8867d6ae8470e3293d Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 31 Jan 2023 18:01:01 +0100 Subject: [PATCH] Upgrade base64 to 0.21 --- Cargo.toml | 2 +- crates/ruma-common/src/serde/base64.rs | 14 +++++++++----- crates/ruma-signatures/src/functions.rs | 8 ++++---- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 648db7fe..0088c4e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ resolver = "2" [workspace.dependencies] assert_matches = "1.5.0" assign = "1.1.1" -base64 = "0.20.0" +base64 = "0.21.0" criterion = "0.4.0" http = "0.2.8" js_int = "0.2.2" diff --git a/crates/ruma-common/src/serde/base64.rs b/crates/ruma-common/src/serde/base64.rs index f73109c8..df65f3ee 100644 --- a/crates/ruma-common/src/serde/base64.rs +++ b/crates/ruma-common/src/serde/base64.rs @@ -2,7 +2,10 @@ use std::{fmt, marker::PhantomData}; -use base64::engine::fast_portable::{self, FastPortable, FastPortableConfig}; +use base64::{ + engine::{general_purpose, GeneralPurpose, GeneralPurposeConfig}, + Engine, +}; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; /// A wrapper around `B` (usually `Vec`) that (de)serializes from / to a base64 string. @@ -54,8 +57,9 @@ impl Base64Config for UrlSafe { impl Base64 { // See https://github.com/matrix-org/matrix-spec/issues/838 - const CONFIG: FastPortableConfig = fast_portable::NO_PAD.with_decode_allow_trailing_bits(true); - const ENGINE: FastPortable = FastPortable::from(&C::CONF.0, Self::CONFIG); + const CONFIG: GeneralPurposeConfig = + general_purpose::NO_PAD.with_decode_allow_trailing_bits(true); + const ENGINE: GeneralPurpose = GeneralPurpose::new(&C::CONF.0, Self::CONFIG); } impl> Base64 { @@ -71,7 +75,7 @@ impl> Base64 { /// Encode the bytes contained in this `Base64` instance to unpadded base64. pub fn encode(&self) -> String { - base64::encode_engine(self.as_bytes(), &Self::ENGINE) + Self::ENGINE.encode(self.as_bytes()) } } @@ -90,7 +94,7 @@ impl Base64 { /// Parse some base64-encoded data to create a `Base64` instance. pub fn parse(encoded: impl AsRef<[u8]>) -> Result { - base64::decode_engine(encoded, &Self::ENGINE).map(Self::new).map_err(Base64DecodeError) + Self::ENGINE.decode(encoded).map(Self::new).map_err(Base64DecodeError) } } diff --git a/crates/ruma-signatures/src/functions.rs b/crates/ruma-signatures/src/functions.rs index 3e0b89e3..4a760e49 100644 --- a/crates/ruma-signatures/src/functions.rs +++ b/crates/ruma-signatures/src/functions.rs @@ -6,7 +6,7 @@ use std::{ mem, }; -use base64::{alphabet, encode_engine}; +use base64::{alphabet, Engine}; use ruma_common::{ canonical_json::{redact, JsonType}, serde::{base64::Standard, Base64}, @@ -338,12 +338,12 @@ pub fn reference_hash( // Room versions higher than version 3 are url safe base64 encoded _ => alphabet::URL_SAFE, }; - let base64_engine = base64::engine::fast_portable::FastPortable::from( + let base64_engine = base64::engine::GeneralPurpose::new( &base64_alphabet, - base64::engine::fast_portable::NO_PAD, + base64::engine::general_purpose::NO_PAD, ); - Ok(encode_engine(hash, &base64_engine)) + Ok(base64_engine.encode(hash)) } /// Hashes and signs an event and adds the hash and signature to objects under the keys `hashes` and