-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (76 loc) · 1.66 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
)
func main() {
flag.Parse()
//Expected commands:
//1) ed25519 sign <key> <file>
//2) ed25519 verify <pub> <file> <signature>
//3) ed25519 gen <key> <pub>
log.SetFlags(log.Lshortfile)
if flag.NArg() == 0 {
log.Fatal("missing command: [sign|verify|gen]")
}
switch flag.Arg(0) {
case "sign":
if flag.NArg() != 3 {
log.Fatal("command 'sign' requires: <private-key-file> <file-to-sign>")
}
key := flag.Arg(1)
file := flag.Arg(2)
sign(key, file)
case "verify":
if flag.NArg() != 4 {
log.Fatal("command 'verify' requires: <public-key-file> <file-to-check> <signature>")
}
key := flag.Arg(1)
file := flag.Arg(2)
signature := flag.Arg(3)
verify(key, file, signature)
case "gen":
if flag.NArg() != 3 {
log.Fatal("command 'gen' requires: <private-key-file> <public-key-file>")
}
key := flag.Arg(1)
public := flag.Arg(2)
generate(key, public)
default:
log.Fatalf("command %s not supported", flag.Arg(0))
}
}
func sign(privKey, fileToSign string) {
//Read the private key
key, err := GetPrivateKey(privKey)
if err != nil {
log.Fatal(err)
}
//Compute the signature on the input file
signature, err := key.Sign(fileToSign)
if err != nil {
log.Fatal(err)
}
fmt.Println("Signature:", signature)
}
func verify(pubKey, fileToCheck, signature string) {
//Read the public key
pub, err := GetPublicKey(pubKey)
if err != nil {
log.Fatal(err)
}
//Verify if input signature is valid
ok, err := pub.Verify(fileToCheck, signature)
if err != nil {
log.Fatal(err)
}
if ok {
fmt.Println("Valid signature")
os.Exit(0)
} else {
fmt.Println("Invalid signature")
os.Exit(1)
}
}