Golang equivalent of Java's SHA256withRSA

Viewed 1383

Is there any Golang equivalent of Java's java.security.Signature's SHA256withRSA? A couple of research is that I could not simple calculate the SHA256 hash and then signing it with RSA.

1 Answers

Figured it out if someone stumbled on the same question, here's how it works in Go

func main() {
    privateKey := loadPrivateKey()
    h := sha256.New()
    h.Write([]byte(`your message`))
    d := h.Sum(nil)

    signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, d)
    if err != nil {
        panic(err)
    }

    fmt.Printf("Signature in byte: %v\n\n", signature)

    encodedSig := base64.StdEncoding.EncodeToString(signature)

    fmt.Printf("Encoded signature: %v\n\n", encodedSig)
}
Related