Created
July 10, 2013 05:10
-
-
Save jedy/5963633 to your computer and use it in GitHub Desktop.
use publib key to verify in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto" | |
"crypto/rsa" | |
"crypto/sha1" | |
"crypto/x509" | |
"encoding/base64" | |
"fmt" | |
) | |
func main() { | |
k := `MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDXX/MsKEBLcLeKA1d/i7ufG1qsqS97xFkIRSeX3TwmHic843AfVrzoh2pZUeOvK9ZLZQpHSM7DoHMYDGD1273+FvZXYpf5LiFtecfxko/Cku16zy6WAeCYVFjjlveBhwPmPCIk+qDRYeiIW05QE2XK+CuDnJ7sxxXIJSSgD3Jo5wIDAQAB` | |
key, _ := base64.StdEncoding.DecodeString(k) | |
re, err := x509.ParsePKIXPublicKey(key) | |
pub := re.(*rsa.PublicKey) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
text := []byte("12999763169054705758.1350895718127996") | |
sig := "e9PE8gfsX9WJQNTojeOJVFAOUbWbAxKZ6LxP8B200HithDFA9dc8FMMny5F4Lv7Kear2juuKTcvqFFqUvu0Yyk2R5g7wlDqXjePvi4YRH2b4FyA876OiNGVFQOaV8ErRtoX1uW8XaZM+tk0zYdsdYOC+UZOsqGC/zUfufJ02/Fe9vfFKWFAwnu4A5n0BM7hPv9fl+E+aQc9KJzdUjMPHHhe3pIkJLwahnCdrt3y+cKwuNMrKwb5NbaI3CWHDr6rWVKnG1PbkT98Z7alVL48kclo0RcV8VUvEtj2qm+6Lbcy0eEEfPNkzwIVg70bHTZ0GWhVYVBEVlt3vny66emKPpw==" | |
h := sha1.New() | |
h.Write(text) | |
digest := h.Sum(nil) | |
ds, _ := base64.StdEncoding.DecodeString(sig) | |
err = rsa.VerifyPKCS1v15(pub, crypto.SHA1, digest, ds) | |
fmt.Println("verify:", err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code-example is failing ( run it on https://play.golang.org/p/pq2Oghp9Eo4). Is it supposed to do that?
I created a sign/verify example here if anyone else is looking for it: https://gist.github.com/nilsmagnus/8cb1ff93b56f73126f65e22840cbc32b