forked from mouuff/go-rocket-update
/
sign.go
63 lines (52 loc) · 1.47 KB
/
sign.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
package main
import (
"flag"
"io/ioutil"
"log"
"path/filepath"
"github.com/mouuff/go-rocket-update/internal/constant"
"github.com/mouuff/go-rocket-update/internal/crypto"
)
// Sign describes the sign subcommand
// this command is used to sign a folder
type Sign struct {
flagSet *flag.FlagSet
path string
key string
}
// Name gets the name of the command
func (cmd *Sign) Name() string {
return "sign"
}
// Init initializes the command
func (cmd *Sign) Init(args []string) error {
cmd.flagSet = flag.NewFlagSet(cmd.Name(), flag.ExitOnError)
cmd.flagSet.StringVar(&cmd.path, "path", "", "path to the package directory (required)")
cmd.flagSet.StringVar(&cmd.key, "key", "", "path to the private key (required)")
return cmd.flagSet.Parse(args)
}
// Run runs the command
func (cmd *Sign) Run() error {
log.Println("Reading private key...")
privkeyBytes, err := ioutil.ReadFile(cmd.key)
if err != nil {
return err
}
privateKey, err := crypto.ParsePemPrivateKey(privkeyBytes)
if err != nil {
return err
}
log.Println("Computing signatures...")
signatures, err := crypto.GetFolderSignatures(privateKey, cmd.path)
if err != nil {
return err
}
signaturesPath := filepath.Join(cmd.path, constant.SignatureRelPath)
log.Println("Writing " + signaturesPath + " ...")
err = crypto.WriteSignaturesToJSON(signaturesPath, signatures)
if err != nil {
return err
}
log.Println("Signed successfully! Don't forget to keep your private key in a safe place!")
return nil
}