@@ -10,7 +10,7 @@ use crate::config::{Table, Value, Array, Datetime};
10
10
use crate :: http:: private:: Key ;
11
11
12
12
use super :: custom_values:: * ;
13
- use { num_cpus, base64} ;
13
+ use { num_cpus, base16 , base64} ;
14
14
15
15
/// Structure for Rocket application configuration.
16
16
///
@@ -298,7 +298,7 @@ impl Config {
298
298
/// * **workers**: Integer (16-bit unsigned)
299
299
/// * **keep_alive**: Integer
300
300
/// * **log**: String
301
- /// * **secret_key**: String (256-bit base64)
301
+ /// * **secret_key**: String (256-bit base64 or base16 )
302
302
/// * **tls**: Table (`certs` (path as String), `key` (path as String))
303
303
pub ( crate ) fn set_raw ( & mut self , name : & str , val : & Value ) -> Result < ( ) > {
304
304
let ( id, ok) = ( |val| val, |_| Ok ( ( ) ) ) ;
@@ -423,11 +423,11 @@ impl Config {
423
423
}
424
424
425
425
/// Sets the `secret_key` in `self` to `key` which must be a 256-bit base64
426
- /// encoded string.
426
+ /// or base16 encoded string.
427
427
///
428
428
/// # Errors
429
429
///
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
431
431
/// `BadType` error.
432
432
///
433
433
/// # Example
@@ -438,20 +438,31 @@ impl Config {
438
438
/// let mut config = Config::new(Environment::Staging);
439
439
/// let key = "8Xui8SN4mI+7egV/9dlfYYLGQJeEx4+DwmSQLwDVXJg=";
440
440
/// assert!(config.set_secret_key(key).is_ok());
441
+ /// let key = "fe4c5b09a9ac372156e44ce133bc940685ef5e0394d6e9274aadacc21e4f2643";
442
+ /// assert!(config.set_secret_key(key).is_ok());
441
443
/// assert!(config.set_secret_key("hello? anyone there?").is_err());
442
444
/// ```
443
445
pub fn set_secret_key < K : Into < String > > ( & mut self , key : K ) -> Result < ( ) > {
444
446
let key = key. into ( ) ;
445
447
let error = self . bad_type ( "secret_key" , "string" ,
446
- "a 256-bit base64 encoded string" ) ;
448
+ "a 256-bit base16 or base64 encoded string" ) ;
447
449
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
+ }
455
466
} ;
456
467
457
468
self . secret_key = SecretKey :: Provided ( Key :: from_master ( & bytes) ) ;
0 commit comments