-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeypair.go
68 lines (58 loc) · 1.47 KB
/
keypair.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
package secgen
import (
"crypto/ed25519"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"strings"
"github.com/mikesmitty/edkey"
"golang.org/x/crypto/ssh"
)
// RSABits used for DKIM keypair
const RSABits = 3072
// Keypair generates ed25519 keypair for ssh (public key, private key)
func Keypair() (string, string, error) {
publicBytes, privateBytes, err := ed25519.GenerateKey(nil)
if err != nil {
return "", "", err
}
publicStruct, err := ssh.NewPublicKey(publicBytes)
if err != nil {
return "", "", err
}
pemblock := &pem.Block{
Type: "OPENSSH PRIVATE KEY",
Bytes: edkey.MarshalED25519PrivateKey(privateBytes),
}
private := pem.EncodeToMemory(pemblock)
public := ssh.MarshalAuthorizedKey(publicStruct)
return string(public), string(private), nil
}
// DKIM generates DKIM signature and private key (TXT record, private key)
func DKIM() (string, string, error) {
privateBytes, err := rsa.GenerateKey(rand.Reader, RSABits)
if err != nil {
return "", "", err
}
publicBytes, err := x509.MarshalPKIXPublicKey(privateBytes.Public())
if err != nil {
return "", "", err
}
pkcs8, err := x509.MarshalPKCS8PrivateKey(privateBytes)
if err != nil {
return "", "", err
}
pemblock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: pkcs8,
}
private := pem.EncodeToMemory(pemblock)
dkim := []string{
"v=DKIM1",
"k=rsa",
"p=" + base64.StdEncoding.EncodeToString(publicBytes),
}
return strings.Join(dkim, "; "), string(private), nil
}