-
Notifications
You must be signed in to change notification settings - Fork 31
/
verify.go
85 lines (69 loc) · 1.87 KB
/
verify.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
84
85
package cmd
import (
"encoding/base64"
"errors"
"fmt"
"github.com/VirgilSecurity/virgil-cli/utils"
"gopkg.in/virgil.v5/cryptoimpl"
"io/ioutil"
"gopkg.in/urfave/cli.v2"
)
func Verify() *cli.Command {
return &cli.Command{
Name: "verify",
ArgsUsage: "[pub_key]",
Usage: "Verify signature",
Flags: []cli.Flag{
&cli.StringFlag{Name: "key", Usage: "public key file"},
&cli.StringFlag{Name: "i", Usage: "input file"},
&cli.StringFlag{Name: "s", Usage: "signature file"},
},
Action: func(context *cli.Context) error {
keyFileName := utils.ReadFlagOrDefault(context, "key", "")
if keyFileName == "" {
return errors.New("key file isn't specified (use -key)")
}
inputFileName := utils.ReadFlagOrDefault(context, "i", "")
if inputFileName == "" {
return errors.New("input file isn't specified (use -i)")
}
signatureFileName := utils.ReadFlagOrDefault(context, "s", "")
if signatureFileName == "" {
return errors.New("signature file isn't specified (use -s)")
}
publicKeyString, err := utils.ReadKeyStringFromFile(context, keyFileName)
if err != nil {
return err
}
data, err := ioutil.ReadFile(inputFileName)
if err != nil {
return err
}
signature, err := ioutil.ReadFile(signatureFileName)
if err != nil {
return err
}
err = VerifyFunc(publicKeyString, data, signature)
if err != nil {
return err
}
fmt.Println("Signature OK ")
return nil
},
}
}
func VerifyFunc(publicKeyString string, data, signature []byte) (err error) {
pk, err := cryptoimpl.DecodePublicKey([]byte(publicKeyString))
if err != nil {
return errors.New("can't import public key")
}
ss, err := base64.StdEncoding.DecodeString(string(signature))
if err != nil {
return err
}
err = crypto.VerifySignature(data, ss, pk)
if err != nil {
return errors.New("signature is invalid")
}
return nil
}