Skip to content

Instantly share code, notes, and snippets.

@ivoras
Created April 30, 2017 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivoras/32b2abd16b5984fa43c006486bfb7e2c to your computer and use it in GitHub Desktop.
Save ivoras/32b2abd16b5984fa43c006486bfb7e2c to your computer and use it in GitHub Desktop.
package main
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/asn1"
"encoding/hex"
"log"
"math/big"
)
type ecdsaSignature struct {
R *big.Int
S *big.Int
}
const signatureHex = "3046022100c71e88ee6dbd43370c2c982031846dd37a764d331e6eb769ef2675d38fa523f40221008db38909f2249279ac50dab8448ee178d46f6b1ef62c4e0c11c9a1ebeaa69966"
const pubKeyHex = "3059301306072a8648ce3d020106082a8648ce3d030107034200047c073a351d218d911ce28d750347388bd355e3dac5de1f93e446fa2f6824d8b54884ceeea81089495bebd6dddfb2023f717a34f065a6a97c83bf6238ec1d714c"
var hash = []byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
func main() {
signatureBytes, err := hex.DecodeString(signatureHex)
if err != nil {
log.Panicln(err)
}
pubKeyBytes, err := hex.DecodeString(pubKeyHex)
if err != nil {
log.Panicln(err)
}
ikey, err := x509.ParsePKIXPublicKey(pubKeyBytes)
if err != nil {
log.Panicln(err)
}
pubKey := ikey.(*ecdsa.PublicKey)
var sig ecdsaSignature
_, err = asn1.Unmarshal(signatureBytes, &sig)
if err != nil {
log.Panicln(err)
}
if ecdsa.Verify(pubKey, hash, sig.R, sig.S) {
log.Println("Verification succeded")
} else {
log.Println("Failed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment