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

影视制作南昌seo

影视制作,南昌seo,广州站,拓者吧室内设计吧官网一下代码用于rsa 签名的验签, 签名可以用其他语言产生。也可以用golang生成。 package mainimport ("crypto""crypto/rsa""crypto/sha256""crypto/x509""encoding/pem""errors""fmt" )fun…

一下代码用于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.yayakq.cn/news/131731/

相关文章:

  • 做外卖的网站上海网站建设设计公司排名
  • 怎样增加网站流量物流网络名词解释
  • 建设美食网站如何注册域名及网站
  • 百度网盘做存储网站企业方案
  • 香橼做空机构网站网络营销的工作岗位有哪些
  • 天津专门做网站电子商城网站开发要多少钱
  • 网站开发服务费记账觅知网是免费的吗
  • 哪些行业需要网站有哪些内容网站备案许可证
  • 商水县住房城乡建设网站服装设计效果图
  • 哪个网站能帮助做路书做网站怎么导入地图
  • 有做软件的网站有哪些宿迁房产网信息网
  • 学校网站的目的企业网站的推广方式和手段有哪些
  • 小程序网站开发怎么样百度商家
  • 湖北省住房部城乡建设厅网站呼和浩特市网站建设
  • 做相册哪个网站好百度扫一扫识别图片
  • 网站footer怎么做南宁网站开发培训
  • 广东省网站建设公司排名网易搜索引擎入口
  • 网站正在建设中源码中铁建设集团门户网登录快照
  • 网站首页幻灯片不显示wordpress动
  • 青岛专业网站推广设计公司网站打不开 ...
  • 论述网站建设的主要内容互联网相关网站
  • 网站基础模板西宁软件优化网站建设
  • 网站建设行业2017女子录视频车流中热舞
  • 网站建设时间影响因素建湖做网站
  • 做消费金融网站价格有那个网站可以做食品台账
  • 网站即将 模板手机怎么网站模板
  • 建设网站与服务器wordpress 自定义登录页面模板
  • 揭阳做网站wordpress源码好乱
  • 怎么做网站动态框男女激烈做羞羞事网站网站韩剧
  • 做网站模板在哪儿找百度云虚拟主机上传wordpress