-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.go
More file actions
65 lines (49 loc) · 1.46 KB
/
rsa.go
File metadata and controls
65 lines (49 loc) · 1.46 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2024 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/FishGoddess/cryptox/hash"
"github.com/FishGoddess/cryptox/rsa"
)
func main() {
// Load the private key and the public key from file.
// Check rsa.Option for more information about file encoding.
privateKey, err := rsa.LoadPrivateKey("rsa.key")
if err != nil {
panic(err)
}
publicKey, err := rsa.LoadPublicKey("rsa.pub")
if err != nil {
panic(err)
}
data := []byte("戴上头箍,爱不了你;不戴头箍,救不了你。")
fmt.Printf("data: %s\n", data)
// Use the public key to encrypt data using base64 encoding.
label := []byte("你好,世界")
encrypt, err := publicKey.EncryptOAEP(data, label, rsa.WithBase64())
if err != nil {
panic(err)
}
fmt.Printf("encrypt: %s\n", encrypt)
// Use the private key to decrypt data using base64 encoding.
decrypt, err := privateKey.DecryptOAEP(encrypt, label, rsa.WithBase64())
if err != nil {
panic(err)
}
fmt.Printf("decrypt: %s\n", decrypt)
// Use the private key to sign data.
digest := hash.SHA256(data)
sign, err := privateKey.SignPSS(digest, rsa.WithHex())
if err != nil {
panic(err)
}
fmt.Printf("sign: %s\n", sign)
// Use the public key to verify the sign.
err = publicKey.VerifyPSS(digest, sign, rsa.WithHex())
if err != nil {
panic(err)
}
fmt.Printf("verify: %s\n", data)
}