Skip to content

Commit

Permalink
Bump base64 dependency to 0.21 (#2562)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattfbacon authored Oct 18, 2023
1 parent 147bd45 commit c1db909
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ version = "0.6.0"

[dependencies.base64]
optional = true
version = "0.13"
version = "0.21"

[dependencies.levenshtein]
optional = true
Expand Down
7 changes: 5 additions & 2 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ use std::fs::File;
use std::io::Read;
use std::path::Path;

use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
use base64::Engine as _;

use crate::internal::prelude::*;
use crate::model::id::EmojiId;
use crate::model::misc::EmojiIdentifier;

#[cfg(feature = "model")]
pub(crate) fn encode_image(raw: &[u8]) -> String {
let mut encoded = base64::encode(raw);
let mut encoded = BASE64_STANDARD.encode(raw);
encoded.insert_str(0, "data:image/png;base64,");
encoded
}
Expand Down Expand Up @@ -335,7 +338,7 @@ fn _read_image(path: &Path) -> Result<String> {
// errors here are intentionally ignored
drop(f.read_to_end(&mut v));

let b64 = base64::encode(&v);
let b64 = BASE64_STANDARD.encode(&v);
let ext = if path.extension() == Some(OsStr::new("png")) { "png" } else { "jpg" };

Ok(format!("data:image/{};base64,{}", ext, b64))
Expand Down
9 changes: 6 additions & 3 deletions src/utils/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
use std::{fmt, str};

use base64::engine::general_purpose::URL_SAFE as BASE64_URL_SAFE;
use base64::Engine as _;

use crate::model::id::UserId;

/// Validates that a token is likely in a valid format.
Expand Down Expand Up @@ -55,7 +58,7 @@ pub fn parse(token: impl AsRef<str>) -> Option<(UserId, i64)> {
let mut parts = token.as_ref().trim_start_matches("Bot ").split('.');

// First part must be a base64-encoded stringified user ID
let user_id = base64::decode_config(parts.next()?, base64::URL_SAFE).ok()?;
let user_id = BASE64_URL_SAFE.decode(parts.next()?).ok()?;
let user_id = UserId(str::from_utf8(&user_id).ok()?.parse().ok()?);

// Second part must be a base64-encoded token generation timestamp
Expand All @@ -64,7 +67,7 @@ pub fn parse(token: impl AsRef<str>) -> Option<(UserId, i64)> {
if timestamp.len() < 6 {
return None;
}
let timestamp_bytes = base64::decode_config(timestamp, base64::URL_SAFE).ok()?;
let timestamp_bytes = BASE64_URL_SAFE.decode(timestamp).ok()?;
let mut timestamp = 0;
for byte in timestamp_bytes {
timestamp *= 256;
Expand All @@ -76,7 +79,7 @@ pub fn parse(token: impl AsRef<str>) -> Option<(UserId, i64)> {
}

// Third part is a base64-encoded HMAC that's not interesting on its own
base64::decode_config(parts.next()?, base64::URL_SAFE).ok()?;
BASE64_URL_SAFE.decode(parts.next()?).ok()?;

Some((user_id, timestamp))
}

0 comments on commit c1db909

Please sign in to comment.