@@ -7,10 +7,10 @@ use crate::cryptograpy;
7
7
use crate :: otp:: otp_element:: OTPElement ;
8
8
9
9
10
- pub fn read_from_file ( ) -> Result < Vec < OTPElement > , String > {
10
+ pub fn read_from_file ( password : & str ) -> Result < Vec < OTPElement > , String > {
11
11
let encrypted_contents = read_to_string ( & get_db_path ( ) ) . unwrap ( ) ;
12
12
//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 ) ;
14
14
match contents {
15
15
Ok ( contents) => {
16
16
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
36
36
if !check_secret ( & upper_secret) {
37
37
return Err ( String :: from ( "Bad secret" ) )
38
38
}
39
+ let pw = & cryptograpy:: prompt_for_passwords ( "Password: " , 8 , false ) ;
39
40
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 ! [ ] ) ;
40
41
let mut elements;
41
- match read_from_file ( ) {
42
+ match read_from_file ( pw ) {
42
43
Ok ( result) => elements = result,
43
44
Err ( e) => return Err ( e)
44
45
}
45
46
elements. push ( otp_element) ;
46
- match overwrite_database ( elements) {
47
+ match overwrite_database ( elements, pw ) {
47
48
Ok ( ( ) ) => Ok ( ( ) ) ,
48
49
Err ( e) => Err ( format ! ( "{}" , e) )
49
50
}
@@ -57,8 +58,8 @@ pub fn remove_element_from_db(mut id: usize) -> Result<(),String>{
57
58
id -= 1 ;
58
59
59
60
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 ) {
62
63
Ok ( result) => elements = result,
63
64
Err ( e) => {
64
65
return Err ( e) ;
@@ -73,7 +74,7 @@ pub fn remove_element_from_db(mut id: usize) -> Result<(),String>{
73
74
break ;
74
75
}
75
76
}
76
- match overwrite_database ( elements) {
77
+ match overwrite_database ( elements, pw ) {
77
78
Ok ( ( ) ) => Ok ( ( ) ) ,
78
79
Err ( e) => Err ( format ! ( "{}" , e) ) ,
79
80
}
@@ -89,7 +90,8 @@ pub fn edit_element(mut id: usize, secret: &str,issuer: &str,label: &str,algorit
89
90
id -= 1 ;
90
91
91
92
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) {
93
95
Ok ( result) => elements = result,
94
96
Err ( _e) => return Err ( String :: from ( "Cannot decrypt existing database" ) )
95
97
}
@@ -116,7 +118,7 @@ pub fn edit_element(mut id: usize, secret: &str,issuer: &str,label: &str,algorit
116
118
break ;
117
119
}
118
120
}
119
- match overwrite_database ( elements) {
121
+ match overwrite_database ( elements, pw ) {
120
122
Ok ( ( ) ) => Ok ( ( ) ) ,
121
123
Err ( e) => Err ( format ! ( "{}" , e) ) ,
122
124
}
@@ -130,7 +132,7 @@ pub fn export_database() -> Result<String, String> {
130
132
exported_path. push_str ( "/exported.cotp" ) ;
131
133
let mut file = File :: create ( & exported_path) . expect ( "Cannot create file" ) ;
132
134
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 ) ) ;
134
136
match contents {
135
137
Ok ( contents) => {
136
138
if contents == "[]" {
@@ -145,13 +147,13 @@ pub fn export_database() -> Result<String, String> {
145
147
}
146
148
}
147
149
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 > {
149
151
let json_string: & str = & serde_json:: to_string ( & elements) ?;
150
- overwrite_database_json ( json_string)
152
+ overwrite_database_json ( json_string, password )
151
153
}
152
154
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) ;
155
157
let mut file = File :: create ( utils:: get_db_path ( ) ) ?;
156
158
utils:: write_to_file ( & encrypted, & mut file)
157
159
}
0 commit comments