Migrate to 'binascii' for base 16, 64 decoding.

This commit is contained in:
Sergio Benitez 2020-02-14 17:14:37 -08:00
parent 9f0e02fe27
commit 385b69cf69
2 changed files with 16 additions and 23 deletions

View File

@ -33,8 +33,7 @@ num_cpus = "1.0"
state = "0.4.1"
time = "0.2.4"
memchr = "2" # TODO: Use pear instead.
base64 = "0.11"
base16 = "0.2"
binascii = "0.1"
pear = "0.1"
atty = "0.2"

View File

@ -10,7 +10,6 @@ use crate::config::{Table, Value, Array, Datetime};
use crate::http::private::Key;
use super::custom_values::*;
use {num_cpus, base16, base64};
/// Structure for Rocket application configuration.
///
@ -423,12 +422,12 @@ impl Config {
}
/// Sets the `secret_key` in `self` to `key` which must be a 256-bit base64
/// or base16 encoded string.
/// or base16 (hex) encoded string.
///
/// # Errors
///
/// If `key` is not a valid 256-bit encoded string, returns a
/// `BadType` error.
/// If `key` is not a valid 256-bit encoded string, returns a `BadType`
/// error.
///
/// # Example
///
@ -436,33 +435,28 @@ impl Config {
/// use rocket::config::{Config, Environment};
///
/// let mut config = Config::new(Environment::Staging);
///
/// // A base64 encoded key.
/// let key = "8Xui8SN4mI+7egV/9dlfYYLGQJeEx4+DwmSQLwDVXJg=";
/// assert!(config.set_secret_key(key).is_ok());
///
/// // A base16 (hex) encoded key.
/// let key = "fe4c5b09a9ac372156e44ce133bc940685ef5e0394d6e9274aadacc21e4f2643";
/// assert!(config.set_secret_key(key).is_ok());
///
/// // An invalid key.
/// assert!(config.set_secret_key("hello? anyone there?").is_err());
/// ```
pub fn set_secret_key<K: Into<String>>(&mut self, key: K) -> Result<()> {
let key = key.into();
let error = self.bad_type("secret_key", "string",
"a 256-bit base16 or base64 encoded string");
let e = self.bad_type("secret_key", "string", "a 256-bit base64 or hex encoded string");
// `binascii` requires a bit more space than actual output for padding
let mut bytes = [0u8; 36];
let bytes = match key.len() {
44 => {
match base64::decode(&key) {
Ok(bytes) => bytes,
Err(_) => return Err(error)
}
}
64 => {
match base16::decode(&key) {
Ok(bytes) => bytes,
Err(_) => return Err(error)
}
}
_ => {
return Err(error)
}
44 => binascii::b64decode(key.as_bytes(), &mut bytes).map_err(|_| e)?,
64 => binascii::hex2bin(key.as_bytes(), &mut bytes).map_err(|_| e)?,
_ => return Err(e)
};
self.secret_key = SecretKey::Provided(Key::from_master(&bytes));