简体   繁体   English

在 Go 中使用 a.pem 文件私钥对带有 RSASSA-PSS 的字符串进行签名

[英]Using a .pem file private key to sign a string with RSASSA-PSS in Go

I'm trying to set up Amazon Pay, and following their guide for signing requests: https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-api-v2/signing-requests.html我正在尝试设置 Amazon Pay,并遵循他们的签名请求指南: https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-api-v2/signing-requests.html

I'm stuck on Step 3. I have a.pem file from Amazon containing a private key.我被困在第 3 步。我有一个来自亚马逊的 .pem 文件,其中包含一个私钥。 It looks like:看起来像:

-----BEGIN PRIVATE KEY-----
M...
-----END PRIVATE KEY-----

I'm supposed to sign a string I created earlier using the RSASSA-PSS algorithm with SHA256 hashing and a salt length of 20. I see that Go has a function to do so ( https://golang.org/pkg/crypto/rsa/#SignPSS ), but I'm not sure how to take my.pem file private key and use it in this function. I'm supposed to sign a string I created earlier using the RSASSA-PSS algorithm with SHA256 hashing and a salt length of 20. I see that Go has a function to do so ( https://golang.org/pkg/crypto/ rsa/#SignPSS ),但我不确定如何获取 my.pem 文件私钥并在此 function 中使用它。 It wants an *rsa.PrivateKey by the looks of it...它看起来想要一个 *rsa.PrivateKey ......

I've tried this...我试过这个...

var privateKeyString = `-----BEGIN PRIVATE KEY-----...`
decoded, _ := pem.Decode([]byte(privateKeyString))
parsed, _ := x509.ParsePKCS8PrivateKey(decoded.Bytes)
privateKey := parsed.(*rsa.PrivateKey)

But pem.Decode is returning nil.但是 pem.Decode 返回 nil。 I've also tried:我也试过:

parsed, _ := x509.ParsePKCS8PrivateKey([]byte(privateKeyString))

But this returns nil as well.但这也返回 nil。 I'm not familiar with crypto type stuff at all, so if anybody could provide some guidance it would be much appreciated!我根本不熟悉加密类型的东西,所以如果有人可以提供一些指导,将不胜感激!

Decode was returning nil because of how the private key string in back ticks was auto-indented.由于反引号中的私钥字符串是如何自动缩进的,解码返回 nil。 Removing all the indentation fixed the issue.删除所有缩进解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM