-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
generate.go
60 lines (51 loc) · 1.68 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
package cli
import (
"bytes"
"context"
"os"
"os/exec"
"github.com/gopasspw/gopass/pkg/out"
"github.com/pkg/errors"
)
// CreatePrivateKeyBatch will create a new GPG keypair in batch mode
func (g *GPG) CreatePrivateKeyBatch(ctx context.Context, name, email, passphrase string) error {
buf := &bytes.Buffer{}
// https://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob;f=doc/DETAILS;h=de0f21ccba60c3037c2a155156202df1cd098507;hb=refs/heads/STABLE-BRANCH-1-4#l716
_, _ = buf.WriteString(`%echo Generating a RSA/RSA key pair
Key-Type: RSA
Key-Length: 2048
Subkey-Type: RSA
Subkey-Length: 2048
Expire-Date: 0
`)
_, _ = buf.WriteString("Name-Real: " + name + "\n")
_, _ = buf.WriteString("Name-Email: " + email + "\n")
_, _ = buf.WriteString("Passphrase: " + passphrase + "\n")
args := []string{"--batch", "--gen-key"}
cmd := exec.CommandContext(ctx, g.binary, args...)
cmd.Stdin = bytes.NewReader(buf.Bytes())
cmd.Stdout = nil
cmd.Stderr = nil
out.Debug(ctx, "gpg.CreatePrivateKeyBatch: %s %+v", cmd.Path, cmd.Args)
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "failed to run command: '%s %+v'", cmd.Path, cmd.Args)
}
g.privKeys = nil
g.pubKeys = nil
return nil
}
// CreatePrivateKey will create a new GPG key in interactive mode
func (g *GPG) CreatePrivateKey(ctx context.Context) error {
args := []string{"--gen-key"}
cmd := exec.CommandContext(ctx, g.binary, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
out.Debug(ctx, "gpg.CreatePrivateKey: %s %+v", cmd.Path, cmd.Args)
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "failed to run command: '%s %+v'", cmd.Path, cmd.Args)
}
g.privKeys = nil
g.pubKeys = nil
return nil
}