rsa-asymmetric-cryptography-with-digital-signature example
Lets verify who the message is from.
Create and verify a digital signature using the
crypto/rsa
standard package.
Refer to the
crypto/rsa
package for more info.
I added a digital signature to this example
rsa-asymmetric-cryptography.
GitHub Webpage
OVERVIEW
RSA is a cryptosystem for public-key encryption, and is used for
securing sensitive data over unsecured networks. It is also used to add
digital signatures for verification.
This illustration may help,
RUN
go run rsa-asymmetric-cryptography-with-digital-signature.go <FILENAME>
go run rsa-asymmetric-cryptography-with-digital-signature.go test.txt
HOW IT WORKS
Generate rsa keys,
// GENERATE PRIVATE & PUBLIC KEY PAIR
privateKeyRaw, err := rsa.GenerateKey(rand.Reader, 2048)
// EXTRACT PUBLIC KEY
publicKeyRaw := &privateKeyRaw.PublicKey
Create a digital signature,
// CREATE SIGNATURE
signatureByte, err := rsa.SignPSS(
rand.Reader,
senderPrivateKeyRaw,
newhash,
hashed,
&opts,
)
Verify the digital signature,
// VERIFY SIGNATURE
err := rsa.VerifyPSS(
senderPublicKeyRaw,
newhash,
hashed,
signatureByte,
&opts,
)