dongtuo4723
2016-09-09 11:42
浏览 452
已采纳

如何将接口{}转换为* rsa.PublicKey golang类型

I have public key stored in a variable of type interface{}

-----BEGIN PUBLIC KEY-----    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdlatRjRjogo3WojgGHFHYLugdUWAY9iR3fy4arWNA1KoS8kVw33cJibXr8bvwUAUparCwlvdbH6dvEOfou0/gCFQsHUfQrSDv+MuSUMAe8jzKE4qW+jK+xQU9a03GUnKHkkle+Q0pX/g6jXZ7r1/xAK5Do2kQ+X5xK9cipRgEKwIDAQAB
-----END PUBLIC KEY-----

Is there anyway I can convert this into type *rsa.PublicKey in golang. It might involve some intermediate step like converting interface{} to string first may be.

图片转代码服务由CSDN问答提供 功能建议

我将公共密钥存储在接口类型的变量中{}

  <代码> ----- BEGIN PUBLIC KEY ----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdlatRjRjogo3WojgGHFHYLugdUWAY9iR3fy4arWNA1KoS8kVw33cJibXr8bvwUAUparCwlvdbH6dvEOfou0 / gCFQsHUfQrSDv + MuSUMAe8jzKE4qW + JK + xQU9a03GUnKHkkle + Q0pX / g6jXZ7r1 / xAK5Do2kQ + X5xK9cipRgEKwIDAQAB 
 ----- END PUBLIC KEY ----- 
    
 
 

无论如何,我都可以将其转换为golang中的* rsa.PublicKey类型。 可能涉及一些中间步骤,例如可能先将interface {}转换为字符串。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongliao8069 2016-09-09 12:00
    已采纳

    In general you would decode a pem file (assuming this is in a x509) cert using the crypto/x509 library. The provided example in the documentation is as follows:

    package main
    
    import (
        "crypto/dsa"
        "crypto/ecdsa"
        "crypto/rsa"
        "crypto/x509"
        "encoding/pem"
        "fmt"
    )
    
    func main() {
        const pubPEM = `
    -----BEGIN PUBLIC KEY-----
    MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAlRuRnThUjU8/prwYxbty
    WPT9pURI3lbsKMiB6Fn/VHOKE13p4D8xgOCADpdRagdT6n4etr9atzDKUSvpMtR3
    CP5noNc97WiNCggBjVWhs7szEe8ugyqF23XwpHQ6uV1LKH50m92MbOWfCtjU9p/x
    qhNpQQ1AZhqNy5Gevap5k8XzRmjSldNAFZMY7Yv3Gi+nyCwGwpVtBUwhuLzgNFK/
    yDtw2WcWmUU7NuC8Q6MWvPebxVtCfVp/iQU6q60yyt6aGOBkhAX0LpKAEhKidixY
    nP9PNVBvxgu3XZ4P36gZV6+ummKdBVnc3NqwBLu5+CcdRdusmHPHd5pHf4/38Z3/
    6qU2a/fPvWzceVTEgZ47QjFMTCTmCwNt29cvi7zZeQzjtwQgn4ipN9NibRH/Ax/q
    TbIzHfrJ1xa2RteWSdFjwtxi9C20HUkjXSeI4YlzQMH0fPX6KCE7aVePTOnB69I/
    a9/q96DiXZajwlpq3wFctrs1oXqBp5DVrCIj8hU2wNgB7LtQ1mCtsYz//heai0K9
    PhE4X6hiE0YmeAZjR0uHl8M/5aW9xCoJ72+12kKpWAa0SFRWLy6FejNYCYpkupVJ
    yecLk/4L1W0l6jQQZnWErXZYe0PNFcmwGXy1Rep83kfBRNKRy5tvocalLlwXLdUk
    AIU+2GKjyT3iMuzZxxFxPFMCAwEAAQ==
    -----END PUBLIC KEY-----`
    
        block, _ := pem.Decode([]byte(pubPEM))
        if block == nil {
            panic("failed to parse PEM block containing the public key")
        }
    
        pub, err := x509.ParsePKIXPublicKey(block.Bytes)
        if err != nil {
            panic("failed to parse DER encoded public key: " + err.Error())
        }
    
        switch pub := pub.(type) {
        case *rsa.PublicKey:
            fmt.Println("pub is of type RSA:", pub)
        case *dsa.PublicKey:
            fmt.Println("pub is of type DSA:", pub)
        case *ecdsa.PublicKey:
            fmt.Println("pub is of type ECDSA:", pub)
        default:
            panic("unknown type of public key")
        }
    }
    

    I'm not really sure about the encoding you have there though. The following might help you past that hurdle:

    已采纳该答案
    打赏 评论
  • dongrang2140 2017-06-01 21:19

    you can use: rsaPublickey, _ := pub.(*rsa.PublicKey) to convert to rsa.PublicKey

    打赏 评论

相关推荐 更多相似问题