-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
149 lines (117 loc) · 3.68 KB
/
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2023 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 rsa
import (
"crypto/rand"
"crypto/rsa"
"io"
"io/ioutil"
"github.com/FishGoddess/cryptox"
)
var (
// Primes is the prime of rsa key.
// See rsa.GenerateMultiPrimeKey.
Primes = 2
)
// GenerateKeys generates a key set of bits.
func GenerateKeys(bits int, opts ...KeyOption) (PrivateKey, PublicKey, error) {
privateKey, err := GeneratePrivateKey(bits, opts...)
if err != nil {
return PrivateKey{}, PublicKey{}, err
}
publicKey, err := GeneratePublicKey(privateKey, opts...)
if err != nil {
return PrivateKey{}, PublicKey{}, err
}
return privateKey, publicKey, nil
}
// GeneratePrivateKey generates a private key of bits.
func GeneratePrivateKey(bits int, opts ...KeyOption) (PrivateKey, error) {
cfg := fromKeyOptions(opts)
privateKey, err := rsa.GenerateMultiPrimeKey(rand.Reader, Primes, bits)
if err != nil {
return PrivateKey{}, err
}
privateKeyBytes, err := cfg.privateKeyEncoder.Encode(privateKey)
if err != nil {
return PrivateKey{}, err
}
return newPrivateKey(privateKey, privateKeyBytes), nil
}
// GeneratePublicKey generates a public key from private key.
func GeneratePublicKey(privateKey PrivateKey, opts ...KeyOption) (PublicKey, error) {
cfg := fromKeyOptions(opts)
publicKey := &(privateKey.Key().PublicKey)
publicKeyBytes, err := cfg.publicKeyEncoder.Encode(publicKey)
if err != nil {
return PublicKey{}, err
}
return newPublicKey(publicKey, publicKeyBytes), nil
}
// ParsePrivateKey parses private key from pem bytes.
func ParsePrivateKey(keyBytes cryptox.Bytes, opts ...KeyOption) (PrivateKey, error) {
cfg := fromKeyOptions(opts)
privateKey, err := cfg.privateKeyDecoder.Decode(keyBytes)
if err != nil {
return PrivateKey{}, err
}
return newPrivateKey(privateKey, keyBytes), nil
}
// ParsePublicKey parses public key from pem bytes.
func ParsePublicKey(keyBytes cryptox.Bytes, opts ...KeyOption) (PublicKey, error) {
cfg := fromKeyOptions(opts)
publicKey, err := cfg.publicKeyDecoder.Decode(keyBytes)
if err != nil {
return PublicKey{}, err
}
return newPublicKey(publicKey, keyBytes), nil
}
// ReadPrivateKey reads private key from a reader.
func ReadPrivateKey(keyReader io.Reader, opts ...KeyOption) (PrivateKey, error) {
keyBytes, err := ioutil.ReadAll(keyReader)
if err != nil {
return PrivateKey{}, err
}
return ParsePrivateKey(keyBytes, opts...)
}
// ReadPublicKey reads public key from a reader.
func ReadPublicKey(keyReader io.Reader, opts ...KeyOption) (PublicKey, error) {
keyBytes, err := ioutil.ReadAll(keyReader)
if err != nil {
return PublicKey{}, err
}
return ParsePublicKey(keyBytes, opts...)
}
// LoadPrivateKey loads private key from a file.
func LoadPrivateKey(keyFile string, opts ...KeyOption) (PrivateKey, error) {
keyBytes, err := ioutil.ReadFile(keyFile)
if err != nil {
return PrivateKey{}, err
}
return ParsePrivateKey(keyBytes, opts...)
}
// LoadPublicKey loads public key from a file.
func LoadPublicKey(keyFile string, opts ...KeyOption) (PublicKey, error) {
keyBytes, err := ioutil.ReadFile(keyFile)
if err != nil {
return PublicKey{}, err
}
return ParsePublicKey(keyBytes, opts...)
}
// MustLoadPrivateKey loads private key from a file or panic on failed.
func MustLoadPrivateKey(keyFile string, opts ...KeyOption) PrivateKey {
key, err := LoadPrivateKey(keyFile, opts...)
if err != nil {
panic(err)
}
return key
}
// MustLoadPublicKey loads public key from a file or panic on failed.
func MustLoadPublicKey(keyFile string, opts ...KeyOption) PublicKey {
key, err := LoadPublicKey(keyFile, opts...)
if err != nil {
panic(err)
}
return key
}