-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa_key.go
64 lines (50 loc) · 1.5 KB
/
rsa_key.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// 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/rsa"
)
func main() {
// Generate a 4096 bits key.
// Check rsa.KeyOption for more information of encoder and decoder.
privateKey, publicKey, err := rsa.GenerateKeys(4096)
if err != nil {
panic(err)
}
fmt.Println(privateKey)
fmt.Println(publicKey)
// Try WriteToFile if you want to write your private key to file.
n, err := privateKey.Bytes().WriteToFile("rsa.key")
if err != nil {
panic(err)
}
fmt.Printf("Write %d bytes to private key file\n", n)
// Try WriteToFile if you want to write your public key to file.
n, err = publicKey.Bytes().WriteToFile("rsa.pub")
if err != nil {
panic(err)
}
fmt.Printf("Write %d bytes to public key file\n", n)
// Load private key from file.
loadedPrivateKey, err := rsa.LoadPrivateKey("rsa.key")
if err != nil {
panic(err)
}
// Load public key from file.
loadedPublicKey, err := rsa.LoadPublicKey("rsa.pub")
if err != nil {
panic(err)
}
fmt.Println(loadedPrivateKey)
fmt.Println(loadedPublicKey)
// Want to load keys from file and panic if failed?
// Try these:
loadedPrivateKey = rsa.MustLoadPrivateKey("rsa.key")
loadedPublicKey = rsa.MustLoadPublicKey("rsa.pub")
// Already have a private or public key in bytes?
// Try these:
_, _ = rsa.ParsePrivateKey(privateKey.Bytes())
_, _ = rsa.ParsePublicKey(publicKey.Bytes())
}