我正在尝试使用go的crypto/x509包解析ecdsa secp384r1曲线键,代码如下( https://play.golang.org/p/opFLBMaKhDv ):
import ( "crypto/x509" "encoding/pem" "fmt" ) var b = `-----BEGIN EC PRIVATE KEY----- MIGkAgEBBDBsee4GAKz9Jo603xmGZ0uYEbJAoUgNqCYjDiLfj6zG4fvVSiCVxoTx rVcvW2lmVcmgBwYFK4EEACKhZANiAATm6yBej3NVXnXAydMdLvrIB0PMr/fT6VCD MB2pXzqxbQs8tYt3Rqd0HnzAZyYb1KhOX5lG0MyBDohhPRXqWE3gMhEq47BdzC7G gEftttcFKXX/PSxsZUDL6GiliaB0/9E= -----END EC PRIVATE KEY-----` func main() { blk, _ := pem.Decode([]byte(b)) _, err := x509.ParseECPrivateKey(blk.Bytes) fmt.Println((err.Error())) }
但在接下来的恐慌中
panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x10e9366]
有人能告诉我我做错了什么吗?
添加错误检查。
func main() { blk, _ := pem.Decode([]byte(b)) key, err := x509.ParseECPrivateKey(blk.Bytes) if err != nil { fmt.Println((err.Error())) } fmt.Println(key) }