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.mzph.cn/news/181767.shtml

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

相关文章

分治法之快速排序

思路: 选择一个基准值,通常是数组中的第一个元素。 将数组分为两部分,一部分是小于基准值的元素,另一部分是大于基准值的元素。 对这两部分分别进行递归排序,直到子数组长度为 1 或 0。 合并排序好的两部分,得到最终…

第二十章Java博客

如果一次只完成一件事情,很容易实现。但现实生活中,很多事情都是同时进行的。Java中为了模拟这种状态,引入了线程机制。简单地说,当程序同时完成多件事情时,就是所谓的多线程。多线程应用相当广泛,使用多线…

【Java学习笔记】 74 - 本章作业

1.验证电子邮件格式是否合法 规定电子邮件规则为 1.只能有一个 2. 前面是用户名,可以是a-z A-Z 0-9 _ - 字符 3. 后面是域名,并且域名只能是英文字母,比如sohu.com或者tsinghua.org.cn 4.写出对应的正则表达式,验证输入的字符串是否为满…

浏览器触发下载Excel文件-Java实现

目录 1:引入maven 2:代码实现 3.导出通讯录信息到Excel文件 4.生成并下载Excel文件部分解释 1:引入maven 添加依赖:首先,在你的项目中添加EasyExcel库的依赖。你可以在项目的构建文件(如Maven的pom.xml)中添加以下依赖项:<dependency><groupId>com.alib…

Linux-chrpath指令

chrpath指令用于改变程序的rpath/runpath&#xff0c;从而改变程序运行时的动态库搜索路径&#xff0c;常见用法为 chrpath -r "/path/to/shared_library" binary_name chrpath -r "$ORIGIN:/$ORIGIN/../lib"第一种用法将一个指定的路径替换原程序的rpath…

Python基础语法之学习input()函数

Python基础语法之学习input函数 前言一、代码二、效果 前言 一、代码 # 默认是字符串类型 number input("请输入一个数字&#xff1a;") print("输入的数字是",number)二、效果 没有人可以阻止你成为自己想成为的人&#xff0c;只有你自己才能放弃梦想。…

idea git合并推送分支

远端代码合并到当前分支 1.本地切换到当前分支 2.远端目标分支右键合并到当前分支(使用合并拉入) 本地当前分支推送合并到远端分支 1.切换到远端本地分支 2.合并本地其他分支(想要推送的分支)到当前分支 3.推送分支 注意:这里的合并是:将XXX合并到XXX中 …

【LeetCode刷题笔记】160.相交链表

创作不易&#xff0c;本篇文章如果帮助到了你&#xff0c;还请点赞 关注支持一下♡>&#x16966;<)!! 主页专栏有更多知识&#xff0c;如有疑问欢迎大家指正讨论&#xff0c;共同进步&#xff01; 更多算法知识专栏&#xff1a;算法分析&#x1f525; 给大家跳段街舞感谢…

Spring(2):Spring事务管理机制

Spring事务管理高层抽象主要包括3个接口&#xff0c;Spring的事务主要是由他们共同完成的&#xff1a; PlatformTransactionManager&#xff1a;事务管理器—主要用于平台相关事务的管理。TransactionDefinition&#xff1a; 事务定义信息(隔离、传播、超时、只读)—通过配置如…

LeetCode算法题解(动态规划)|LeetCode198. 打家劫舍、LeetCode213. 打家劫舍 II、LeetCode337. 打家劫舍 III

一、LeetCode198. 打家劫舍 题目链接&#xff1a;198. 打家劫舍 题目描述&#xff1a; 你是一个专业的小偷&#xff0c;计划偷窃沿街的房屋。每间房内都藏有一定的现金&#xff0c;影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统&#xff0c;如果两间相邻的…

android 12 添加菜单

1.创建一级菜单 packages\apps\Settings\res\xml\top_level_settings.xml <com.android.settings.widget.HomepagePreferenceandroid:fragment"com.android.settings.DeviceStatusSettings"android:icon"drawable/ic_settings_display_white"android:…

harmonyos应用开发者高级认证考试部分答案(2)

一、判断 只要使用端云一体化的云端资源就需要支付费用&#xff08;错&#xff09; 所有使用Component修饰的自定义组件都支持onPageShow&#xff0c;onBackPress和onPageHide生命周期函数。&#xff08;错&#xff09; HarmonyOS应用可以兼容OpenHarmony生态&#xff08;对&am…

哪里可了解低代码数据可视化开发平台?

如果想要提升办公协作效率&#xff0c;可以用什么样的平台助力实现这一目标&#xff1f;其实&#xff0c;随着市场竞争的日益加剧&#xff0c;低代码技术平台的应用价值也逐渐凸显出来&#xff0c;其可视化、易操作、灵活便利等优势特点&#xff0c;是很多中大型企业倾向于使用…

QMap使用(插入遍历删除)

1、插入的方式 int i 0; double j 1.2; QMap<int,double>map; map.insert(i,j); 2、遍历的方式 可以参考&#xff1a;史上最全的QMap遍历方式_不午休の野猫的博客-CSDN博客 第一种迭代器遍历&#xff1a;&#xff08;可读、可写&#xff0c;可修改元素&#xff09…

Day 14周二

Day 14周二 每日算法 题目1 反转字符串 妈的这个代码我刚刚开始看了半天&#xff0c;想着&#xff0c;里面的那个reverse为什么就不反转啊&#xff0c;结果是我sb了&#xff0c;那个for循环的括号没有把start i 1&#xff1b;给包含进去&#xff0c;找个start根本一直都是…

主从同步机制

RocketMQ的Broker分为Master和Slave两个角色&#xff0c;为了保证高可用性&#xff0c;Master角色的机器接收到消息后&#xff0c;要把内容同步到Slave机器上&#xff0c;这样一旦Master宕机&#xff0c;Slave机器依然可以提供服务。下面分析Master和Slave角色机器间同步功能实…

Nature子刊最新研究:Hi-C宏基因组揭示土壤-噬菌体-宿主相互作用

土壤中有大量的噬菌体。然而&#xff0c;大多数宿主未知&#xff0c;无法获得其基因组特征。2023年11月23日&#xff0c;最新发表于《Nature communications》期刊题为“Hi-C metagenome sequencing reveals soil phage–host interactions”的文章&#xff0c;通过高通量染色体…

2023 最新版navicat 下载与安装 步骤及演示 (图示版)

2023 最新版navicat 下载与安装 步骤演示 -图示版 1. 下载Navicat2 .安装navicat 160 博主 默语带您 Go to New World. ✍ 个人主页—— 默语 的博客&#x1f466;&#x1f3fb; 《java 面试题大全》 &#x1f369;惟余辈才疏学浅&#xff0c;临摹之作或有不妥之处&#xff0c…

?.的用法

使用场景&#xff1a; undefined和null是两个比较特殊的数据类型,是不能用点操作符去访问属性的&#xff0c;否则将会报错。 空值合并操作符?? 使用 ?? 时用于提供默认值&#xff0c;只有当左侧表达式的结果为 null 或 undefined 时&#xff0c;才会返回右侧表达式的值。 …

时钟控制模块

时钟控制模块 锁相环电路简单的理解 https://www.bilibili.com/video/BV1yS4y1n7vV/?spm_id_from333.337.search-card.all.click&vd_source712cdb762d6632543eeeadb56271617a一 时钟是从哪里来的 时钟晶振&#xff08;32.768KHz&#xff09;供给RTC使用在IMX6ULL的T16和…