Last active
April 27, 2018 16:42
-
-
Save nasermirzaei89/4b14c6fdf258a569c62ea8a172bde624 to your computer and use it in GitHub Desktop.
RSA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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