aboutsummaryrefslogtreecommitdiff
path: root/generate.go
blob: adc94b8c969df51e09399667a3d6b0e289ff27ed (plain)
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 main

import (
	"flag"

	"dim13.org/signify/ask"
	"dim13.org/signify/b64file"
	"dim13.org/signify/key"
)

// Usage: signify -G [-n] [-c comment] -p pubkey -s seckey

func generate(args []string) error {
	opts := flag.NewFlagSet("generate", flag.ExitOnError)
	var (
		nopass  = opts.Bool("n", false, "No key passphrase")
		comment = opts.String("c", "signify", "Comment")
		pubFile = opts.String("p", "", "Public key file (required)")
		secFile = opts.String("s", "", "Secret key file (required)")
	)
	opts.Parse(args)
	if *pubFile == "" || *secFile == "" {
		opts.Usage()
		return nil
	}
	if err := ValidateNames(*pubFile, *secFile); err != nil {
		return err
	}

	var der key.Deriver
	if !*nopass {
		der = ask.Confirmed{}
	}

	pubKey, secKey, err := key.Generate(der)
	if err != nil {
		return err
	}

	if err := encodeSec(*secFile, *comment+" secret key", secKey); err != nil {
		return err
	}
	if err := encodePub(*pubFile, *comment+" public key", pubKey); err != nil {
		return err
	}
	return nil
}

func encodeSec(fname, comment string, sig *key.Sec) error {
	fd, err := Create(fname, ModeSec)
	if err != nil {
		return err
	}
	defer fd.Close()
	return b64file.Encode(fd, sig, comment+" secret key", nil)
}

func encodePub(fname, comment string, pub *key.Pub) error {
	fd, err := Create(fname, ModePub)
	if err != nil {
		return err
	}
	defer fd.Close()
	return b64file.Encode(fd, pub, comment+" public key", nil)
}