添加导入、导出身份功能,目前导入到默认文件、从默认文件中读取

This commit is contained in:
liubohahaha committed 2021-08-16 16:03:08 +08:00
1 parent 58647e61c3
commit 626e758158
3 files changed
+153 -1

No files matched your search

@@ -42,6 +42,8 @@ import cn.qjm253.quick_android_easy_bar.EasyBar;
import cn.qjm253.quick_android_easy_bar.EasyBarExtensionsKt;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import com.pkusz.min_vpn_client.utils.IdentityDump;
public class LoginActivity extends MINVpnBaseActivity<LoginActivityPresenter>
implements LoginActivityContract.View{
@@ -140,6 +142,26 @@ public class LoginActivity extends MINVpnBaseActivity<LoginActivityPresenter>
etPassword.setText(MINVpnSettingAPI.INSTANCE.getPassword());
}
//导出按钮
export_identity.setBackgroundResource(R.drawable.button_shape_default);
export_identity.setOnClickListener(v->{
ContextExtensionKt.hideSoftKeyboard(this,export_identity);
export_identity.startAnimation();
export_identity.revertAnimation();
new IdentityDump().exportIdentity();
Toast.makeText(this, "You clicked export identity button", Toast.LENGTH_SHORT).show();
});
//导入按钮
import_identity.setBackgroundResource(R.drawable.button_shape_default);
import_identity.setOnClickListener(v->{
ContextExtensionKt.hideSoftKeyboard(this,import_identity);
import_identity.startAnimation();
import_identity.revertAnimation();
new IdentityDump().importIdentity();
Toast.makeText(this, "You clicked import identity button", Toast.LENGTH_SHORT).show();
});
// 登录按钮
btnLogin.setBackgroundResource(R.drawable.button_shape_default);
btnLogin.setOnClickListener(v->{
@@ -189,6 +211,10 @@ public class LoginActivity extends MINVpnBaseActivity<LoginActivityPresenter>
ViewExtensionKt.transYAnimation(etPassword,
0f, -DisplayUtils.dp2px(this, 90)),
ViewExtensionKt.transYAnimation(btnLogin,
0f, -DisplayUtils.dp2px(this, 90)),
ViewExtensionKt.transYAnimation(export_identity,
0f, -DisplayUtils.dp2px(this, 90)),
ViewExtensionKt.transYAnimation(import_identity,
0f, -DisplayUtils.dp2px(this, 90)));
}else{
ViewExtensionKt.animer200(imageView_imgLogo,
@@ -201,6 +227,10 @@ public class LoginActivity extends MINVpnBaseActivity<LoginActivityPresenter>
ViewExtensionKt.transYAnimation(etPassword,
0f, -DisplayUtils.dp2px(this, 90),0f),
ViewExtensionKt.transYAnimation(btnLogin,
0f, -DisplayUtils.dp2px(this, 90),0f),
ViewExtensionKt.transYAnimation(export_identity,
0f, -DisplayUtils.dp2px(this, 90),0f),
ViewExtensionKt.transYAnimation(import_identity,
0f, -DisplayUtils.dp2px(this, 90),0f));
}
return null;
@@ -0,0 +1,84 @@
package com.pkusz.min_vpn_client.utils;
import security.SafeBag;
import util.FileUtil;
import java.io.IOException;
import java.util.Arrays;
public class IdentityDump {
// 身份信息保存位置
public static String identityPath="/storage/emulated/0/Identity/";
// 用户名
public static String username="UserName1";
// 身份文件保存位置
public static String identityFilePath=identityPath+username+".export";
/**
* 生成本地密钥,持久化存储到本地硬盘
* @param username
* @param identityPath
*/
private void generateForeverIdentity(String username,String identityPath){
// identityName是将username前面加上"/"
String identityName="/"+username;
KeyManager.INSTANCE.initKeyChain(identityName,identityPath);
}
/**
* 导出用户身份
*/
public void exportIdentity(){
// 生成数据库身份
new IdentityDump().generateForeverIdentity(username,identityPath);
System.out.println("生成的用户身份: "+KeyManager.INSTANCE
.getIdentity("/"+username,identityPath));
// 导出数据库身份
SafeBag safeBag;
try {
safeBag=KeyManager.INSTANCE.getKeyChain().exportSafeBag("");
System.out.println(Arrays.toString(safeBag.getValue()));
} catch (Exception e) {
e.printStackTrace();
return;
}
// 将身份数据写入本地文件
try {
FileUtil.writeFile(identityFilePath,safeBag.getValue());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 导入用户身份
*/
public void importIdentity(){
// 导入身份前查看是否有该用户身份
// System.out.println("试图取出未导入的用户身份: "+KeyManager.INSTANCE
// .getIdentity("/"+username,identityPath));
// 从本地文件中读取身份数据
byte[] identityBytes;
try {
identityBytes=FileUtil.readFile(identityFilePath);
} catch (IOException e) {
e.printStackTrace();
return;
}
// 转成SafeBag
SafeBag safeBag=new SafeBag(identityBytes);
// 导入到本地身份数据库中
try {
KeyManager.INSTANCE.getKeyChain().importSafeBag(safeBag,"",true);
} catch (Exception e) {
e.printStackTrace();
}
// 导入身份后查看是否有该用户身份
System.out.println("查看是否成功导入用户身份: "+KeyManager.INSTANCE
.getIdentity("/"+username,identityPath));
}
}
+39 -1
View File
@@ -94,12 +94,50 @@
<!-- app:layout_constraintEnd_toEndOf="parent" />-->
<br.com.simplepass.loadingbutton.customViews.CircularProgressButton
android:id="@+id/export_identity"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginStart="40dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="200dp"
android:background="@drawable/button_shape_default"
android:textColor="@color/white"
android:text="导出身份"
app:layout_constraintLeft_toLeftOf="@id/etPassword"
app:layout_constraintRight_toRightOf="@id/etPassword"
app:layout_constraintTop_toBottomOf="@id/etPassword"
app:spinning_bar_color="#FFFFFF"
app:spinning_bar_padding="4dp"
app:spinning_bar_width="3dp"
app:finalCornerAngle="40dp"
app:initialCornerAngle="0dp" />
<br.com.simplepass.loadingbutton.customViews.CircularProgressButton
android:id="@+id/import_identity"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginStart="200dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="40dp"
android:background="@drawable/button_shape_default"
android:text="导入身份"
android:textColor="@color/white"
app:finalCornerAngle="40dp"
app:initialCornerAngle="0dp"
app:layout_constraintLeft_toLeftOf="@id/etPassword"
app:layout_constraintRight_toRightOf="@id/etPassword"
app:layout_constraintTop_toBottomOf="@id/etPassword"
app:spinning_bar_color="#FFFFFF"
app:spinning_bar_padding="4dp"
app:spinning_bar_width="3dp" />
<br.com.simplepass.loadingbutton.customViews.CircularProgressButton
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="40dp"
android:layout_marginTop="75dp"
android:layout_marginEnd="40dp"
android:background="@drawable/button_shape_default"
android:text="@string/login"