doudanma9706 2018-07-23 05:00 采纳率: 100%
浏览 630
已采纳

如何始终通过无效的密钥类型错误生成JWT令牌

I met this issue and really do not know how to resolve it, can anyone help to provide a working solution?

func GenerateJWT(name, role string) (string, error)  {
    //create a singner for rsa 256
    claims := &jwt.StandardClaims{
        ExpiresAt: 15000,
        Issuer:    "test",
    }

    token :=jwt.NewWithClaims(jwt.SigningMethodES256, claims)
    log.Println("generated toke is ")
    log.Println(token)
    tokenString, err := token.SignedString([]byte("secret"))
....
}

Now I am alway having:

key is of invalid type

error. I google a lot, and even for jwt-go library it self, they are providing exactly same solution, but why I kept having the

key is of invalid type

error?

Can anyone help to provide a working sample about how to generate jwt token in go?

  • 写回答

1条回答 默认 最新

  • douyi02577 2018-07-23 05:45
    关注

    From the README:

    The ECDSA signing method (ES256,ES384,ES512) expect *ecdsa.PrivateKey for signing and *ecdsa.PublicKey for validation

    So use an elliptic curve key:

    package main
    
    import (
            "crypto/ecdsa"
            "crypto/elliptic"
            "crypto/rand"
            "log"
    
            jwt "github.com/dgrijalva/jwt-go"
    )
    
    func main() {
            key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
            if err != nil {
                    log.Fatal(err)
            }
    
            claims := &jwt.StandardClaims{
                    ExpiresAt: 15000,
                    Issuer:    "test",
            }
    
            token := jwt.NewWithClaims(jwt.SigningMethodES256, claims)
    
            tokenString, err := token.SignedString(key)
            if err != nil {
                    log.Fatal(err)
            }
    
            log.Println(tokenString)
    }
    

    To store the generated key for later use with jwt.ParseECPrivateKeyFromPEM and jwt.ParseECPublicKeyFromPEM:

    import (
            "crypto/ecdsa"
            "crypto/x509"
            "encoding/pem"
    )
    
    func pemKeyPair(key *ecdsa.PrivateKey) (privKeyPEM []byte, pubKeyPEM []byte, err error) {
            der, err := x509.MarshalECPrivateKey(key)
            if err != nil {
                    return nil, nil, err
            }
    
            privKeyPEM = pem.EncodeToMemory(&pem.Block{
                    Type:  "EC PRIVATE KEY",
                    Bytes: der,
            })
    
            der, err = x509.MarshalPKIXPublicKey(key.Public())
            if err != nil {
                    return nil, nil, err
            }
    
            pubKeyPEM = pem.EncodeToMemory(&pem.Block{
                    Type:  "EC PUBLIC KEY",
                    Bytes: der,
            })
    
            return
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用这个函数清除字符串空格为什么会出现问题?
  • ¥30 智能车串级pid调参
  • ¥15 visual studio code翻译老是错误
  • ¥20 卫星测高数据的高程转换
  • ¥20 PHP+javascript文本框之间字符串逻辑判断
  • ¥15 爬取招聘网站数据信息
  • ¥15 安装完tensorflow,import tensorflow as tf后报错,如何解决?
  • ¥15 ultralytics库导出onnx模型,模型失去预测能力
  • ¥15 linux下点对点协议连接2个USB串口的硬件流量控制问题
  • ¥15 SQL数据自动生成问题