Skip to content

Commit 7477219

Browse files
committed
Picking bcrypt cost from props file to be able to handle sifferent edge cases
1 parent 54b2b45 commit 7477219

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/lib.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ struct CoreLogicHandler {
159159
impl CoreLogicHandler {
160160
async fn new(editor: Box<dyn AsyncEditor>, props: Props) -> CoreLogicHandler {
161161
let mut props = props;
162-
let code_version = rkl_version();
163162
let editor = editor;
164163
// Holds the UserSelections
165164
let user_selection;
@@ -185,28 +184,22 @@ impl CoreLogicHandler {
185184
editor.show_password_enter().await
186185
};
187186

188-
// The bcrypt cost changed in 0.17.0 from 7 to 12
189-
let bcrypt_cost = if code_version == props.version() || file_handler::is_first_run(FILENAME) {
190-
BCRYPT_COST
191-
} else {
192-
BCRYPT_COST_PRE_0_17_0
193-
};
194187
// Take the provided password and do the initialization
195188
let (us, cr) = handle_provided_password_for_init(
196189
provided_password,
197-
bcrypt_cost,
198190
FILENAME,
199191
&mut safe,
200192
&mut configuration,
201193
&editor,
202194
).await;
203-
// If the password was not correct
195+
// If the password was correct
204196
if us != UserSelection::GoTo(Menu::TryPass(false)) {
197+
// Save the version
198+
props.set_version(rkl_version());
199+
let _ = file_handler::save_props(&props, PROPS_FILENAME);
205200
// Set the UserSelection
206201
user_selection = us;
207202
cryptor = cr;
208-
props.set_version(code_version);
209-
let _ = file_handler::save_props(&props, PROPS_FILENAME);
210203
break;
211204
}
212205
};
@@ -260,7 +253,6 @@ impl CoreLogicHandler {
260253
UserSelection::GoTo(Menu::TryPass(update_last_sync_version)) => {
261254
let (user_selection, cr) = handle_provided_password_for_init(
262255
s.editor.show_password_enter().await,
263-
BCRYPT_COST,
264256
FILENAME,
265257
&mut s.safe,
266258
&mut s.configuration,
@@ -293,7 +285,7 @@ impl CoreLogicHandler {
293285
UserSelection::ProvidedPassword(pwd, salt_pos) => {
294286
debug!("UserSelection::GoTo(Menu::ProvidedPassword)");
295287
s.cryptor =
296-
file_handler::create_bcryptor(FILENAME, pwd.to_string(), BCRYPT_COST, *salt_pos, true, true)
288+
file_handler::create_bcryptor(FILENAME, pwd.to_string(), bcrypt_cost_from_file(), *salt_pos, true, true)
297289
.unwrap();
298290
Box::pin(future::ready(UserSelection::GoTo(Menu::Main)))
299291
}
@@ -666,7 +658,7 @@ Warning: Saving will discard all the entries that could not be recovered.
666658
let cr = file_handler::create_bcryptor(
667659
&path,
668660
pwd.to_string(),
669-
BCRYPT_COST,
661+
bcrypt_cost_from_file(),
670662
*salt_pos,
671663
false,
672664
import_from_default_location,
@@ -1103,9 +1095,19 @@ where
11031095
}
11041096
}
11051097

1098+
fn bcrypt_cost_from_file() -> u32 {
1099+
let props = file_handler::load_properties(PROPS_FILENAME).unwrap_or_default();
1100+
1101+
// The bcrypt cost changed in 0.17.0 from 7 to 12
1102+
if rkl_version() == props.version() || file_handler::is_first_run(FILENAME) {
1103+
BCRYPT_COST
1104+
} else {
1105+
BCRYPT_COST_PRE_0_17_0
1106+
}
1107+
}
1108+
11061109
async fn handle_provided_password_for_init(
11071110
provided_password: UserSelection,
1108-
bcrypt_cost: u32,
11091111
filename: &str,
11101112
safe: &mut Safe,
11111113
configuration: &mut RklConfiguration,
@@ -1114,8 +1116,10 @@ async fn handle_provided_password_for_init(
11141116
let user_selection: UserSelection;
11151117
match provided_password {
11161118
UserSelection::ProvidedPassword(pwd, salt_pos) => {
1117-
// New Cryptor here
1118-
let cr =
1119+
let bcrypt_cost = bcrypt_cost_from_file();
1120+
info!("Using bcrypt with cost {bcrypt_cost}");
1121+
// Create cryptor for decryption
1122+
let cr: BcryptAes =
11191123
file_handler::create_bcryptor(filename, pwd.to_string(), bcrypt_cost, *salt_pos, false, true)
11201124
.unwrap();
11211125
// Try to decrypt and load the Entries
@@ -1169,6 +1173,13 @@ async fn handle_provided_password_for_init(
11691173
&user_selection,
11701174
safe.entries.len()
11711175
);
1176+
// Return cryptor with the correct current bcrypt cost
1177+
let cr = if bcrypt_cost != BCRYPT_COST {
1178+
file_handler::create_bcryptor(filename, pwd.to_string(), BCRYPT_COST, *salt_pos, false, true)
1179+
.unwrap()
1180+
} else {
1181+
cr
1182+
};
11721183
(user_selection, cr)
11731184
}
11741185
UserSelection::GoTo(Menu::Exit) => {

0 commit comments

Comments
 (0)