Source file
src/crypto/rsa/example_test.go
1
2
3
4
5 package rsa
6
7 import (
8 "crypto"
9 "crypto/aes"
10 "crypto/cipher"
11 "crypto/rand"
12 "crypto/sha256"
13 "encoding/hex"
14 "fmt"
15 "io"
16 "os"
17 )
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 func ExampleDecryptPKCS1v15SessionKey() {
39
40
41 rng := rand.Reader
42
43
44
45
46 key := make([]byte, 32)
47 if _, err := io.ReadFull(rng, key); err != nil {
48 panic("RNG failure")
49 }
50
51 rsaCiphertext, _ := hex.DecodeString("aabbccddeeff")
52
53 if err := DecryptPKCS1v15SessionKey(rng, rsaPrivateKey, rsaCiphertext, key); err != nil {
54
55
56
57
58 fmt.Fprintf(os.Stderr, "Error from RSA decryption: %s\n", err)
59 return
60 }
61
62
63
64 block, err := aes.NewCipher(key)
65 if err != nil {
66 panic("aes.NewCipher failed: " + err.Error())
67 }
68
69
70
71 var zeroNonce [12]byte
72 aead, err := cipher.NewGCM(block)
73 if err != nil {
74 panic("cipher.NewGCM failed: " + err.Error())
75 }
76 ciphertext, _ := hex.DecodeString("00112233445566")
77 plaintext, err := aead.Open(nil, zeroNonce[:], ciphertext, nil)
78 if err != nil {
79
80
81 fmt.Fprintf(os.Stderr, "Error decrypting: %s\n", err)
82 return
83 }
84
85 fmt.Printf("Plaintext: %s\n", string(plaintext))
86 }
87
88 func ExampleSignPKCS1v15() {
89
90
91 rng := rand.Reader
92
93 message := []byte("message to be signed")
94
95
96
97
98
99
100 hashed := sha256.Sum256(message)
101
102 signature, err := SignPKCS1v15(rng, rsaPrivateKey, crypto.SHA256, hashed[:])
103 if err != nil {
104 fmt.Fprintf(os.Stderr, "Error from signing: %s\n", err)
105 return
106 }
107
108 fmt.Printf("Signature: %x\n", signature)
109 }
110
111 func ExampleVerifyPKCS1v15() {
112 message := []byte("message to be signed")
113 signature, _ := hex.DecodeString("ad2766728615cc7a746cc553916380ca7bfa4f8983b990913bc69eb0556539a350ff0f8fe65ddfd3ebe91fe1c299c2fac135bc8c61e26be44ee259f2f80c1530")
114
115
116
117
118
119
120 hashed := sha256.Sum256(message)
121
122 err := VerifyPKCS1v15(&rsaPrivateKey.PublicKey, crypto.SHA256, hashed[:], signature)
123 if err != nil {
124 fmt.Fprintf(os.Stderr, "Error from verification: %s\n", err)
125 return
126 }
127
128
129 }
130
131 func ExampleEncryptOAEP() {
132 secretMessage := []byte("send reinforcements, we're going to advance")
133 label := []byte("orders")
134
135
136
137 rng := rand.Reader
138
139 ciphertext, err := EncryptOAEP(sha256.New(), rng, &test2048Key.PublicKey, secretMessage, label)
140 if err != nil {
141 fmt.Fprintf(os.Stderr, "Error from encryption: %s\n", err)
142 return
143 }
144
145
146
147 fmt.Printf("Ciphertext: %x\n", ciphertext)
148 }
149
150 func ExampleDecryptOAEP() {
151 ciphertext, _ := hex.DecodeString("4d1ee10e8f286390258c51a5e80802844c3e6358ad6690b7285218a7c7ed7fc3a4c7b950fbd04d4b0239cc060dcc7065ca6f84c1756deb71ca5685cadbb82be025e16449b905c568a19c088a1abfad54bf7ecc67a7df39943ec511091a34c0f2348d04e058fcff4d55644de3cd1d580791d4524b92f3e91695582e6e340a1c50b6c6d78e80b4e42c5b4d45e479b492de42bbd39cc642ebb80226bb5200020d501b24a37bcc2ec7f34e596b4fd6b063de4858dbf5a4e3dd18e262eda0ec2d19dbd8e890d672b63d368768360b20c0b6b8592a438fa275e5fa7f60bef0dd39673fd3989cc54d2cb80c08fcd19dacbc265ee1c6014616b0e04ea0328c2a04e73460")
152 label := []byte("orders")
153
154
155
156 rng := rand.Reader
157
158 plaintext, err := DecryptOAEP(sha256.New(), rng, test2048Key, ciphertext, label)
159 if err != nil {
160 fmt.Fprintf(os.Stderr, "Error from decryption: %s\n", err)
161 return
162 }
163
164 fmt.Printf("Plaintext: %s\n", string(plaintext))
165
166
167
168
169 }
170
View as plain text