}
}
@Override
public UserInformation login(String userId, String masterPassword) {
Account account = getAccount(userId);
if (account == null) {
return null;
}
byte[] persistentPassword = buildPersistentPassword(userId, masterPassword);
if (!Arrays.equals(persistentPassword, account.getMasterPassword())) {
return null;
}
SecretKey wrappingKey = buildWrappingKey(userId, masterPassword);
byte[] iv = cryptoEngine.buildInitializationVector(wrappingKey.getEncoded());
byte[] encryptionKeyAsBytes = cryptoEngine.decrypt(account.getEncryptedSecretKey(),
wrappingKey,
iv);
SecretKey secretKey = cryptoEngine.bytesToSecretKey(encryptionKeyAsBytes);
return new UserInformation(account.getUserId(),
secretKey,
new Preferences(account.getPreferredLocale(),
account.getPreferredTimeZone(),
account.isPasswordsUnmasked(),
account.getPasswordGenerationPreferences()));
}