1 回答
TA贡献1895条经验 获得超3个赞
新浪网.ParseRSAPublicKeyFromPEM()
在内部调用 x509。ParsePKIXPublicKey()
方法,用于导入 X.509/SPKI 格式的 PEM 编码密钥。
X.509/SPKI 格式的 PEM 编码密钥可以使用选项 -e -m pkcs8 从带有 ssh-keygen 的私钥派生。目前,应用了选项 -e -m pem,该选项生成 PKCS#1 格式的公钥,该公钥无法由 处理。jwt.ParseRSAPublicKeyFromPEM()
如果使用 X.509/SPKI 格式的 PEM 编码密钥,则公钥导入工作正常:
package main
import (
"fmt"
"log"
"github.com/dgrijalva/jwt-go"
)
var key = `-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAo+fW9kv2Y5Fjky8TQLK3rYtjqCKIEf0Yjm1lXTnkpWVBjO7x
EzB7HYBkHnIA19HEdzjeKLlLcJevOU6G3p+t8/5vVhciPnhKpwU6ZzrR0P3Q2toC
+KgtrnPHXpm8Py/HVwBxHWZ352NNa5J3dHv6a7B1k7IMAaBT053P99l1NQrBCVJf
kXckQAUOnsYk/PKFFufNhlu3nAd+eUl0Iv1IAWsUb5lHKkIOOWxnlreFN5gdCgL6
YxLwXRBV010zu/Y9zF8zdhSZXWoIvD/JpOvkKh9ym1VE0YzYkSW+m0XnTa4VMdZf
mWcQNKhYZdNg0TqwRco893fVeo756JzOHQ0KcQIDAQABAoIBADnxO4zWTcPlIc5m
VegJReWT4ScPDgtN7eBry+mpDatqoEGyNocSHHPRb5NTufiRr2J2OBMtbf3foZIg
sI0C5fvbdoB4rSJoY/unXX4gzQuUrsCvUV2WFpAVXeWTc3ji6xVWkqZSExE5iaT9
oj0Llvt65hXQW243v2qP33U+6rBVC/2NozGzlq9uHvNJURF0/ZztIo47K6q+giZC
ONvycXVGT8h5PIzfQsnnrsaLYCNe+yJ1x9zPT32xStI8+rF/PFBO1T8umna6+kY2
pY5XQfiYH7gfUIqfjA48Ovnxdmyk9ib6DzcgB+vlY+4cgCGX27sbG99pxGKu8nGg
iJjBGAECgYEA1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t
8/5vVhciPnhKpwU6ZzrR0P3Q2toC+KgtrnPHXpm8Py/HVwBxHWZ352NNa5J3dHv6
a7B1k7IMAaBT053P99l1NQrBCVJfkXckQAUOnsYk/PKFFufNhlu3nAd+eUl0Iv1I
AWsUb5lHKkIOOWxnlreFN5gdCgL6YxLwXRBV010zu/Y9zF8zdhSZXWoIvD/JpOvk
Kh9ym1VE0YzYkSW+m0XnTa4VMdZfmWcQNKhYZdNg0TqwRco893fVeo756JzOHQ0K
cQIDAQAB
-----END PUBLIC KEY-----`
func main() {
_, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(key))
if err != nil {
log.Println("errorPrivate:", err)
}
_, err = jwt.ParseRSAPublicKeyFromPEM([]byte(pubkey))
if err != nil {
log.Println("errorPublic:", err)
}
fmt.Println("Done")
}
请注意,dgrijalva/jwt-go 似乎没有被维护,s. #457,并且存在许多未解决的问题。
- 1 回答
- 0 关注
- 298 浏览
添加回答
举报