-
Notifications
You must be signed in to change notification settings - Fork 2
/
dh.go
65 lines (49 loc) · 1.1 KB
/
dh.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
package openssl
import (
"io/ioutil"
"os/exec"
"strconv"
log "github.com/cihub/seelog"
)
type DH struct {
path string
content []byte
}
func (o *Openssl) LoadOrCreateDH(filename string, size int) (*DH, error) {
dh, err := o.LoadDH(filename)
if err != nil {
return o.CreateDH(filename, size)
}
return dh, nil
}
func (o *Openssl) LoadDH(filename string) (*DH, error) {
filename = o.Path + "/" + filename
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
dh := &DH{}
dh.path = filename
dh.content = content
return dh, nil
}
func (o *Openssl) CreateDH(filename string, size int) (*DH, error) {
var err error
filename = o.Path + "/" + filename
log.Info("Generate Diffie-Hellman key (", filename, ")")
dh := &DH{}
dh.path = filename
dh.content, err = exec.Command("openssl", "dhparam", strconv.Itoa(size)).Output()
if err != nil {
log.Error(err)
return nil, err
}
err = ioutil.WriteFile(filename, dh.content, 0600)
return dh, err
}
func (dh *DH) GetFilePath() string {
return dh.path
}
func (dh *DH) String() string {
return string(dh.content)
}