Signing and Verification:
RSA works by generating form of key pair of private and public keys.
For Signing:
we need to provide some inputs,
- A random reader used for generating random bits because if we provide the same input, it doesn't give the same output as last time.
- Before signing, we need to hash our message.
- we also need to provide which hash function is used for message hashing.
- Finally, private key.
For Verifying:
we need to provide some inputs,
- hash of our message.
- which hash function is used for message hashing while signing.
- Finally, public key and signature what we obtained while signing.
Example:
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
)
func main() {
privatekey, publickey := GenerateRsaKeys()
message := "hello"
hashedMessage := sha256.Sum256([]byte(message))
signedBytes, err := rsa.SignPKCS1v15(rand.Reader, privatekey, crypto.SHA256, hashedMessage[:])
if err != nil {
fmt.Println("signing error using RSA keys")
log.Fatal(err)
}
err = rsa.VerifyPKCS1v15(publickey, crypto.SHA256, hashedMessage[:], signedBytes)
if err != nil {
fmt.Println("signing error using RSA keys")
log.Fatal(err)
}
fmt.Println("Message verified Successfully")
}
func GenerateRsaKeys() (*rsa.PrivateKey, *rsa.PublicKey) {
privatekey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
fmt.Println("cannot generate RSA keys")
log.Fatal(err)
}
publicKey := &privatekey.PublicKey
return privatekey, publicKey
}
Output:
Message verified Successfully


Comments
Post a Comment