-
Notifications
You must be signed in to change notification settings - Fork 31
/
sign.go
83 lines (68 loc) · 1.89 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package cmd
import (
"encoding/base64"
"errors"
"fmt"
"github.com/VirgilSecurity/virgil-cli/utils"
"gopkg.in/virgil.v5/cryptoimpl"
"io"
"os"
"gopkg.in/urfave/cli.v2"
)
func Sign() *cli.Command {
return &cli.Command{
Name: "sign",
ArgsUsage: "[pr_key]",
Usage: "Sign data",
Flags: []cli.Flag{&cli.StringFlag{Name: "o", Usage: "destination file name"},
&cli.StringFlag{Name: "key", Usage: "private key file"},
&cli.StringFlag{Name: "p", Usage: "private key password"},
&cli.StringFlag{Name: "i", Usage: "input file"},
},
Action: func(context *cli.Context) error {
pass := utils.ReadFlagOrDefault(context, "p", "")
destinationFileName := utils.ReadFlagOrDefault(context, "o", "")
dataToSign, err := utils.ReadFileFlagOrParamOrFromConsole(context, "i", "data", "Enter data to sign")
if err != nil {
return err
}
keyFileName := utils.ReadFlagOrDefault(context, "key", "")
privateKeyString, err := utils.ReadKeyStringFromFile(context, keyFileName)
if err != nil {
return err
}
var writer io.Writer
if destinationFileName != "" {
file, err := os.Create(destinationFileName)
if err != nil {
return err
}
writer = file
defer func() {
if err := file.Close(); err != nil {
panic(err)
}
}()
} else {
writer = os.Stdout
}
signature, err := SignFunc(privateKeyString, pass, dataToSign)
if err != nil {
return err
}
_, err = fmt.Fprint(writer, base64.StdEncoding.EncodeToString(signature))
if err != nil {
return err
}
fmt.Println()
return err
},
}
}
func SignFunc(privateKeyString, password string, data []byte) (publicKey []byte, err error) {
pk, err := cryptoimpl.DecodePrivateKey([]byte(privateKeyString), []byte(password))
if err != nil {
return nil, errors.New("can't parse private key (may be key password required)")
}
return crypto.Sign(data, pk)
}