package main import ( "crypto" "crypto/rsa" "crypto/sha512" "crypto/x509" "encoding/hex" "encoding/pem" "errors" "fmt" "log" ) func init() { k, err := getKey() if err != nil { log.Fatal(err) } publicKey = k } func compareBytesHashSum(hash string, bytes *[]byte) (bool, error) { hdc, err := hex.DecodeString(hash) if err != nil { fmt.Println(err) return false, nil } var ha [48]byte copy(ha[:], hdc) // convert slice to array for hash checking hfb := sha512.Sum384(*bytes) if hfb == ha { return true, nil } return false, nil } func base16ToHash(hash string) (*[48]byte, error) { hdc, err := hex.DecodeString(hash) if err != nil { fmt.Println(err) return nil, err } var ha [48]byte copy(ha[:], hdc) // convert slice to array return &ha, nil } func verifySig(sig *[]byte, bytes *[]byte) bool { hash := sha512.Sum384(*bytes) err := rsa.VerifyPKCS1v15(publicKey, crypto.SHA384, hash[:], *sig) if err != nil { fmt.Println(err) return false } return true } func parseKey(b *[]byte) (*rsa.PublicKey, error) { pp /* heh */, _ := pem.Decode(*b) if pp == nil { return nil, errors.New("invalid key") } if pp.Type != "PUBLIC KEY" { return nil, errors.New("not a proper public key") } pk, err := x509.ParsePKIXPublicKey(pp.Bytes) if err != nil { return nil, errors.New("failed to parse key") } var pkt *rsa.PublicKey var ok bool if pkt, ok = pk.(*rsa.PublicKey); !ok { return nil, err } return pkt, nil }