Skip to content

Instantly share code, notes, and snippets.

@nasermirzaei89
Last active April 27, 2018 16:42
Show Gist options
  • Save nasermirzaei89/4b14c6fdf258a569c62ea8a172bde624 to your computer and use it in GitHub Desktop.
Save nasermirzaei89/4b14c6fdf258a569c62ea8a172bde624 to your computer and use it in GitHub Desktop.
RSA
package main
import (
"crypto/rand"
"crypto/rsa"
"log"
)
func main() {
rnd := rand.Reader
msg := []byte("Hello World")
key, err := rsa.GenerateKey(rnd, 2048)
if err != nil {
log.Panicln(err)
}
enc, err := rsa.EncryptPKCS1v15(rnd, &key.PublicKey, msg)
if err != nil {
log.Panicln(err)
}
dec, err := rsa.DecryptPKCS1v15(rnd, key, enc)
if err != nil {
log.Panicln(err)
}
log.Println(string(dec) == string(msg))
}
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"log"
)
func main() {
rnd := rand.Reader
msg := []byte("Hello World")
key, err := rsa.GenerateKey(rnd, 2048)
if err != nil {
log.Panicln(err)
}
hashed := sha256.Sum256(msg)
sig, err := rsa.SignPKCS1v15(rnd, key, crypto.SHA256, hashed[:])
if err != nil {
log.Panicln(err)
}
err = rsa.VerifyPKCS1v15(&key.PublicKey, crypto.SHA256, hashed[:], sig)
if err != nil {
log.Panicln(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment