Newer
Older
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
44
45
46
47
48
49
50
51
52
53
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"fmt"
"os"
)
func main() {
// Generate Alice RSA keys Of 2048 Buts
alicePrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
fmt.Println(err.Error)
os.Exit(1)
}
// Extract Public Key from RSA Private Key
alicePublicKey := alicePrivateKey.PublicKey
secretMessage := "IHsKICAgICAgInByb3ZpZGVyIjogewogICAgICAgICJuYW1lIjogInVjM20iLAogICAgICAgICJkb21haW4iOiAiaWRjby51YzNtLmVzIgogICAgICB9LAogICAgICAiYWNjZXNzTGlzdCI6IFsicHVibGljLWtleS0xIiwgInB1YmxpYy1rZXktMiJdCiAgICB9Cg"
fmt.Println("Original Text ", secretMessage)
signature := SignPKCS1v15(secretMessage, *alicePrivateKey)
fmt.Println("Singature : ", signature)
verif := VerifyPKCS1v15(signature, secretMessage, alicePublicKey)
fmt.Println(verif)
}
func SignPKCS1v15(plaintext string, privKey rsa.PrivateKey) string {
// crypto/rand.Reader is a good source of entropy for blinding the RSA
// operation.
rng := rand.Reader
hashed := sha256.Sum256([]byte(plaintext))
signature, err := rsa.SignPKCS1v15(rng, &privKey, crypto.SHA256, hashed[:])
if err != nil {
fmt.Fprintf(os.Stderr, "Error from signing: %s\n", err)
return "Error from signing"
}
return base64.StdEncoding.EncodeToString(signature)
}
func VerifyPKCS1v15(signature string, plaintext string, pubkey rsa.PublicKey) string {
sig, _ := base64.StdEncoding.DecodeString(signature)
hashed := sha256.Sum256([]byte(plaintext))
err := rsa.VerifyPKCS1v15(&pubkey, crypto.SHA256, hashed[:], sig)
if err != nil {
fmt.Fprintf(os.Stderr, "Error from verification: %s\n", err)
return "Error from verification:"
}
return "Signature Verification Passed"
}