@@ -7,10 +7,10 @@ use crate::cryptograpy;
77use crate :: otp:: otp_element:: OTPElement ;
88
99
10- pub fn read_from_file ( ) -> Result < Vec < OTPElement > , String > {
10+ pub fn read_from_file ( password : & str ) -> Result < Vec < OTPElement > , String > {
1111 let encrypted_contents = read_to_string ( & get_db_path ( ) ) . unwrap ( ) ;
1212 //rust close files at the end of the function
13- let contents = cryptograpy:: decrypt_string ( & encrypted_contents, & cryptograpy :: prompt_for_passwords ( "Password: " , 8 ) ) ;
13+ let contents = cryptograpy:: decrypt_string ( & encrypted_contents, password ) ;
1414 match contents {
1515 Ok ( contents) => {
1616 let vector: Vec < OTPElement > = serde_json:: from_str ( & contents) . unwrap ( ) ;
@@ -36,14 +36,15 @@ pub fn add_element(secret: &String,issuer: &String,label: &String,algorithm: &st
3636 if !check_secret ( & upper_secret) {
3737 return Err ( String :: from ( "Bad secret" ) )
3838 }
39+ let pw = & cryptograpy:: prompt_for_passwords ( "Password: " , 8 , false ) ;
3940 let otp_element = OTPElement :: new ( upper_secret. to_string ( ) , issuer. to_string ( ) , label. to_string ( ) , digits, String :: from ( "TOTP" ) , String :: from ( algorithm) . to_uppercase ( ) , String :: from ( "Default" ) , 0 , 0 , 30 , vec ! [ ] ) ;
4041 let mut elements;
41- match read_from_file ( ) {
42+ match read_from_file ( pw ) {
4243 Ok ( result) => elements = result,
4344 Err ( e) => return Err ( e)
4445 }
4546 elements. push ( otp_element) ;
46- match overwrite_database ( elements) {
47+ match overwrite_database ( elements, pw ) {
4748 Ok ( ( ) ) => Ok ( ( ) ) ,
4849 Err ( e) => Err ( format ! ( "{}" , e) )
4950 }
@@ -57,8 +58,8 @@ pub fn remove_element_from_db(mut id: usize) -> Result<(),String>{
5758 id -= 1 ;
5859
5960 let mut elements: Vec < OTPElement > ;
60-
61- match read_from_file ( ) {
61+ let pw = & cryptograpy :: prompt_for_passwords ( "Password: " , 8 , false ) ;
62+ match read_from_file ( pw ) {
6263 Ok ( result) => elements = result,
6364 Err ( e) => {
6465 return Err ( e) ;
@@ -73,7 +74,7 @@ pub fn remove_element_from_db(mut id: usize) -> Result<(),String>{
7374 break ;
7475 }
7576 }
76- match overwrite_database ( elements) {
77+ match overwrite_database ( elements, pw ) {
7778 Ok ( ( ) ) => Ok ( ( ) ) ,
7879 Err ( e) => Err ( format ! ( "{}" , e) ) ,
7980 }
@@ -89,7 +90,8 @@ pub fn edit_element(mut id: usize, secret: &str,issuer: &str,label: &str,algorit
8990 id -= 1 ;
9091
9192 let mut elements: Vec < OTPElement > ;
92- match read_from_file ( ) {
93+ let pw = & cryptograpy:: prompt_for_passwords ( "Password: " , 8 , false ) ;
94+ match read_from_file ( pw) {
9395 Ok ( result) => elements = result,
9496 Err ( _e) => return Err ( String :: from ( "Cannot decrypt existing database" ) )
9597 }
@@ -116,7 +118,7 @@ pub fn edit_element(mut id: usize, secret: &str,issuer: &str,label: &str,algorit
116118 break ;
117119 }
118120 }
119- match overwrite_database ( elements) {
121+ match overwrite_database ( elements, pw ) {
120122 Ok ( ( ) ) => Ok ( ( ) ) ,
121123 Err ( e) => Err ( format ! ( "{}" , e) ) ,
122124 }
@@ -130,7 +132,7 @@ pub fn export_database() -> Result<String, String> {
130132 exported_path. push_str ( "/exported.cotp" ) ;
131133 let mut file = File :: create ( & exported_path) . expect ( "Cannot create file" ) ;
132134 let encrypted_contents = read_to_string ( & get_db_path ( ) ) . unwrap ( ) ;
133- let contents = cryptograpy:: decrypt_string ( & encrypted_contents, & cryptograpy:: prompt_for_passwords ( "Password: " , 8 ) ) ;
135+ let contents = cryptograpy:: decrypt_string ( & encrypted_contents, & cryptograpy:: prompt_for_passwords ( "Password: " , 8 , false ) ) ;
134136 match contents {
135137 Ok ( contents) => {
136138 if contents == "[]" {
@@ -145,13 +147,13 @@ pub fn export_database() -> Result<String, String> {
145147 }
146148}
147149
148- pub fn overwrite_database ( elements : Vec < OTPElement > ) -> Result < ( ) , std:: io:: Error > {
150+ pub fn overwrite_database ( elements : Vec < OTPElement > , password : & str ) -> Result < ( ) , std:: io:: Error > {
149151 let json_string: & str = & serde_json:: to_string ( & elements) ?;
150- overwrite_database_json ( json_string)
152+ overwrite_database_json ( json_string, password )
151153}
152154
153- pub fn overwrite_database_json ( json : & str ) -> Result < ( ) , std:: io:: Error > {
154- let encrypted = cryptograpy:: encrypt_string ( json. to_string ( ) , & cryptograpy :: prompt_for_passwords ( "Insert password for database encryption: " , 8 ) ) ;
155+ pub fn overwrite_database_json ( json : & str , password : & str ) -> Result < ( ) , std:: io:: Error > {
156+ let encrypted = cryptograpy:: encrypt_string ( json. to_string ( ) , password) ;
155157 let mut file = File :: create ( utils:: get_db_path ( ) ) ?;
156158 utils:: write_to_file ( & encrypted, & mut file)
157159}
0 commit comments