-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.go
43 lines (36 loc) · 1.07 KB
/
rsa.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package gopki
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
)
func Encrypt(publicKey *rsa.PublicKey, message string) ([]byte, error) {
secretMessage := []byte(message)
rng := rand.Reader
return rsa.EncryptOAEP(sha256.New(), rng, publicKey, secretMessage, nil)
}
func Decrypt(privateKey *rsa.PrivateKey, cipherMessage []byte) (string, error) {
rng := rand.Reader
plaintext, err := rsa.DecryptOAEP(sha256.New(), rng, privateKey, cipherMessage, nil)
return string(plaintext), err
}
func Signing(privateKey *rsa.PrivateKey, message string) ([]byte, error) {
msgHash := sha256.New()
_, err := msgHash.Write([]byte(message))
if err != nil {
return nil, err
}
hashed := msgHash.Sum(nil)
rng := rand.Reader
return rsa.SignPKCS1v15(rng, privateKey, crypto.SHA256, hashed)
}
func VerifySign(publicKey *rsa.PublicKey, message string, signature []byte) error {
msgHash := sha256.New()
_, err := msgHash.Write([]byte(message))
if err != nil {
return err
}
hashed := msgHash.Sum(nil)
return rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, hashed, signature)
}