Home > Backend Development > Golang > I'm trying to create a jwt token using ES256 algorithm but I can't parse my private key

I'm trying to create a jwt token using ES256 algorithm but I can't parse my private key

WBOY
Release: 2024-02-10 15:18:09
forward
772 people have browsed it

我正在尝试使用 ES256 算法创建 jwt 令牌,但我无法解析我的私钥

php editor Strawberry is trying to create a jwt token using the ES256 algorithm, but encounters a problem parsing the private key. This is a common problem and may be caused by the private key being incorrect or the format not meeting the requirements. To solve this problem, you first need to ensure that the private key is correct and meets the requirements of the ES256 algorithm. If the private key was obtained from elsewhere, you can try to regenerate a new private key. In addition, make sure the private key is in the correct format. You can use the openssl command line tool to verify the private key format. If the private key is in the correct format but still cannot be parsed, it may be due to other reasons, such as permission issues or environment configuration issues. Before solving this problem, you can first check the relevant permissions and configuration to make sure there are no omissions or errors.

Question content

This is my golang code trying to create a JWT token

package main

import (
    "encoding/pem"
    "fmt"
    "time"

    "github.com/dgrijalva/jwt-go"
)

func main() {
    // Sample PEM-encoded private key
    pemKey := `-----BEGIN EC PRIVATE KEY-----MHcCAQEEIAh5qA3rmqQQuu0vbKV/+zouz/y/Iy2pLpIcWUSyImSwoAoGCCqGSM49AwEHoUQDQgAEYD54V/vp+54P9DXarYqx4MPcm+HKRIQzNasYSoRQHQ/6S6Ps8tpMcT+KvIIC8W/e9k0W7Cm72M1P9jU7SLf/vg==-----END EC PRIVATE KEY-----`

    // Convert the PEM-encoded private key to a byte slice
    pemBytes, _ := pem.Decode([]byte(pemKey))
    // // Parse and decode the private key
    key, err := jwt.ParseECPrivateKeyFromPEM(pemBytes.Bytes)
    if err != nil {
        fmt.Println("Error parsing private key:", err)
        return
    }

    uAccessToken := jwt.NewWithClaims(jwt.SigningMethodES256, jwt.MapClaims{
        "iss":         "issuer",
        "sub":         "access token",
        "exp":         time.Now().Add(time.Minute * 20).Unix(),
    })

    AccessToken, err := uAccessToken.SignedString(key); 
    if err != nil {
        fmt.Println("Error signing token:", err)
        return
    }

    // // You now have the ECDSA private key available in the 'key' variable

    fmt.Println("ECDSA Private Key:", AccessToken)
}
Copy after login

But every time I try to create a token I get this error:

<code>
Error parsing private key: Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key
</code>
Copy after login

Can anyone suggest what's going wrong here?

I'm trying to create a JWT using the ES256 algorithm where I need the private key. But somehow I got the error.

Solution

Requires two fixes

1 - Comment/Remove pem.Decode() It requires the raw pem key in bytes.

2-Key string in single line change key string

Check the solution here

The above is the detailed content of I'm trying to create a jwt token using ES256 algorithm but I can't parse my private key. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template