-
Notifications
You must be signed in to change notification settings - Fork 9
/
generate.go
74 lines (65 loc) · 1.58 KB
/
generate.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
package client
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rsa"
"fmt"
"io"
)
// KeyType is an enum type to allow for the description of a specific Key
// algorithm.
type KeyType uint8
const (
// KeyTypeECDSAP256 is ECDSA P-256
KeyTypeECDSAP256 KeyType = iota
// KeyTypeECDSAP384 is ECDSA P-384
KeyTypeECDSAP384
// KeyTypeECDSAP521 is ECDSA P-521
KeyTypeECDSAP521
// KeyTypeED25519 is Ed25519
KeyTypeED25519
// KeyTypeRSA2048 is RSA with 2048 bits.
KeyTypeRSA2048
// KeyTypeRSA4096 is RSA with 4096 bits.
KeyTypeRSA4096
)
// Generate a key of the given Key Type.
func generateKey(rand io.Reader, keyType KeyType) (crypto.Signer, crypto.PublicKey, error) {
switch keyType {
case KeyTypeECDSAP256, KeyTypeECDSAP384, KeyTypeECDSAP521:
var curve elliptic.Curve
switch keyType {
case KeyTypeECDSAP256:
curve = elliptic.P256()
case KeyTypeECDSAP384:
curve = elliptic.P384()
case KeyTypeECDSAP521:
curve = elliptic.P521()
}
privKey, err := ecdsa.GenerateKey(curve, rand)
if err != nil {
return nil, nil, err
}
return privKey, privKey.Public(), nil
case KeyTypeRSA2048, KeyTypeRSA4096:
var size int = 0
switch keyType {
case KeyTypeRSA2048:
size = 2048
case KeyTypeRSA4096:
size = 4096
}
privKey, err := rsa.GenerateKey(rand, size)
if err != nil {
return nil, nil, err
}
return privKey, privKey.Public(), nil
case KeyTypeED25519:
pubKey, privKey, err := ed25519.GenerateKey(rand)
return privKey, pubKey, err
default:
return nil, nil, fmt.Errorf("hallow/client: unknown key type: %x", keyType)
}
}