aboutsummaryrefslogtreecommitdiff
path: root/generate.go
blob: f067b8b294e59078fca59c94f2e287375e235dde (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
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 := b64file.EncodeFile(*secFile, ModeSec, secKey, *comment+" secret key", nil); err != nil {
		return err
	}
	if err := b64file.EncodeFile(*pubFile, ModePub, pubKey, *comment+" public key", nil); err != nil {
		return err
	}
	return nil
}