Commit 9e3d311b authored by 赵鹏翔's avatar 赵鹏翔

授权码加密到文件,优化存储

parent 8aea2dfe
......@@ -4,18 +4,21 @@ import android.Manifest
import android.os.Environment
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.EncodeUtils
import com.blankj.utilcode.util.FileIOUtils
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.ToastUtils
import com.fastcashier.lib_common.function.account.AccountService.getAccountPassword
import com.fastcashier.lib_common.function.account.AccountService.getUserName
import com.fastcashier.lib_common.function.account.AccountService.setAccountPassword
import com.fastcashier.lib_common.util.isBase64
import com.permissionx.guolindev.PermissionX
import java.nio.charset.Charset
import java.util.concurrent.Executors
/**
* 类描述:账户管理员密码管理
* 概述:
* 概述:密码以base64处理存到本地,初始化时解码到内存
* 创建人:zpxiang
* 创建时间:2022/4/1
* 修改人:
......@@ -34,6 +37,7 @@ class AccountPasswordManageKit private constructor() {
}
return sInstance
}
@Synchronized
fun get(): AccountPasswordManageKit {
return instance!!
......@@ -67,9 +71,14 @@ class AccountPasswordManageKit private constructor() {
fun initAccountPassword(context: AppCompatActivity, callback: AccountPasswordManageCallback?) {
if (FileUtils.isFileExists(currentAccountFilePath)) {
Executors.newCachedThreadPool().execute {
val password = FileIOUtils.readFile2String(
val password64 = FileIOUtils.readFile2String(
currentAccountFilePath
)
val password = if (isBase64(password64))
EncodeUtils.base64Decode(password64).toString(Charset.defaultCharset())
else password64
setAccountPassword(password)
context.runOnUiThread { callback?.onPasswordGet(password) }
}
......@@ -77,11 +86,12 @@ class AccountPasswordManageKit private constructor() {
}
setAccountPassword(ACCOUNT_DEFAULT_PASSWORD)
callback?.onPasswordGet(ACCOUNT_DEFAULT_PASSWORD)
val password64 = EncodeUtils.base64Encode2String(ACCOUNT_DEFAULT_PASSWORD.toByteArray())
if (FileUtils.createOrExistsFile(currentAccountFilePath)) {
Executors.newCachedThreadPool().execute {
FileIOUtils.writeFileFromString(
currentAccountFilePath,
ACCOUNT_DEFAULT_PASSWORD
password64
)
}
}
......@@ -117,8 +127,9 @@ class AccountPasswordManageKit private constructor() {
FileUtils.deleteAllInDir(currentAccountFilePath)
}
if (FileUtils.createOrExistsFile(currentAccountFilePath)) {
val password64 = EncodeUtils.base64Encode2String(password.toByteArray())
Executors.newCachedThreadPool()
.execute { FileIOUtils.writeFileFromString(currentAccountFilePath, password) }
.execute { FileIOUtils.writeFileFromString(currentAccountFilePath, password64) }
}
}
}
\ No newline at end of file
......@@ -11,6 +11,7 @@ import android.text.TextUtils
import java.net.Inet4Address
import java.net.NetworkInterface
import java.net.SocketException
import java.util.regex.Pattern
fun isEmpty(s: String?): Boolean {
return TextUtils.isEmpty(s)
......@@ -144,3 +145,13 @@ fun isNetworkConnected(context: Context?): Boolean {
}
return false
}
/**
* 判断是否为Base64编码后的数据
*/
fun isBase64(str: String): Boolean {
val base64Pattern =
"^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$"
return Pattern.matches(base64Pattern, str)
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment