当前位置: 首页 > news >正文

Golang rsa 验证

一下代码用于rsa 签名的验签,

签名可以用其他语言产生。也可以用golang生成。

package mainimport ("crypto""crypto/rsa""crypto/sha256""crypto/x509""encoding/pem""errors""fmt"
)func ParseRsaPublicKeyFromPemStr(pubPEM string) (*rsa.PublicKey, error) {block, _ := pem.Decode([]byte(pubPEM))if block == nil {return nil, errors.New("failed to parse PEM block containing the key")}pub, err := x509.ParsePKCS1PublicKey(block.Bytes)if err != nil {return nil, err}return pub, nil// switch pub := pub.(type) {// case *rsa.PublicKey:// 	return pub, nil// default:// 	break // fall through// }// return nil, errors.New("Key type is not RSA")
}func main() {// The GenerateKey method takes in a reader that returns random bits, and// the number of bits// privateKey, err := rsa.GenerateKey(rand.Reader, 2048)// if err != nil {// 	panic(err)// }// The public key is a part of the *rsa.PrivateKey struct// publicKey := privateKey.PublicKeypublicKey, err := ParseRsaPublicKeyFromPemStr(`-----BEGIN RSA PUBLIC KEY-----
MIIBxxxxxx
-----END RSA PUBLIC KEY-----`)if err != nil {panic(err)}// // use the public and private keys// // ...// // https://play.golang.org/p/tldFUt2c4nx// modulusBytes := base64.StdEncoding.EncodeToString(privateKey.N.Bytes())// privateExponentBytes := base64.StdEncoding.EncodeToString(privateKey.D.Bytes())// fmt.Println(modulusBytes)// fmt.Println(privateExponentBytes)// fmt.Println(publicKey.E)// encryptedBytes, err := rsa.EncryptOAEP(// 	sha256.New(),// 	rand.Reader,// 	&publicKey,// 	[]byte("super secret message111"),// 	nil)// if err != nil {// 	panic(err)// }// fmt.Println("encrypted bytes: ", encryptedBytes)// // The first argument is an optional random data generator (the rand.Reader we used before)// // we can set this value as nil// // The OEAPOptions in the end signify that we encrypted the data using OEAP, and that we used// // SHA256 to hash the input.// decryptedBytes, err := privateKey.Decrypt(nil, encryptedBytes, &rsa.OAEPOptions{Hash: crypto.SHA256})// if err != nil {// 	panic(err)// }// // We get back the original information in the form of bytes, which we// // the cast to a string and print// fmt.Println("decrypted message: ", string(decryptedBytes))msg := []byte(`{"user_agent":"test"}`)// // Before signing, we need to hash our message// // The hash is what we actually signmsgHash := sha256.New()_, err = msgHash.Write(msg)if err != nil {panic(err)}msgHashSum := msgHash.Sum(nil)// // In order to generate the signature, we provide a random number generator,// // our private key, the hashing algorithm that we used, and the hash sum// // of our message// signature, err := rsa.SignPSS(rand.Reader, privateKey, crypto.SHA256, msgHashSum, nil)// if err != nil {// 	panic(err)// }signature := []byte{27, 79, , 8}// To verify the signature, we provide the public key, the hashing algorithm// the hash sum of our message and the signature we generated previously// there is an optional "options" parameter which can omit for nowerr = rsa.VerifyPSS(publicKey, crypto.SHA256, msgHashSum, signature, nil)if err != nil {fmt.Println("could not verify signature: ", err)return}// If we don't get any error from the `VerifyPSS` method, that means our// signature is validfmt.Println("signature verified")
}

参考:
https://gist.github.com/sohamkamani/08377222d5e3e6bc130827f83b0c073e

http://www.lryc.cn/news/254026.html

相关文章:

  • 网络安全威胁——跨站脚本攻击
  • Java利用UDP实现简单的双人聊天
  • HBase整合Phoenix
  • C# 委托/事件/lambda
  • 13款趣味性不错(炫酷)的前端动画特效及源码(预览获取)分享(附源码)
  • C# 友元程序集
  • CRM系统的数据分析和报表功能对企业重要吗?
  • 【单体架构事务失效解决方式之___代理对象加锁】
  • 面试被问到 HTTP和HTTPS的区别有哪些?你该如何回答~
  • 点评项目——短信登陆模块
  • 2023亚太地区五岳杯量子计算挑战赛
  • Python 模块的使用方法
  • 【知识】稀疏矩阵是否比密集矩阵更高效?
  • 代码随想Day24 | 回溯法模板、77. 组合
  • 搜索与回溯算法②
  • Centos图形化界面封装OpenStack Ubuntu镜像
  • 使用Jmeter进行http接口测试怎么做?
  • 创建腾讯云存储桶---上传图片--使用cos-sdk完成上传
  • 12.3_黑马MybatisPlus笔记(上)
  • 智能优化算法应用:基于寄生捕食算法无线传感器网络(WSN)覆盖优化 - 附代码
  • 全息图着色器插件:Hologram Shaders Pro for URP, HDRP Built-in
  • Python Opencv实践 - 简单的AR项目
  • Java不可变集合
  • openGauss学习笔记-146 openGauss 数据库运维-备份与恢复-配置文件的备份与恢复
  • 一文读懂中间件
  • 【编程基础心法】「设计模式系列」让我们一起来学编程界的“兵法”设计模式(序章)
  • 技术阅读周刊第第8️⃣期
  • HTML程序大全(2):通用注册模版
  • 【循环结构 for、break、continue高级用法】
  • JAVA网络编程——BIO、NIO、AIO深度解析