Skip to content

Commit 121210c

Browse files
notriddlejebrosen
authored andcommitted
Add support for base16-encoded (a.k.a. hex-encoded) secret keys.
1 parent 9623561 commit 121210c

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

core/lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ state = "0.4.1"
3434
time = "0.1"
3535
memchr = "2" # TODO: Use pear instead.
3636
base64 = "0.10"
37+
base16 = "0.2"
3738
pear = "0.1"
3839
atty = "0.2"
3940

core/lib/src/config/config.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::config::{Table, Value, Array, Datetime};
1010
use crate::http::private::Key;
1111

1212
use super::custom_values::*;
13-
use {num_cpus, base64};
13+
use {num_cpus, base16, base64};
1414

1515
/// Structure for Rocket application configuration.
1616
///
@@ -298,7 +298,7 @@ impl Config {
298298
/// * **workers**: Integer (16-bit unsigned)
299299
/// * **keep_alive**: Integer
300300
/// * **log**: String
301-
/// * **secret_key**: String (256-bit base64)
301+
/// * **secret_key**: String (256-bit base64 or base16)
302302
/// * **tls**: Table (`certs` (path as String), `key` (path as String))
303303
pub(crate) fn set_raw(&mut self, name: &str, val: &Value) -> Result<()> {
304304
let (id, ok) = (|val| val, |_| Ok(()));
@@ -423,11 +423,11 @@ impl Config {
423423
}
424424

425425
/// Sets the `secret_key` in `self` to `key` which must be a 256-bit base64
426-
/// encoded string.
426+
/// or base16 encoded string.
427427
///
428428
/// # Errors
429429
///
430-
/// If `key` is not a valid 256-bit base64 encoded string, returns a
430+
/// If `key` is not a valid 256-bit encoded string, returns a
431431
/// `BadType` error.
432432
///
433433
/// # Example
@@ -438,20 +438,31 @@ impl Config {
438438
/// let mut config = Config::new(Environment::Staging);
439439
/// let key = "8Xui8SN4mI+7egV/9dlfYYLGQJeEx4+DwmSQLwDVXJg=";
440440
/// assert!(config.set_secret_key(key).is_ok());
441+
/// let key = "fe4c5b09a9ac372156e44ce133bc940685ef5e0394d6e9274aadacc21e4f2643";
442+
/// assert!(config.set_secret_key(key).is_ok());
441443
/// assert!(config.set_secret_key("hello? anyone there?").is_err());
442444
/// ```
443445
pub fn set_secret_key<K: Into<String>>(&mut self, key: K) -> Result<()> {
444446
let key = key.into();
445447
let error = self.bad_type("secret_key", "string",
446-
"a 256-bit base64 encoded string");
448+
"a 256-bit base16 or base64 encoded string");
447449

448-
if key.len() != 44 {
449-
return Err(error);
450-
}
451-
452-
let bytes = match base64::decode(&key) {
453-
Ok(bytes) => bytes,
454-
Err(_) => return Err(error)
450+
let bytes = match key.len() {
451+
44 => {
452+
match base64::decode(&key) {
453+
Ok(bytes) => bytes,
454+
Err(_) => return Err(error)
455+
}
456+
}
457+
64 => {
458+
match base16::decode(&key) {
459+
Ok(bytes) => bytes,
460+
Err(_) => return Err(error)
461+
}
462+
}
463+
_ => {
464+
return Err(error)
465+
}
455466
};
456467

457468
self.secret_key = SecretKey::Provided(Key::from_master(&bytes));

site/guide/9-configuration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ limits = { forms = 32768 }
9292
The `workers` and `secret_key` default parameters are computed by Rocket
9393
automatically; the values above are not valid TOML syntax. When manually
9494
specifying the number of workers, the value should be an integer: `workers =
95-
10`. When manually specifying the secret key, the value should a 256-bit base64
96-
encoded string. Such a string can be generated using a tool such as openssl:
97-
`openssl rand -base64 32`.
95+
10`. When manually specifying the secret key, the value should a random 256-bit
96+
value, encoded as a base64 or base16 string. Such a string can be generated
97+
using a tool like openssl: `openssl rand -base64 32`.
9898

9999
The "global" pseudo-environment can be used to set and/or override configuration
100100
parameters globally. A parameter defined in a `[global]` table sets, or

0 commit comments

Comments
 (0)