0

我得到了以下go-program:

package main

import (
    "fmt"
    "crypto/elliptic"
    "crypto/ecdsa"
    "crypto/sha512"
    "crypto/rand"
    "encoding/asn1"
)

func main() {
    message := []byte("TollJuhuWurstPizzaSchnellEssen")
    hash := sha512.Sum512(message)

//secp256r1
secp_priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
    panic(err)
}

r, s , err := ecdsa.Sign(rand.Reader, secp_priv, hash[:])
if err != nil {
    panic(err)
}

secpub := elliptic.Marshal(elliptic.P256(), secp_priv.PublicKey.X, secp_priv.PublicKey.Y)
fmt.Printf("secp256r1 pub: %#v\n", secpub)

secsig := elliptic.Marshal(elliptic.P256(), r, s)
fmt.Printf("secp256r1 sig %d: %#v\n",len(secsig), secsig)

x, y := elliptic.Unmarshal(elliptic.P256(), secpub)
secp_priv.PublicKey.X = x
secp_priv.PublicKey.Y = y

r2, s2 := elliptic.Unmarshal(elliptic.P256(), secsig[:])
fmt.Printf("r,s: %d %d\n", r, s)
fmt.Printf("r2,s2: %d %d\n", r2, s2)

valid := ecdsa.Verify(&secp_priv.PublicKey, hash[:], r, s)   //&secp_priv.PublicKey

fmt.Printf("secp256r1 verify1: %v\n", valid)

//SignASN1
asn1_sig , err := ecdsa.SignASN1(rand.Reader, secp_priv, hash[:])
if err != nil {
    panic(err)
}

fmt.Printf("secp256r1 sig_asn1 %d: %#v\n",len(asn1_sig), asn1_sig)
var buffer asn1.RawValue
_, err = asn1.Unmarshal(asn1_sig, &buffer)
if err != nil {
    panic(err)
}


fmt.Printf("secp256r1 unmarshal full sig_asn1 %d: %#v\n",len(buffer.FullBytes), buffer.FullBytes)
fmt.Printf("secp256r1 unmarshal sig_asn1 %d: %#v\n",len(buffer.Bytes), buffer.Bytes)

valid = ecdsa.VerifyASN1(&secp_priv.PublicKey, hash[:], asn1_sig)   //&secp_priv.PublicKey

fmt.Printf("secp256r1 verify1: %v\n", valid)



}

我想知道,东西是如何连接的。有一个 asn1 编码是什么意思。当我解组它时,我有 68 字节的切片。但是当我解组 r, s big.Int 值时,我得到了 65 字节(64 字节,前缀字节为 0x04)。我也无法理解为什么 r2 和 s2 变为 nil。我可以解组但不能编组签名...操作是可逆的吗?

我想为这个库提供签名和公钥: https ://github.com/kmackay/micro-ecc/tree/static

但我不知道,要采取什么字节片:(

4

0 回答 0