added show command and command handling

This commit is contained in:
Özgür Kesim 2021-11-21 11:39:15 +01:00
parent b9cb91c4e1
commit 43f8927a1c
Signed by: oec
GPG Key ID: 3D76A56D79EDD9D7

View File

@ -608,14 +608,24 @@ func Sign(input *Input, url string, pk ed25519.PrivateKey) ([]SignOperation, err
}
var (
injson = flag.String("input", "-", "input json for signing")
keyfile = flag.String("key", "auditor.key", "filename of EC25519 private key")
url = flag.String("url", "https://auditor.codeblau.de/", "auditor url")
)
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n %s [Options] (show|sign)\nOptions:\n", os.Args[0], os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(1)
}
if len(*keyfile) == 0 {
log.Println("keyfile needed")
return
@ -626,25 +636,48 @@ func main() {
log.Printf("couldn't read keyfile: %v\n", e)
return
}
pk := ed25519.NewKeyFromSeed(k)
pub := (pk.Public()).(ed25519.PublicKey)
pe, _ := crockfordEncode(pub)
input := new(Input)
dec := json.NewDecoder(os.Stdin)
e = dec.Decode(input)
if e != nil {
log.Fatal(e)
}
switch flag.Arg(0) {
case "show":
fmt.Printf("public key: %s\n", string(pe))
case "sign":
var dec *json.Decoder
output, err := Sign(input, *url, pk)
if err != nil {
log.Fatalf("error signing: %v", err)
}
input := new(Input)
if *injson == "-" {
dec = json.NewDecoder(os.Stdin)
} else {
f, e := os.Open(*injson)
if e != nil {
log.Fatal(e)
}
defer f.Close()
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
e = enc.Encode(output)
if e != nil {
log.Fatal(e)
dec = json.NewDecoder(f)
}
e = dec.Decode(input)
if e != nil {
log.Fatal(e)
}
output, err := Sign(input, *url, pk)
if err != nil {
log.Fatalf("error signing: %v", err)
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
e = enc.Encode(output)
if e != nil {
log.Fatal(e)
}
default:
flag.Usage()
os.Exit(2)
}
}