[TOTP]android kotlin实现 totp身份验证器 类似Google身份验证器

        背景:自己或者公司用一些谷歌身份验证器或者microsoft身份验证器,下载来源不明,或者有广告,使用不安全。于是自己写一个,安全放心使用。

代码已开源:shixiaotian/sxt-android-totp: android totp authenticator (github.com)

效果图

  

此身份验证器,一共1个activity,3个fragment。

实现原理,通过线程动态触发totp算法,从加密的sqlite里读取密钥信息进行加密计算。新增密钥可通过扫描二维码,或者手动添加的方式实现。

一.添加对应的包

包括,totp算法包,zxing二维码扫描包,sqlite加密包

分别为

authenticator="1.0.0"
zxing-android-embedded="4.2.0"
database-sqlcipher="4.5.0"androidx-authenticator = { group = "org.jboss.aerogear", name = "aerogear-otp-java", version.ref = "authenticator" }
androidx-zxing-android-embedded = { group = "com.journeyapps", name = "zxing-android-embedded", version.ref = "zxing-android-embedded" }
androidx-database-sqlcipher ={ group = "net.zetetic", name = "android-database-sqlcipher", version.ref = "database-sqlcipher" }

  文件

libs.versions.toml

[versions]
agp = "8.7.2"
kotlin = "1.9.24"
coreKtx = "1.10.1"
junit = "4.13.2"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
appcompat = "1.6.1"
material = "1.10.0"
activity = "1.8.0"
constraintlayout = "2.1.4"
authenticator="1.0.0"
zxing-android-embedded="4.2.0"
database-sqlcipher="4.5.0"[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
androidx-authenticator = { group = "org.jboss.aerogear", name = "aerogear-otp-java", version.ref = "authenticator" }
androidx-zxing-android-embedded = { group = "com.journeyapps", name = "zxing-android-embedded", version.ref = "zxing-android-embedded" }
androidx-database-sqlcipher ={ group = "net.zetetic", name = "android-database-sqlcipher", version.ref = "database-sqlcipher" }[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }

 build.gradele.kts

plugins {alias(libs.plugins.android.application)alias(libs.plugins.kotlin.android)
}android {namespace = "com.shixiaotian.totp.scan.application"compileSdk = 34defaultConfig {applicationId = "com.shixiaotian.totp.scan.application"minSdk = 24targetSdk = 34versionCode = 1versionName = "1.0"testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"}buildTypes {release {isMinifyEnabled = falseproguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"),"proguard-rules.pro")}}compileOptions {sourceCompatibility = JavaVersion.VERSION_11targetCompatibility = JavaVersion.VERSION_11}kotlinOptions {jvmTarget = "11"}
}dependencies {implementation(libs.androidx.core.ktx)implementation(libs.androidx.appcompat)implementation(libs.material)implementation(libs.androidx.activity)implementation(libs.androidx.constraintlayout)implementation(libs.androidx.authenticator)implementation(libs.androidx.espresso.core)implementation(libs.androidx.zxing.android.embedded)implementation(libs.androidx.database.sqlcipher)testImplementation(libs.junit)androidTestImplementation(libs.androidx.junit)androidTestImplementation(libs.androidx.espresso.core)}

二.项目程序文件结构为

三.全部程序文件

MyConstant

作用:常量类,添加了如下三个参数,方便自行变换,增加安全性和防止撞库

package com.shixiaotian.totp.scan.application.commonclass MyConstants {companion object {//数据库名称,记得加扰动防止重复const val dbName = "sxt.auth.code.0098675.db"// 数据库密码const val dbPassword = "jsdfjhkldsvcbuehuisudbekokmhshyebgqoondyasd"// app 首次运行标记const val firstRunTag = "sxt.auth.code.0098674.firstRunTag"}
}

DatabaseHelper

作用:数据库操作相关类,集成了数据库加密,初始化,基本插入、读取、删除等功能

package com.shixiaotian.totp.scan.application.dbimport android.content.Context
import android.database.Cursor
import androidx.core.content.contentValuesOf
import com.shixiaotian.totp.scan.application.common.MyConstants
import com.shixiaotian.totp.scan.application.vo.User
import net.sqlcipher.database.SQLiteDatabase
import net.sqlcipher.database.SQLiteOpenHelperclass DatabaseHelper(context: Context) : SQLiteOpenHelper(context, MyConstants.dbName, null, 1) {private val dbContext:Context = contextoverride fun onCreate(db: SQLiteDatabase) {}override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {// 更新数据库的时候调用}private fun openEncryptedDatabase(): SQLiteDatabase {val dbHelper = DatabaseHelper(dbContext)val db = dbHelper.getWritableDatabase(MyConstants.dbPassword)return db}// 写入数据fun insertUser(username: String, secretKey: String, issuer: String): Long {val db = this.openEncryptedDatabase()var id =db.insert("sxt_totp_users", null, contentValuesOf("username" to username, "secretKey" to secretKey, "issuer" to issuer))db.close()return id}// 删除数据fun deleteUser(id: Int) {val db = this.openEncryptedDatabase()db.delete("sxt_totp_users", "id = ?", arrayOf(id.toString()))db.close()}// 查询数据fun getUser(id: Int): User? {val db = this.openEncryptedDatabase()val cursor: Cursor = db.query("sxt_totp_users", null, "id = ?", arrayOf(id.toString()), null, null, null)var user: User? = nullif (cursor.moveToFirst()) {val id = cursor.getInt(0)val name = cursor.getString(1)val secretKey = cursor.getString(2)val issuer = cursor.getString(3)// 假设User有id和name两个字段user = User(id, name,secretKey, issuer)}cursor.close()db.close()return user}fun getAllUser(): List<User> {val db = this.openEncryptedDatabase()val items = ArrayList<User>()val cursor: Cursor  = db.query("sxt_totp_users", arrayOf("id","username","secretKey", "issuer"), null, null, null, null, null)cursor.moveToFirst()while (!cursor.isAfterLast) {val id = cursor.getInt(0)val name = cursor.getString(1)val secretKey = cursor.getString(2)val issuer = cursor.getString(3)items.add(User(id, name,secretKey, issuer))cursor.moveToNext()}cursor.close()db.close()return items}fun initDB() {// 创建表val db = this.openEncryptedDatabase()db.execSQL("CREATE TABLE sxt_totp_users (id INTEGER PRIMARY KEY, username TEXT, secretKey TEXT, issuer TEXT)")db.close()}fun init() {this.insertUser("apple","apple", "github")this.insertUser("pear","pear", "steam")this.insertUser("apricot","apricot","wiki")this.insertUser("peach","peach","TK")}
}

CodeAddFragment

作用:令牌添加页面片段,提供手动输入令牌,和触发zxing令牌扫描功能,并回收zxing扫描后的结果,进行存储,将相关id数据传输给codeShow单独展示的页面进行展示。

package com.shixiaotian.totp.scan.application.fragmentsimport android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import com.shixiaotian.totp.scan.application.db.DatabaseHelper
import com.shixiaotian.totp.scan.application.tools.EncodeTools
import com.google.zxing.integration.android.IntentIntegrator
import com.journeyapps.barcodescanner.CaptureActivity
import com.shixiaotian.totp.scan.application.R// TODO: Rename parameter arguments, choose names that match
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"/*** A simple [Fragment] subclass.* Use the [CodeAddFragment.newInstance] factory method to* create an instance of this fragment.*/
class CodeAddFragment : Fragment() {private lateinit var saveButton: Viewprivate lateinit var scanButton: Viewprivate lateinit var nameText: TextViewprivate lateinit var secretKeyText: TextViewprivate lateinit var issuerText: TextView// TODO: Rename and change types of parametersprivate var param1: String? = nullprivate var param2: String? = nulloverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)arguments?.let {param1 = it.getString(ARG_PARAM1)param2 = it.getString(ARG_PARAM2)}}@SuppressLint("MissingInflatedId")override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {// Inflate the layout for this fragmentval view = inflater.inflate(R.layout.fragment_code_add, container, false)val dbHelper = DatabaseHelper(requireContext())saveButton = view.findViewById(R.id.saveButton)saveButton.setOnClickListener {nameText = view.findViewById<TextView>(R.id.addUsernameText)val name = nameText.getText();secretKeyText = view.findViewById<TextView>(R.id.addSecretKeyText)val secretKey = secretKeyText.getText();issuerText = view.findViewById<TextView>(R.id.addIssuerText)val issuer = issuerText.getText();if(name.isEmpty()) {alertAddError("Name can't be blank")} else if(secretKey.isEmpty()) {alertAddError("SecretKey can't be blank")} else if(issuer.isEmpty()){alertAddError("issuer can't be blank")} else {var saveId = dbHelper.insertUser(name.toString(), secretKey.toString(), issuer.toString());nameText.setText("")secretKeyText.setText("")issuerText.setText("")val fragment = CodeShowFragment.newInstance(saveId.toString(), "")parentFragmentManager.beginTransaction().replace(R.id.viewPager, fragment).commit()}}scanButton = view.findViewById(R.id.cameraButton)scanButton.setOnClickListener {val integrator = IntentIntegrator.forSupportFragment(this)integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE)integrator.setOrientationLocked(false)integrator.captureActivity = CaptureActivity::class.javaintegrator.setRequestCode(5766)  //_scan为自己定义的请求码integrator.initiateScan()}return view}fun alertAddError(msg : String) {println("alertAddError : " + msg)val builder = AlertDialog.Builder(context)builder.setTitle("add error")builder.setMessage(msg)builder.setPositiveButton("OK") { dialog, _ ->dialog.dismiss()}builder.create().show()}override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {super.onActivityResult(requestCode, resultCode, data)when (requestCode) {//_scan为自己定义的扫码请求码5766 -> {// 跳转扫描页面返回扫描数据var scanResult = IntentIntegrator.parseActivityResult(IntentIntegrator.REQUEST_CODE,resultCode,data);//  判断返回值是否为空if (scanResult != null) {//返回条形码数据var result = scanResult.contentsif(result == null) {parentFragmentManager.beginTransaction().replace(R.id.viewPager, this).commit()return}val user = EncodeTools.decode(result);if(user == null) {Toast.makeText(context, "Scan Fail", Toast.LENGTH_SHORT).show()parentFragmentManager.beginTransaction().replace(R.id.viewPager, this).commit()return}// 保存数据val dbHelper = DatabaseHelper(requireContext())var saveId = dbHelper.insertUser(user!!.getUsername(), user.getSecretKey(), user.getIssuer())if(saveId < 0 ) {Toast.makeText(context, "Save Data Fail", Toast.LENGTH_SHORT).show()parentFragmentManager.beginTransaction().replace(R.id.viewPager, this).commit()return}// 跳转val fragment = CodeShowFragment.newInstance(saveId.toString(), "")parentFragmentManager.beginTransaction().replace(R.id.viewPager, fragment).commit()} else {Toast.makeText(context, "Scan Fail:ERROR", Toast.LENGTH_SHORT).show()parentFragmentManager.beginTransaction().replace(R.id.viewPager, this).commit()}} else -> {val fragment = CodeAddFragment()parentFragmentManager.beginTransaction().replace(R.id.viewPager, fragment).commit()}}}companion object {/*** Use this factory method to create a new instance of* this fragment using the provided parameters.** @param param1 Parameter 1.* @param param2 Parameter 2.* @return A new instance of fragment CodeAddFragment.*/// TODO: Rename and change types and number of parameters@JvmStaticfun newInstance(param1: String, param2: String) =CodeAddFragment().apply {arguments = Bundle().apply {putString(ARG_PARAM1, param1)putString(ARG_PARAM2, param2)}}}
}

CodeListFragment

作用:令牌列表界面,提供了定时器刷新整个列表的功能,和每个令牌单独点击触发的功能

package com.shixiaotian.totp.scan.application.fragmentsimport android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
import android.widget.TextView
import com.shixiaotian.totp.scan.application.CodeListAdapter
import com.shixiaotian.totp.scan.application.R
import com.shixiaotian.totp.scan.application.db.DatabaseHelper
import com.shixiaotian.totp.scan.application.tools.MyTimeUtils
import com.shixiaotian.totp.scan.application.vo.User// TODO: Rename parameter arguments, choose names that match
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"/*** A simple [Fragment] subclass.* Use the [CodeListFragment.newInstance] factory method to* create an instance of this fragment.*/
class CodeListFragment : Fragment() {private var start: Long = 30private lateinit var adapter: CodeListAdapterprivate val handler = Handler()private var runnable: Runnable? = null// TODO: Rename and change types of parametersprivate var param1: String? = nullprivate var param2: String? = nullprivate var userList: List<User>? =nullprivate val data = listOf("apple","pear","apricot","peach","grape","banana","pineapple","plum","watermelon","orange","lemon","mango","strawberry","medlar","mulberry","nectarine","cherry","pomegranate","fig","persimmon")override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)arguments?.let {param1 = it.getString(ARG_PARAM1)param2 = it.getString(ARG_PARAM2)}}override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {// 获取视图val view = inflater.inflate(R.layout.fragment_code_list, container, false)val listView = view.findViewById<ListView>(R.id.listView)var textView3 = view.findViewById<TextView>(R.id.textView3)// 查询数据库val dbHelper = DatabaseHelper(requireContext())userList = dbHelper.getAllUser();//获取当前分钟秒数// 启动定时器timer(textView3)// 创建ArrayAdapter,将数据源传递给它adapter = CodeListAdapter(requireContext(), R.layout.code_item, userList!!)// 将适配器与ListView关联listView.adapter = adapterlistView.setOnItemClickListener { parent, view, position, id ->val textView = view.findViewById<TextView>(R.id.user_id);val fragment = CodeShowFragment.newInstance(textView.text.toString(), "")// 执行跳转parentFragmentManager.beginTransaction().replace(R.id.viewPager, fragment).commit()val codeShowFragment = CodeShowFragment()switchFragment(codeShowFragment)}return view}// 刷新数据private fun refresh() {if(userList != null) {adapter.notifyDataSetChanged()}}// 定时器private fun timer(textView : TextView) {// 动态计算当前秒数start = MyTimeUtils.getCurrentSec()runnable = Runnable {val formattedNumber = String.format("%02d",start/1000)textView.setText(formattedNumber + "s")start = start -100if(start <0) {refresh()start= MyTimeUtils.getCurrentSec()}// 在这里设置下一次循环的延时时间,例如1秒handler.postDelayed(runnable!!, 100)}// 初始化计时器handler.postDelayed(runnable!!, 50) // 延时1秒后开始循环}@SuppressLint("SuspiciousIndentation")private fun switchFragment(fragment: Fragment) {val transaction = parentFragmentManager.beginTransaction()transaction.show(fragment)transaction.commit()}companion object {/*** Use this factory method to create a new instance of* this fragment using the provided parameters.** @param param1 Parameter 1.* @param param2 Parameter 2.* @return A new instance of fragment CodeListFragment.*/// TODO: Rename and change types and number of parameters@JvmStaticfun newInstance(param1: String, param2: String) =CodeListFragment().apply {arguments = Bundle().apply {putString(ARG_PARAM1, param1)putString(ARG_PARAM2, param2)}}}override fun onDestroyView() {println("onDestroyView")handler.removeCallbacks(runnable!!)super.onDestroyView()}
}

CodeShowFragment

作用:令牌单独展示页面的片段代码,用于页面操作功能处理。提供了定时器进行倒计时刷新令牌,和令牌删除功能。

package com.shixiaotian.totp.scan.application.fragmentsimport android.app.AlertDialog
import android.os.Bundle
import android.os.Handler
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.TextView
import com.shixiaotian.totp.scan.application.R
import com.shixiaotian.totp.scan.application.db.DatabaseHelper
import com.shixiaotian.totp.scan.application.tools.EncodeTools
import com.shixiaotian.totp.scan.application.tools.MyTimeUtils// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"/*** A simple [Fragment] subclass.* Use the [CodeShowFragment.newInstance] factory method to* create an instance of this fragment.*/
class CodeShowFragment : Fragment() {private var codeView: TextView? =null// TODO: Rename and change types of parametersprivate var param1: String? = nullprivate var param2: String? = nullprivate var start: Long = 30000private val handler = Handler()private var runnable: Runnable? = nullprivate var secretKey: String =""private lateinit var progressBar: ProgressBaroverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)arguments?.let {param1 = it.getString(ARG_PARAM1)param2 = it.getString(ARG_PARAM2)}}override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {// Inflate the layout for this fragmentvar id: String? = param1if(id == null) {id = "0"}// 查询数据库val dbHelper = DatabaseHelper(requireContext())val user = dbHelper.getUser(id.toInt())val view = inflater.inflate(R.layout.fragment_code_show, container, false)// 初始化进度条progressBar = view.findViewById<ProgressBar>(R.id.progressBar)// 设置进度条的最大值progressBar.max = 30000// 设置当前进度progressBar.progress = 30000// 显示进度条progressBar.visibility = ProgressBar.VISIBLEval showIssuerTextView = view.findViewById<TextView>(R.id.showIssuerTextView)val usernameView = view.findViewById<TextView>(R.id.showUsernameTextView)codeView = view.findViewById<TextView>(R.id.showCodeView)val timeView3 = view.findViewById<TextView>(R.id.showTimeView)if(user != null) {secretKey = user!!.getSecretKey();// 开启个线程,动态计算密钥,并更新到ui界面showIssuerTextView.setText(user.getIssuer())usernameView.setText(user.getUsername())if (codeView != null) {codeView!!.setText(EncodeTools.encode(user.getSecretKey()))}timer(timeView3)}// 删除按钮val deleteButton = view.findViewById<TextView>(R.id.deleteButton)deleteButton.setOnClickListener {showDeleteConfirmationDialog(id)}return view}private fun refresh() {codeView!!.setText(EncodeTools.encode(secretKey))}private fun timer(textView : TextView) {// 动态计算当前秒数start = MyTimeUtils.getCurrentSec()runnable = Runnable {val formattedNumber = String.format("%02d",start/1000)textView.setText(formattedNumber + "s")progressBar.setProgress(start.toInt());start = start -100if(start < 0) {start= MyTimeUtils.getCurrentSec()var refreshRunnable =  Runnable {refresh()}Thread(refreshRunnable).start()}// 在这里设置下一次循环的延时时间,例如1秒handler.postDelayed(runnable!!, 100)}// 初始化计时器handler.postDelayed(runnable!!, 50) // 延时1秒后开始循环}fun showDeleteConfirmationDialog(deleteId : String) {val builder = AlertDialog.Builder(context)builder.setMessage("确定要删除吗?").setPositiveButton("Yes") { dialog, id ->// 删除操作val dbHelper = DatabaseHelper(requireContext())dbHelper.deleteUser(deleteId.toInt())val codeListFragment = CodeListFragment()parentFragmentManager.beginTransaction().replace(R.id.viewPager, codeListFragment).commit()}.setNegativeButton("No") { dialog, id ->// 取消操作,对话框不会被关闭}.setCancelable(false)val alert = builder.create()alert.show()}companion object {/*** Use this factory method to create a new instance of* this fragment using the provided parameters.** @param param1 Parameter 1.* @param param2 Parameter 2.* @return A new instance of fragment CodeShowFragment.*/// TODO: Rename and change types and number of parameters@JvmStaticfun newInstance(param1: String, param2: String) =CodeShowFragment().apply {arguments = Bundle().apply {putString(ARG_PARAM1, param1)putString(ARG_PARAM2, param2)}}}override fun onDestroyView() {if(handler!= null && runnable != null) {handler.removeCallbacks(runnable!!)}super.onDestroyView()}}

EncodeTools

作用:软件核心功能,调用jboss包的otp算法,对密钥进行运算,得出动态令牌。解析二维码扫描出的totp链接信息,转换成user实体提供软件运行

package com.shixiaotian.totp.scan.application.toolsimport com.shixiaotian.totp.scan.application.vo.User
import org.jboss.aerogear.security.otp.Totpclass EncodeTools {companion object {@JvmStaticfun encode(secretKey: String,timeStep: Long = 30,digits: Int = 6,algorithm: String = "SHA1"): String? {if (secretKey == null || secretKey.isBlank()) {return ""}try {val totp = Totp(secretKey);var result = totp.now();return result;} catch (e: Exception) {return "ERROR SK"}}@JvmStaticfun decode(uri: String): User? {if(uri.isEmpty()) {return null}if(!uri.startsWith("otpauth://totp/")) {return null}try {var uriContentIndex = uri.indexOf("otpauth://totp/");var uriContent = uri.subSequence(15, uri.length);val secContents = uriContent.split(":");var issuer = secContents.get(0);var otherContent = secContents.get(1)val secOtherContent= otherContent.split("?")var username = secOtherContent.get(0)var thOtherContent = secOtherContent.get(1)val fthOtherContent = thOtherContent.split("&")var secretKeyContent = fthOtherContent.get(0)var secretKey = secretKeyContent.split("=").get(1)var user = User(0, username, secretKey, issuer)return user}catch (e: Exception) {return null}}}}

FirstRunTools

作用:检测软件是否为第一次安装使用,该段代码不完全适用,建议使用者进行改造,或者移除

package com.shixiaotian.totp.scan.application.toolsimport android.content.Context
import android.content.SharedPreferences
import com.shixiaotian.totp.scan.application.common.MyConstantsclass FirstRunTools {companion object {@JvmStatic fun isFirstRun(context: Context): Boolean {val prefs: SharedPreferences = context.getSharedPreferences(MyConstants.firstRunTag, Context.MODE_PRIVATE)val isFirstTime = prefs.getBoolean(MyConstants.firstRunTag + "isFirstTime", true)if (isFirstTime) {val editor = prefs.edit()editor.putBoolean(MyConstants.firstRunTag + "isFirstTime", false)editor.apply()return true}return false}}
}

MyTimeUtils

作用:时间工具,因为totp是每30秒计算一次,而每次进入软件的时间不同,该功能用于纠正进入的时间差,让令牌刷新倒计时进入精确的时间区间。

package com.shixiaotian.totp.scan.application.toolsimport android.icu.util.Calendarclass MyTimeUtils {companion object {@JvmStatic fun getCurrentSec(): Long {// 获取当前时间的毫秒数val currentTimeMillis = System.currentTimeMillis()// 创建Calendar实例val calendar = Calendar.getInstance()// 设置Calendar的时间为当前时间calendar.timeInMillis = currentTimeMillis// 将秒和毫秒字段重置为0calendar.set(Calendar.SECOND, 0)calendar.set(Calendar.MILLISECOND, 0)// 当前分钟的开始时间的毫秒数val startOfCurrentMinuteMillis = calendar.timeInMillis// 已过去的毫秒数val elapsedMillis = currentTimeMillis - startOfCurrentMinuteMillis// 已过去的秒数//val elapsedSeconds = elapsedMillis / 1000if(elapsedMillis > 30000) {return 60000 - elapsedMillis;} else {return 30000 - elapsedMillis;}}}
}

User

作用:作为数据传输和存储的实体,存储用户的令牌等相关信息

package com.shixiaotian.totp.scan.application.voclass User {private var id : Int = 0private var username : String=""private var secretKey : String=""private var issuer : String=""private var code : String=""constructor(id: Int, username: String, secretKey: String, issuer: String) {this.id = idthis.username = usernamethis.secretKey = secretKeythis.issuer = issuer}fun getId() : Int {return id}fun setId(id : Int) {this.id = id}fun getUsername() : String {return username}fun setUsername(username : String) {this.username = username}fun getSecretKey() : String {return secretKey}fun setSecretKey(secretKey : String) {this.secretKey = secretKey}fun getCode() : String {return code}fun setCode(code : String) {this.code = code}fun getIssuer() : String {return issuer}fun setIssuer(issuer : String) {this.issuer = issuer}
}

CodeListAdapter

作用:适配列表内每一个数据,为令牌动态计算提供适配

package com.shixiaotian.totp.scan.applicationimport android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import com.shixiaotian.totp.scan.application.tools.EncodeTools
import com.shixiaotian.totp.scan.application.vo.User/*** 动态码列表内容适配器*/
class CodeListAdapter (context: Context, val resourceId: Int, data: List<User>) : ArrayAdapter<User>(context, resourceId, data) {override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {val view = LayoutInflater.from(context).inflate(resourceId, parent, false)val userId: TextView = view.findViewById(R.id.user_id)val issuer: TextView = view.findViewById(R.id.user_issuer)val username: TextView = view.findViewById(R.id.user_username)val userSecretKey: TextView = view.findViewById(R.id.user_secretKey)val userCode: TextView = view.findViewById(R.id.user_code)val user = getItem(position)if (user!=null){userId.text = user.getId().toString()issuer.text = user.getIssuer()username.text = user.getUsername()userSecretKey.text = user.getSecretKey()var code = EncodeTools.encode(user.getSecretKey()) as Stringuser.setCode(code)userCode.text = user.getCode()}return view}
}

MainActivity

作用:添加主页面上基本的按钮监听

package com.shixiaotian.totp.scan.applicationimport android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.FragmentManager
import com.shixiaotian.totp.scan.application.fragments.CodeAddFragment
import com.shixiaotian.totp.scan.application.fragments.CodeListFragment
import com.shixiaotian.totp.scan.application.db.DatabaseHelper
import com.shixiaotian.totp.scan.application.tools.FirstRunTools
import net.sqlcipher.database.SQLiteDatabaseclass MainActivity : AppCompatActivity() {private lateinit var listButton: Viewprivate lateinit var addButton: Viewprivate lateinit var fragmentManager: FragmentManageroverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)enableEdgeToEdge()setContentView(R.layout.activity_main)// 初始化预处理init()val codeAddFragment = CodeAddFragment()val codeListFragment = CodeListFragment()fragmentManager = supportFragmentManagerfragmentManager.beginTransaction().replace(R.id.viewPager, codeListFragment).commit()// 菜单按钮监听listButton = findViewById(R.id.menuButton)listButton.setOnClickListener {fragmentManager.beginTransaction().replace(R.id.viewPager, codeListFragment).commit()}// 添加按钮监听addButton = findViewById(R.id.addButton)addButton.setOnClickListener {fragmentManager.beginTransaction().replace(R.id.viewPager, codeAddFragment).commit()}ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)insets}}private fun init() {SQLiteDatabase.loadLibs(this);println("---开始初始化")// 判断是否首次运行if(FirstRunTools.isFirstRun(this)) {println("---首次运行触发")val dbHelper = DatabaseHelper(this)// 初始化数据库dbHelper.initDB()dbHelper.init()}}}

activity_main.xml

作用:主页面布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#3F5CB5"tools:context=".MainActivity"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:id="@+id/mainFragment"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><FrameLayoutandroid:id="@+id/viewPager"android:layout_width="match_parent"android:layout_height="match_parent"></FrameLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="200px"android:layout_alignParentBottom="true"android:background="#ffffff"android:orientation="horizontal"><ImageViewandroid:id="@+id/menuButton"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:background="#20212E"android:gravity="center_horizontal"android:src="@android:drawable/ic_menu_search"android:layout_marginRight="5px"android:textSize="30sp" /><ImageViewandroid:id="@+id/addButton"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:background="#20212E"android:gravity="center_horizontal"android:src="@android:drawable/ic_menu_add"android:layout_marginLeft="5px"android:textSize="30sp" /></LinearLayout></RelativeLayout></androidx.constraintlayout.widget.ConstraintLayout>

code_item.xml

作用:令牌列表每一个令牌的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/User"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><TextViewandroid:id="@+id/user_id"android:layout_width="0px"android:layout_height="0px"android:layout_gravity="left"android:visibility="invisible"/><TextViewandroid:id="@+id/user_secretKey"android:layout_width="0px"android:layout_height="0px"android:layout_gravity="center_vertical"android:visibility="invisible"/><TextViewandroid:id="@+id/user_issuer"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceListItemSmall"android:gravity="center_vertical"android:paddingStart="?android:attr/listPreferredItemPaddingStart"android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"android:minHeight="?android:attr/listPreferredItemHeightSmall"android:textSize="35sp"android:text="apple"/><TextViewandroid:id="@+id/user_username"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceListItemSmall"android:gravity="center_vertical"android:paddingStart="?android:attr/listPreferredItemPaddingStart"android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"android:minHeight="?android:attr/listPreferredItemHeightSmall"android:textSize="25sp"android:text="1234567@qq.com"/><TextViewandroid:id="@+id/user_code"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center_vertical"android:layout_gravity="center"android:textColor="#000000"android:text="665277"android:textSize="55sp"android:textStyle="bold"/></LinearLayout>
</LinearLayout>

fragment_code_add.xml

作用:添加令牌页面,手动添加或者,触发zxing扫码添加

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ffffff"tools:context=".fragments.CodeAddFragment"><!-- TODO: Update blank fragment layout --><LinearLayoutandroid:layout_margin="100px"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/addNameView"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="40sp"android:textStyle="bold"android:text="Username" /><EditTextandroid:id="@+id/addUsernameText"android:layout_width="match_parent"android:layout_height="wrap_content"android:maxLength="10"android:inputType="text"android:textSize="30sp"/><TextViewandroid:id="@+id/addSecretKeyView"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="40sp"android:textStyle="bold"android:text="SecretKey" /><EditTextandroid:id="@+id/addSecretKeyText"android:layout_width="match_parent"android:layout_height="wrap_content"android:maxLength="100"android:textSize="30sp"android:inputType="text" /><TextViewandroid:id="@+id/addIssuerView"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="40sp"android:textStyle="bold"android:text="Issuer" /><EditTextandroid:id="@+id/addIssuerText"android:layout_width="match_parent"android:layout_height="wrap_content"android:maxLength="10"android:inputType="text"android:textSize="30sp"/><TextViewandroid:id="@+id/saveButton"android:layout_width="match_parent"android:layout_height="100sp"android:background="#A62641"android:gravity="center"android:layout_marginTop="100px"android:textColor="#ffffff"android:text="Save"android:textSize="50sp" /><TextViewandroid:id="@+id/cameraButton"android:layout_width="match_parent"android:layout_height="100sp"android:layout_marginTop="100px"android:background="#20212E"android:gravity="center"android:text="Scan"android:textColor="#ffffff"android:textSize="50sp" /></LinearLayout>
</FrameLayout>

fragment_code_list.xml

作用:提供令牌快速查看列表,和选择单个令牌进行操作的功能

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/code_list"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".fragments.CodeListFragment"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/textView3"android:layout_width="match_parent"android:layout_height="101dp"android:gravity="center"android:background="#312F2F"android:text="30s"android:textColor="#ffffff"android:textColorLink="#FFFFFF"android:textSize="60sp" /><ListViewandroid:id="@+id/listView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:background="#ffffff"android:divider="#000000"android:dividerHeight="1dp" /><Viewandroid:layout_width="match_parent"android:layout_height="200px"android:background="#666666"></View></LinearLayout>
</FrameLayout>

fragment_code_show.xml

作用:动态令牌展示页面,提供令牌查看和删除功能

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/code_show"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#312F2F"tools:context=".fragments.CodeShowFragment"><!-- TODO: Update blank fragment layout --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/showIssuerTextView"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:layout_marginTop="100px"android:text="steam"android:textColor="#ffffff"android:textColorLink="#FFFFFF"android:textSize="75sp" /><TextViewandroid:id="@+id/showUsernameTextView"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:layout_marginTop="30px"android:text="54526322@qq.com"android:textColor="#ffffff"android:textColorLink="#FFFFFF"android:textSize="25sp" /><TextViewandroid:id="@+id/showTimeView"android:layout_width="match_parent"android:layout_height="101dp"android:gravity="center"android:background="#312F2F"android:text="30s"android:textColor="#ffffff"android:textColorLink="#FFFFFF"android:layout_marginTop="20px"android:textSize="70sp" /><ProgressBarandroid:id="@+id/progressBar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="match_parent"android:layout_height="45dp"android:max="100"android:progress="10"android:progressDrawable="@drawable/progress_bar_color" /><TextViewandroid:id="@+id/showCodeView"android:layout_width="match_parent"android:layout_height="wrap_content"android:freezesText="false"android:gravity="center_horizontal"android:layout_marginTop="50px"android:text="9TXTSY"android:textColor="#ffffff"android:textColorLink="#FFFFFF"android:textSize="80sp" /><TextViewandroid:id="@+id/deleteButton"android:layout_alignParentBottom="true"android:layout_width="match_parent"android:layout_height="100sp"android:background="#20212E"android:gravity="center"android:layout_margin="50px"android:textColor="#ffffff"android:text="Delete"android:textSize="50sp" /></LinearLayout></FrameLayout>

AndroidManifest.xml

作用:添加相机权限,设定zxing相机扫码activity相机方向等相关数据

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><uses-permission android:name="android.permission.CAMERA" /><applicationandroid:allowBackup="true"android:dataExtractionRules="@xml/data_extraction_rules"android:fullBackupContent="@xml/backup_rules"android:icon="@android:drawable/ic_lock_lock"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.MyApplication"tools:targetApi="31"><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name="com.journeyapps.barcodescanner.CaptureActivity"android:screenOrientation="fullSensor"tools:replace="screenOrientation" /></application></manifest>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/498528.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Type c系列接口驱动电路·内置供电驱动电路使用USB2.0驱动电路!!!

目录 前言 Type c常见封装类型 Type c引脚功能详解 Type c常见驱动电路详解 Type c数据手册 ​​​​​​​ ​​​​​​​ 编写不易&#xff0c;仅供学习&#xff0c;请勿搬运&#xff0c;感谢理解 常见元器件驱动电路文章专栏连接 LM7805系列降压芯片驱动电路…

【竞技宝】LOL:IG新赛季分组被质疑

北京时间2024年12月31日&#xff0c;今天已经2024年的最后一天&#xff0c;在进入一月之后&#xff0c;英雄联盟将迎来全新的2025赛季。而目前新赛季第一阶段的抽签结果已经全部出炉&#xff0c;其中人气最高的IG战队在本次抽签中抽到了“绝世好签”引来了网友们的质疑。 首先介…

【大模型实战篇】Mac本地部署RAGFlow的踩坑史

1. 题外话 最近一篇文章还是在11月30日写的&#xff0c;好长时间没有打卡了。最近工作上的事情特别多&#xff0c;主要聚焦在大模型的预训练、微调和RAG两个方面。主要用到的框架是Megatron-DeepSpeed&#xff0c;后续会带来一些分享。今天的文章主要聚焦在RAG。 近期调研了一系…

Prompt工程--AI开发--可置顶粘贴小工具

PROMPT 1.背景要求&#xff1a;我需要开发一个简单的粘贴小工具&#xff0c;用于方便地粘贴和管理文本内容。该工具需要具备以下功能&#xff1a;粘贴功能&#xff1a;提供一个文本框&#xff0c;用户可以粘贴内容。窗口置顶&#xff1a;支持窗口置顶功能&#xff0c;确保窗口…

FPGA自学之路:到底有多崎岖?

FPGA&#xff0c;即现场可编程门阵列&#xff0c;被誉为硬件世界的“瑞士军刀”&#xff0c;其灵活性和可编程性让无数开发者为之倾倒。但谈及FPGA的学习难度&#xff0c;不少人望而却步。那么&#xff0c;FPGA自学之路到底有多崎岖呢&#xff1f; 几座大山那么高&#xff1f;…

它真的可以绕过 ICloud 激活吗

作为最著名的越狱辅助应用程序之一&#xff0c;3u工具 非常出色地将各种越狱工具和功能集成到一个应用程序中。除了越狱之外&#xff0c;3u工具 有时也被认为是 iCloud 激活锁绕过工具。 但3u工具真的能绕过激活锁吗&#xff1f; 如果没有的话还有其他的应用吗&#xff1f; 这…

手写顺序流程图组件

效果图 完整代码 <template><div><div class"container" :style"{ width: ${spacingX * (colNum - 1) itemWidth * colNum}px }"><divv-for"(item, i) in recordList":key"i"class"list-box":style&…

SimForge HSF 案例分享|复杂仿真应用定制——UAVSim无人机仿真APP(技术篇)

导读 「神工坊」核心技术——「SimForge HSF高性能数值模拟引擎」支持工程计算应用的快速开发、自动并行&#xff0c;以及多域耦合、AI求解加速&#xff0c;目前已实现航发整机数值模拟等多个系统级高保真数值模拟应用落地&#xff0c;支持10亿阶、100w核心量级的高效求解。其低…

揭秘文件上传漏洞之操作原理(Thoughts on File Upload Vulnerabilities)

从上传到入侵&#xff1a;揭秘文件上传漏洞之操作原理 大家好&#xff0c;今天我们来聊一个"老而弥坚"的漏洞类型 —— 文件上传漏洞。虽然这个漏洞存在很多年了&#xff0c;但直到现在依然频频出现在各种漏洞报告中。今天我们就来深入了解一下它的原理和各种校验方…

网络安全 | 云安全与物联网(IoT)

网络安全 | 云安全与物联网&#xff08;IoT&#xff09; 一、前言二、云计算与物联网概述2.1 云计算2.2 物联网 三、物联网中的云安全需求与挑战3.1 数据安全3.2 网络安全3.3 身份认证与访问控制3.4 设备安全 四、云安全在物联网中的应对策略4.1 技术层面4.2 管理层面 五、案例…

FFmpeg:详细安装教程与环境配置指南

FFmpeg 部署完整教程 在本篇博客中&#xff0c;我们将详细介绍如何下载并安装 FFmpeg&#xff0c;并将其添加到系统的环境变量中&#xff0c;以便在终端或命令行工具中直接调用。无论你是新手还是有一定基础的用户&#xff0c;这篇教程都能帮助你轻松完成 FFmpeg 的部署。 一、…

基于Redis有序集合实现滑动窗口限流

滑动窗口算法是一种基于时间窗口的限流算法&#xff0c;它将时间划分为若干个固定大小的窗口&#xff0c;每个窗口内记录了该时间段内的请求次数。通过动态地滑动窗口&#xff0c;可以动态调整限流的速率&#xff0c;以应对不同的流量变化。 整个限流可以概括为两个主要步骤&a…

C++——deque的了解和使用

目录 引言 标准库中的deque 一、deque的基本概念 二、deque的常用接口 1.deque的迭代器 2.deque的初始化 3.deque的容量操作 3.1 有效长度和容量大小 3.2 有效长度和容量操作 4.deque的访问操作 5.deque的修改操作 三、deque的应用场景 结束语 引言 在C中&#x…

【蓝桥杯】:蓝桥杯之路径之谜

题目分析 这是一道路径谜题&#xff0c;描述了一个骑士在一个(n\times n)方格组成的城堡中行走的问题。骑士从西北角&#xff08;入口&#xff09;走到东南角&#xff08;出口&#xff09;&#xff0c;可以横向或纵向移动&#xff0c;但不能斜着走&#xff0c;也不能跳跃。每走…

Mybatis 入门

Mybatis 入门 一、简介 mybatis 是一个优秀的基于 java 的持久层框架&#xff0c;它内部封装了 jdbc&#xff0c;使开发者只需要关注 sql 语句本身&#xff0c; 而不需要花费精力去处理加载驱动、创建连接、创建 statement 等繁杂的过程。 mybatis 通过 xml 或注解的方式将要…

《Java核心技术 卷II》流的创建

流的创建 Collection接口中stream方法可以将任何集合转换为一个流。 用静态Stream.of转化成数组。 Stream words Stream.of(contents.split("\\PL")); of方法具有可变长参数&#xff0c;可以构建具有任意数量的流。 使用Array.stream(array,from,to)可以用数组…

uniapp:微信小程序文本长按无法出现复制菜单

一、问题描述 在集成腾讯TUI后&#xff0c;为了能让聊天文本可以复制&#xff0c;对消息组件的样式进行修改&#xff0c;主要是移除下面的user-select属性限制&#xff1a; user-select: none;-webkit-user-select: none;-khtml-user-select: none;-moz-user-select: none;-ms…

UFS供电

UFS device结构图如上所示&#xff0c;可以看到有三路电源&#xff1a;VCC&#xff0c;VCCQ和VCCQ2。定义如下&#xff1a; 这三路电压参数如下&#xff1a; 上电时序如下所示&#xff1a; 但实际使用的UFS device产品&#xff0c;可能与spce略有不同。我看到的几款三星、美光和…

c++类和对象(六个默认成员函数)

文章目录 一.类的六个默认成员函数二.构造函数1.概念2.特性 三.析构函数1.概念2.特性 四.拷贝构造函数1.概念2.特性 五.赋值操作符重载5.1运算符重载5.2 赋值运算符重载 一.类的六个默认成员函数 如果一个类中什么成员都没有&#xff0c;简称为空类。空类中什么都没有吗&#x…

互联网直播点播平台EasyDSS无人机视频推拉流技术实现工地远程监控巡检直播

在建筑行业&#xff0c;施工现场的安全管理和实时监控一直是项目管理中的重点。随着技术的进步&#xff0c;无人机工地直播技术成为了一种新兴的解决方案&#xff0c;它不仅能够提高施工透明度&#xff0c;还能够加强现场安全管理。EasyDSS作为一种先进的流媒体技术平台&#x…