@@ -7,6 +7,7 @@ use std::fmt;
77use std:: str:: FromStr ;
88
99use chrono:: { DateTime , Duration , TimeZone , Utc } ;
10+ use data_encoding:: BASE64URL_NOPAD ;
1011use hmac:: { Hmac , Mac } ;
1112use rand:: { rngs:: OsRng , thread_rng, RngCore } ;
1213use serde:: { de:: DeserializeOwned , Deserialize , Serialize } ;
@@ -201,11 +202,11 @@ impl SecretKey {
201202 pub fn sign_with_header ( & self , data : & [ u8 ] , header : & SignatureHeader ) -> String {
202203 let mut header =
203204 serde_json:: to_vec ( & header) . expect ( "attempted to pack non json safe header" ) ;
204- let header_encoded = base64 :: encode_config ( & header[ .. ] , base64 :: URL_SAFE_NO_PAD ) ;
205+ let header_encoded = BASE64URL_NOPAD . encode ( & header) ;
205206 header. push ( b'\x00' ) ;
206207 header. extend_from_slice ( data) ;
207208 let sig = self . inner . sign :: < Sha512 > ( & header) ;
208- let mut sig_encoded = base64 :: encode_config ( & sig. to_bytes ( ) [ .. ] , base64 :: URL_SAFE_NO_PAD ) ;
209+ let mut sig_encoded = BASE64URL_NOPAD . encode ( & sig. to_bytes ( ) ) ;
209210 sig_encoded. push ( '.' ) ;
210211 sig_encoded. push_str ( & header_encoded) ;
211212 sig_encoded
@@ -242,7 +243,7 @@ impl FromStr for SecretKey {
242243 type Err = KeyParseError ;
243244
244245 fn from_str ( s : & str ) -> Result < SecretKey , KeyParseError > {
245- let bytes = match base64 :: decode_config ( s , base64 :: URL_SAFE_NO_PAD ) {
246+ let bytes = match BASE64URL_NOPAD . decode ( s . as_bytes ( ) ) {
246247 Ok ( bytes) => bytes,
247248 _ => return Err ( KeyParseError :: BadEncoding ) ,
248249 } ;
@@ -263,16 +264,12 @@ impl FromStr for SecretKey {
263264impl fmt:: Display for SecretKey {
264265 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
265266 if f. alternate ( ) {
266- write ! (
267- f,
268- "{}" ,
269- base64:: encode_config( & self . inner. to_bytes( ) [ ..] , base64:: URL_SAFE_NO_PAD )
270- )
267+ write ! ( f, "{}" , BASE64URL_NOPAD . encode( & self . inner. to_bytes( ) ) )
271268 } else {
272269 write ! (
273270 f,
274271 "{}" ,
275- base64 :: encode_config ( & self . inner. secret. to_bytes( ) [ .. ] , base64 :: URL_SAFE_NO_PAD )
272+ BASE64URL_NOPAD . encode ( & self . inner. secret. to_bytes( ) )
276273 )
277274 }
278275 }
@@ -292,17 +289,13 @@ impl PublicKey {
292289 pub fn verify_meta ( & self , data : & [ u8 ] , sig : & str ) -> Option < SignatureHeader > {
293290 let mut iter = sig. splitn ( 2 , '.' ) ;
294291 let sig_bytes = match iter. next ( ) {
295- Some ( sig_encoded) => {
296- base64:: decode_config ( sig_encoded, base64:: URL_SAFE_NO_PAD ) . ok ( ) ?
297- }
292+ Some ( sig_encoded) => BASE64URL_NOPAD . decode ( sig_encoded. as_bytes ( ) ) . ok ( ) ?,
298293 None => return None ,
299294 } ;
300295 let sig = ed25519_dalek:: Signature :: from_bytes ( & sig_bytes) . ok ( ) ?;
301296
302297 let header = match iter. next ( ) {
303- Some ( header_encoded) => {
304- base64:: decode_config ( header_encoded, base64:: URL_SAFE_NO_PAD ) . ok ( ) ?
305- }
298+ Some ( header_encoded) => BASE64URL_NOPAD . decode ( header_encoded. as_bytes ( ) ) . ok ( ) ?,
306299 None => return None ,
307300 } ;
308301 let mut to_verify = header. clone ( ) ;
@@ -373,7 +366,7 @@ impl FromStr for PublicKey {
373366 type Err = KeyParseError ;
374367
375368 fn from_str ( s : & str ) -> Result < PublicKey , KeyParseError > {
376- let bytes = match base64 :: decode_config ( s , base64 :: URL_SAFE_NO_PAD ) {
369+ let bytes = match BASE64URL_NOPAD . decode ( s . as_bytes ( ) ) {
377370 Ok ( bytes) => bytes,
378371 _ => return Err ( KeyParseError :: BadEncoding ) ,
379372 } ;
@@ -386,11 +379,7 @@ impl FromStr for PublicKey {
386379
387380impl fmt:: Display for PublicKey {
388381 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
389- write ! (
390- f,
391- "{}" ,
392- base64:: encode_config( & self . inner. to_bytes( ) [ ..] , base64:: URL_SAFE_NO_PAD )
393- )
382+ write ! ( f, "{}" , BASE64URL_NOPAD . encode( & self . inner. to_bytes( ) ) )
394383 }
395384}
396385
@@ -445,11 +434,11 @@ impl SignedRegisterState {
445434 /// Signs the given `RegisterState` and serializes it into a single string.
446435 fn sign ( state : RegisterState , secret : & [ u8 ] ) -> Self {
447436 let json = serde_json:: to_string ( & state) . expect ( "relay register state serializes to JSON" ) ;
448- let token = base64 :: encode_config ( & json, base64 :: URL_SAFE_NO_PAD ) ;
437+ let token = BASE64URL_NOPAD . encode ( json. as_bytes ( ) ) ;
449438
450439 let mut mac = Self :: mac ( secret) ;
451440 mac. input ( token. as_bytes ( ) ) ;
452- let signature = base64 :: encode_config ( & mac. result ( ) . code ( ) , base64 :: URL_SAFE_NO_PAD ) ;
441+ let signature = BASE64URL_NOPAD . encode ( & mac. result ( ) . code ( ) ) ;
453442
454443 Self ( format ! ( "{token}:{signature}" ) )
455444 }
@@ -475,14 +464,16 @@ impl SignedRegisterState {
475464 max_age : Option < Duration > ,
476465 ) -> Result < RegisterState , UnpackError > {
477466 let ( token, signature) = self . split ( ) ;
478- let code = base64:: decode_config ( signature, base64:: URL_SAFE_NO_PAD )
467+ let code = BASE64URL_NOPAD
468+ . decode ( signature. as_bytes ( ) )
479469 . map_err ( |_| UnpackError :: BadEncoding ) ?;
480470
481471 let mut mac = Self :: mac ( secret) ;
482472 mac. input ( token. as_bytes ( ) ) ;
483473 mac. verify ( & code) . map_err ( |_| UnpackError :: BadSignature ) ?;
484474
485- let json = base64:: decode_config ( token, base64:: URL_SAFE_NO_PAD )
475+ let json = BASE64URL_NOPAD
476+ . decode ( token. as_bytes ( ) )
486477 . map_err ( |_| UnpackError :: BadEncoding ) ?;
487478 let state =
488479 serde_json:: from_slice :: < RegisterState > ( & json) . map_err ( UnpackError :: BadPayload ) ?;
@@ -539,7 +530,7 @@ fn nonce() -> String {
539530 let mut rng = thread_rng ( ) ;
540531 let mut bytes = vec ! [ 0u8 ; 64 ] ;
541532 rng. fill_bytes ( & mut bytes) ;
542- base64 :: encode_config ( & bytes, base64 :: URL_SAFE_NO_PAD )
533+ BASE64URL_NOPAD . encode ( & bytes)
543534}
544535
545536/// Represents a request for registration with the upstream.
0 commit comments