/app/src/main/java/sun/bob/leela/runnable/ChangePasswordRunnable.java

https://github.com/PuffOpenSource/Puff-Android · Java · 101 lines · 82 code · 15 blank · 4 comment · 10 complexity · aa4a51973dac921b0790c1070794c0de MD5 · raw file

  1. package sun.bob.leela.runnable;
  2. import android.content.Context;
  3. import android.util.Base64;
  4. import android.util.Log;
  5. import java.util.ArrayList;
  6. import java.util.Stack;
  7. import javax.crypto.Cipher;
  8. import javax.crypto.spec.SecretKeySpec;
  9. import de.greenrobot.event.EventBus;
  10. import sun.bob.leela.db.Account;
  11. import sun.bob.leela.db.AccountHelper;
  12. import sun.bob.leela.events.CryptoEvent;
  13. import sun.bob.leela.utils.AppConstants;
  14. /**
  15. * Created by bob.sun on 16/8/12.
  16. */
  17. public class ChangePasswordRunnable implements Runnable {
  18. private Context context;
  19. private ArrayList<Account> accounts;
  20. // private Stack<Account> accounts;
  21. private String oldPassword, newPassword;
  22. public ChangePasswordRunnable(Context context, String oldPassword, String newPassword) {
  23. this.context = context;
  24. this.oldPassword = oldPassword;
  25. this.newPassword = newPassword;
  26. EventBus.getDefault().register(this);
  27. accounts = AccountHelper.getInstance(null).getAllAccount();
  28. }
  29. @Override
  30. public void run() {
  31. CryptoEvent result = null;
  32. new PBKDFRunnable(newPassword.toString()).run();
  33. try {
  34. for (Account account : accounts) {
  35. if (account.getType() == AppConstants.TYPE_QUICK || account.getType() == AppConstants.TYPE_MASTER)
  36. continue;
  37. String acct = new String(decrypt(Base64.decode(account.getAccount(), Base64.DEFAULT)));//.replace(account.getAccount_salt(), "");
  38. String passwd = new String(decrypt(Base64.decode(account.getHash(), Base64.DEFAULT)));//.replace(account.getSalt(), "");
  39. String addt = new String(decrypt(Base64.decode(account.getAdditional(), Base64.DEFAULT)));//.replace(account.getAdditional_salt(), "");
  40. account.setAccount(Base64.encodeToString(encrypt(acct), Base64.DEFAULT));
  41. account.setHash(Base64.encodeToString(encrypt(passwd), Base64.DEFAULT));
  42. account.setAdditional(Base64.encodeToString(encrypt(addt), Base64.DEFAULT));
  43. AccountHelper.getInstance(null).saveAccount(account);
  44. }
  45. result = new CryptoEvent("", AppConstants.TYPE_MASTER_CHANGE, "master");
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. result = new CryptoEvent("", AppConstants.TYPE_SHTHPPN, "master");
  49. } finally {
  50. EventBus.getDefault().post(result);
  51. }
  52. }
  53. private byte[] encrypt(String text) throws Exception{
  54. SecretKeySpec skeySpec = new SecretKeySpec(newPassword.getBytes("UTF-8"), "Blowfish");
  55. Cipher cipher = Cipher.getInstance("Blowfish");
  56. cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  57. byte[] encrypted = cipher.doFinal(text.getBytes());
  58. return encrypted;
  59. }
  60. public byte[] decrypt(byte[] rawText) throws Exception {
  61. SecretKeySpec skeySpec = new SecretKeySpec(oldPassword.getBytes("UTF-8"), "Blowfish");
  62. Cipher cipher = Cipher.getInstance("Blowfish");
  63. cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  64. byte[] decrypted = cipher.doFinal(rawText);
  65. return decrypted;
  66. }
  67. public void onEventMainThread(CryptoEvent event) {
  68. if (!event.getField().equalsIgnoreCase("master")) {
  69. return;
  70. }
  71. if (event.getType() == AppConstants.TYPE_ENCRYPT) {
  72. Account account = AccountHelper.getInstance(null).getMasterAccount();
  73. if (account == null) {
  74. account = new Account();
  75. }
  76. account.setHash(event.getResult());
  77. account.setSalt("");
  78. account.setName("");
  79. account.setType(AppConstants.TYPE_MASTER);
  80. account.setCategory(AppConstants.CAT_ID_PRIVATE);
  81. account.setTag("");
  82. AccountHelper.getInstance(null).saveAccount(account);
  83. EventBus.getDefault().unregister(this);
  84. }
  85. }
  86. }