44//
55// Copyright (c) DUSK NETWORK. All rights reserved.
66
7+ use std:: borrow:: Borrow ;
78use std:: fmt:: Display ;
89use std:: path:: PathBuf ;
910use std:: str:: FromStr ;
@@ -35,10 +36,11 @@ use rusk_wallet::{PBKDF2_ROUNDS, SALT_SIZE};
3536use sha2:: { Digest , Sha256 } ;
3637
3738use crate :: command:: TransactionHistory ;
39+ use crate :: zeroizing_bytes:: ZeroizingBytes ;
3840
3941pub ( crate ) trait Prompt {
4042 /// Prompt the user to enter a password
41- fn create_new_password ( & self ) -> InquireResult < String > {
43+ fn create_new_password ( & self ) -> InquireResult < ZeroizingBytes > {
4244 create_new_password ( )
4345 }
4446
@@ -52,37 +54,36 @@ pub(crate) struct Prompter;
5254
5355impl Prompt for Prompter { }
5456
55- pub ( crate ) fn ask_pwd ( msg : & str ) -> Result < String , InquireError > {
57+ pub ( crate ) fn ask_pwd ( msg : & str ) -> Result < ZeroizingBytes , InquireError > {
5658 let pwd = Password :: new ( msg)
5759 . with_display_toggle_enabled ( )
5860 . without_confirmation ( )
5961 . with_display_mode ( PasswordDisplayMode :: Masked )
6062 . prompt ( ) ;
6163
62- pwd
64+ pwd. map ( ZeroizingBytes :: from )
6365}
6466
65- pub ( crate ) fn create_new_password ( ) -> Result < String , InquireError > {
67+ pub ( crate ) fn create_new_password ( ) -> Result < ZeroizingBytes , InquireError > {
6668 let pwd = Password :: new ( "Password:" )
6769 . with_display_toggle_enabled ( )
6870 . with_display_mode ( PasswordDisplayMode :: Hidden )
6971 . with_custom_confirmation_message ( "Confirm password: " )
7072 . with_custom_confirmation_error_message ( "The passwords doesn't match" )
7173 . prompt ( ) ;
7274
73- pwd
75+ pwd. map ( ZeroizingBytes :: from )
7476}
7577
7678/// Request the user to authenticate with a password and return the derived key
7779pub ( crate ) fn derive_key_from_password (
7880 msg : & str ,
79- password : & Option < String > ,
81+ password : & Option < ZeroizingBytes > ,
8082 salt : Option < & [ u8 ; SALT_SIZE ] > ,
8183 file_version : DatFileVersion ,
82- ) -> anyhow:: Result < Vec < u8 > > {
84+ ) -> anyhow:: Result < ZeroizingBytes > {
8385 let pwd = match password. as_ref ( ) {
84- Some ( p) => p. to_string ( ) ,
85-
86+ Some ( p) => p. clone ( ) ,
8687 None => ask_pwd ( msg) ?,
8788 } ;
8889
@@ -91,13 +92,13 @@ pub(crate) fn derive_key_from_password(
9192
9293/// Request the user to create a wallet password and return the derived key
9394pub ( crate ) fn derive_key_from_new_password (
94- password : & Option < String > ,
95+ password : & Option < ZeroizingBytes > ,
9596 salt : Option < & [ u8 ; SALT_SIZE ] > ,
9697 file_version : DatFileVersion ,
9798 prompter : & dyn Prompt ,
98- ) -> anyhow:: Result < Vec < u8 > > {
99+ ) -> anyhow:: Result < ZeroizingBytes > {
99100 let pwd = match password. as_ref ( ) {
100- Some ( p) => p. to_string ( ) ,
101+ Some ( p) => p. clone ( ) ,
101102 None => prompter. create_new_password ( ) ?,
102103 } ;
103104
@@ -155,28 +156,29 @@ pub(crate) fn request_mnemonic_phrase(
155156
156157pub ( crate ) fn derive_key (
157158 file_version : DatFileVersion ,
158- pwd : & str ,
159+ pwd : & ZeroizingBytes ,
159160 salt : Option < & [ u8 ; SALT_SIZE ] > ,
160- ) -> anyhow:: Result < Vec < u8 > > {
161+ ) -> anyhow:: Result < ZeroizingBytes > {
161162 match file_version {
162163 DatFileVersion :: RuskBinaryFileFormat ( version) => {
163164 if version_without_pre_higher ( version) >= ( 0 , 0 , 2 , 0 ) {
164165 let salt = salt
165166 . ok_or_else ( || anyhow:: anyhow!( "Couldn't find the salt" ) ) ?;
166167 Ok ( pbkdf2:: pbkdf2_hmac_array :: < Sha256 , SALT_SIZE > (
167- pwd. as_bytes ( ) ,
168+ pwd. borrow ( ) ,
168169 salt,
169170 PBKDF2_ROUNDS ,
170171 )
171172 . to_vec ( ) )
172173 } else {
173174 let mut hasher = Sha256 :: new ( ) ;
174- hasher. update ( pwd . as_bytes ( ) ) ;
175+ hasher. update ( Borrow :: < [ u8 ] > :: borrow ( pwd ) ) ;
175176 Ok ( hasher. finalize ( ) . to_vec ( ) )
176177 }
177178 }
178- _ => Ok ( blake3:: hash ( pwd. as_bytes ( ) ) . as_bytes ( ) . to_vec ( ) ) ,
179+ _ => Ok ( blake3:: hash ( pwd. borrow ( ) ) . as_bytes ( ) . to_vec ( ) ) ,
179180 }
181+ . map ( ZeroizingBytes :: from)
180182}
181183
182184/// Request a directory
0 commit comments