package com.haohaohu.cachemanage.strategy;

import android.content.Context;
import android.os.Build;

import com.haohaohu.cachemanage.util.KeyStoreHelper;

import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * KeyStore加密策略
 *
 * @author haohao(ronghao3508@gmail.com) on 2018/5/28 16:18
 * @version v1.0
 */
public class KeyStoreEncryptStrategy implements IEncryptStrategy {

    private Context mContext;
    private String alias;

    public KeyStoreEncryptStrategy(Context context) {
        this(context, context.getPackageName());
    }

    public KeyStoreEncryptStrategy(Context context, String alias) {
        this.mContext = context;
        this.alias = context.getPackageName() + "_" + alias;
        createKeyStoreSecretKey(this.alias);
    }

    @Override
    public String encrypt(String str) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                return KeyStoreHelper.encryptM(alias, str);
            }
            else {
                return KeyStoreHelper.encryptJBMR2(alias, str);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    @Override
    public String decode(String str) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                return KeyStoreHelper.decryptM(alias, str);
            }
            else{
            return KeyStoreHelper.decryptJBMR2(alias, str);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public void createKeyStoreSecretKey(String alias) {
        try {
            KeyStoreHelper.createKeys(mContext, alias);
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
    }
}

@@ -72,33 +72,8 @@
 ext;
-%0Aimport android.os.Build;
 %0A%0Aim
@@ -128,159 +128,8 @@
 r;%0A%0A
-import java.security.InvalidAlgorithmParameterException;%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.security.NoSuchProviderException;%0A%0A
 /**%0A
@@ -761,636 +761,297 @@
     
-if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.M) %7B%0A                return KeyStoreHelper.encryptM(alias, str);%0A            %7D%0A            else %7B%0A                return KeyStoreHelper.encryptJBMR2(alias, str);%0A            %7D%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A            return %22%22;%0A        %7D%0A    %7D%0A%0A    @Override%0A    public String decode(String str) %7B%0A        try %7B%0A            if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.M) %7B%0A                return KeyStoreHelper.decryptM(alias, str);%0A            %7D%0A            else%7B%0A            return KeyStoreHelper.decryptJBMR2(alias, str);%0A            %7D
+return KeyStoreHelper.encrypt(mContext, alias, str);%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A            return %22%22;%0A        %7D%0A    %7D%0A%0A    @Override%0A    public String decode(String str) %7B%0A        try %7B%0A            return KeyStoreHelper.decrypt(mContext, alias, str);
 %0A   
@@ -1157,21 +1157,22 @@
 %7D%0A%0A    p
-ublic
+rivate
  void cr
@@ -1300,192 +1300,8 @@
 ch (
-NoSuchProviderException e) %7B%0A            e.printStackTrace();%0A        %7D catch (NoSuchAlgorithmException e) %7B%0A            e.printStackTrace();%0A        %7D catch (InvalidAlgorithmParameter
 Exce

Update KeyStoreEncryptStrategy.java