Kotlin实现签名
首先祝我中秋节快乐。
上代码--->
object SignatureUtils {
/**
* 签名
*/
fun sign(str: String, privateKey: PrivateKey): String {
val signature = Signature.getInstance("SHA256withRSA")
//初始化
signature.initSign(privateKey)
//数据源
signature.update(str.toByteArray())
//签名
val sign = signature.sign()
return Base64.encode(sign)
}
/**
* 校验
*/
fun verify(str: String, sign: String, publicKey: PublicKey): Boolean {
//校验
val signature = Signature.getInstance("SHA256withRSA")
signature.initVerify(publicKey)
signature.update(str.toByteArray())
return signature.verify(Base64.decode(sign))
}
}
fun main(args: Array) {
val str = "老婆快给我转移1000万"
val privateKey = RSACrypt.getPrivateKey()
val publicKey = RSACrypt.getPublicKey()
val result = SignatureUtils.sign(str, privateKey!!)
println("签名的结果:$result")
//校验
val verify = SignatureUtils.verify(str, result, publicKey!!)
println("校验结果:$verify")
}
如何生成私钥和公钥