使用 RSA 验证并签署 JSON Web 令牌从 NodeJS 到 Golang,反之亦然?
Verify and Sign JSON Web Tokens from NodeJS to Golang and vice-versa using RSA?
我已经在 Golang 中生成了一个私钥,它应该用于签署 JSON 发给其他用 NodeJS 编码的服务的 Web 令牌。
在交换任何令牌之前,NodeJS 服务应存储 Golang 客户端的 public 密钥。
我的问题是,我不知道如何将 rsa.PublicKey 导出为与 NodeJS 的 npmjs 一起工作的格式。com/package/jsonwebtoken 并在另一个方向做同样的事情;这意味着向 Golang 客户端提供 NodeJS 服务的 public 密钥来验证传入的令牌。
编辑:
有问题的代码https://github.com/zarkones/XENA
查看文件 /xena-apep/main.go 第 16 行和第 36 行。
编辑2:
这里是有问题的代码:
var privateIdentificationKey = generatePrivateKey() // Generating the private key.
identify(id.String(), privateIdentificationKey.publicKey) // Won't work because of the type miss-match.
/* Generates a private key. */
func generatePrivateKey() *rsa.PrivateKey {
secret, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
fmt.Println(err)
}
return secret
}
/* Makes Xena-Atila aware of its existence. */
func identify(id string, identificationKey string) {
insertionPayload := []byte(`{"id":"` + id + `","identificationKey":"` + identificationKey + `","status":"ALIVE"}`)
request, err := http.NewRequest("POST", centralizedHost+"/v1/clients", bytes.NewBuffer(insertionPayload))
request.Header.Set("Content-Type", "application/json")
if err != nil {
fmt.Println("Unable to connect to the centralized host.")
}
client := &http.Client{}
response, err := client.Do(request)
defer response.Body.Close()
}
node-jsonwebtoken 中的 jwt.sign()
和 jwt.verify()
方法需要 PEM 编码密钥。因此,NodeJS 端将提供 public 密钥 PEM 编码或期望 PEM 编码密钥(X.509/SPKI 格式或 PKCS#1 格式)。
密钥导出和导入在crypto/x509
包中使用Go实现,encoding/pem
包中使用PEM编码,crypto/rsa
包中使用RSA。
在 Go 中使用发布的 GeneratePrivateKey()
方法生成私钥和 public 密钥是:
privateKey := GeneratePrivateKey()
publicKey := &privateKey.PublicKey
可以在 Go 中以 X.509/SPKI 格式导出 PEM 编码的 public 密钥,例如有:
func ExportSPKIPublicKeyPEM(pubkey *rsa.PublicKey) (string){
spkiDER, _ := x509.MarshalPKIXPublicKey(pubkey)
spkiPEM := pem.EncodeToMemory(
&pem.Block{
Type: "PUBLIC KEY",
Bytes: spkiDER,
},
)
return string(spkiPEM)
}
或者,可以使用 MarshalPKCS1PublicKey()
导出 PKCS#1 格式的 PEM 编码 public 密钥。为此,必须指定 Type
RSA PUBLIC KEY
。
可以使用 ASN.1 解析器检查导出的密钥,例如在线:https://lapo.it/asn1js/
可以在 Go 中导入 X.509/SPKI 格式的 PEM 编码 public 密钥,例如有:
func ImportSPKIPublicKeyPEM(spkiPEM string) (*rsa.PublicKey) {
body, _ := pem.Decode([]byte(spkiPEM ))
publicKey, _ := x509.ParsePKIXPublicKey(body.Bytes)
if publicKey, ok := publicKey.(*rsa.PublicKey); ok {
return publicKey
} else {
return nil
}
}
使用 ParsePKCS1PublicKey()
可以导入 PKCS#1 格式的 PEM 编码 public 密钥。
我已经在 Golang 中生成了一个私钥,它应该用于签署 JSON 发给其他用 NodeJS 编码的服务的 Web 令牌。
在交换任何令牌之前,NodeJS 服务应存储 Golang 客户端的 public 密钥。
我的问题是,我不知道如何将 rsa.PublicKey 导出为与 NodeJS 的 npmjs 一起工作的格式。com/package/jsonwebtoken 并在另一个方向做同样的事情;这意味着向 Golang 客户端提供 NodeJS 服务的 public 密钥来验证传入的令牌。
编辑:
有问题的代码https://github.com/zarkones/XENA 查看文件 /xena-apep/main.go 第 16 行和第 36 行。
编辑2:
这里是有问题的代码:
var privateIdentificationKey = generatePrivateKey() // Generating the private key.
identify(id.String(), privateIdentificationKey.publicKey) // Won't work because of the type miss-match.
/* Generates a private key. */
func generatePrivateKey() *rsa.PrivateKey {
secret, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
fmt.Println(err)
}
return secret
}
/* Makes Xena-Atila aware of its existence. */
func identify(id string, identificationKey string) {
insertionPayload := []byte(`{"id":"` + id + `","identificationKey":"` + identificationKey + `","status":"ALIVE"}`)
request, err := http.NewRequest("POST", centralizedHost+"/v1/clients", bytes.NewBuffer(insertionPayload))
request.Header.Set("Content-Type", "application/json")
if err != nil {
fmt.Println("Unable to connect to the centralized host.")
}
client := &http.Client{}
response, err := client.Do(request)
defer response.Body.Close()
}
node-jsonwebtoken 中的 jwt.sign()
和 jwt.verify()
方法需要 PEM 编码密钥。因此,NodeJS 端将提供 public 密钥 PEM 编码或期望 PEM 编码密钥(X.509/SPKI 格式或 PKCS#1 格式)。
密钥导出和导入在crypto/x509
包中使用Go实现,encoding/pem
包中使用PEM编码,crypto/rsa
包中使用RSA。
在 Go 中使用发布的 GeneratePrivateKey()
方法生成私钥和 public 密钥是:
privateKey := GeneratePrivateKey()
publicKey := &privateKey.PublicKey
可以在 Go 中以 X.509/SPKI 格式导出 PEM 编码的 public 密钥,例如有:
func ExportSPKIPublicKeyPEM(pubkey *rsa.PublicKey) (string){
spkiDER, _ := x509.MarshalPKIXPublicKey(pubkey)
spkiPEM := pem.EncodeToMemory(
&pem.Block{
Type: "PUBLIC KEY",
Bytes: spkiDER,
},
)
return string(spkiPEM)
}
或者,可以使用 MarshalPKCS1PublicKey()
导出 PKCS#1 格式的 PEM 编码 public 密钥。为此,必须指定 Type
RSA PUBLIC KEY
。
可以使用 ASN.1 解析器检查导出的密钥,例如在线:https://lapo.it/asn1js/
可以在 Go 中导入 X.509/SPKI 格式的 PEM 编码 public 密钥,例如有:
func ImportSPKIPublicKeyPEM(spkiPEM string) (*rsa.PublicKey) {
body, _ := pem.Decode([]byte(spkiPEM ))
publicKey, _ := x509.ParsePKIXPublicKey(body.Bytes)
if publicKey, ok := publicKey.(*rsa.PublicKey); ok {
return publicKey
} else {
return nil
}
}
使用 ParsePKCS1PublicKey()
可以导入 PKCS#1 格式的 PEM 编码 public 密钥。