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 ( var (
injson = flag.String("input", "-", "input json for signing")
keyfile = flag.String("key", "auditor.key", "filename of EC25519 private key") keyfile = flag.String("key", "auditor.key", "filename of EC25519 private key")
url = flag.String("url", "https://auditor.codeblau.de/", "auditor url") url = flag.String("url", "https://auditor.codeblau.de/", "auditor url")
) )
func main() { 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() flag.Parse()
if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(1)
}
if len(*keyfile) == 0 { if len(*keyfile) == 0 {
log.Println("keyfile needed") log.Println("keyfile needed")
return return
@ -626,25 +636,48 @@ func main() {
log.Printf("couldn't read keyfile: %v\n", e) log.Printf("couldn't read keyfile: %v\n", e)
return return
} }
pk := ed25519.NewKeyFromSeed(k) pk := ed25519.NewKeyFromSeed(k)
pub := (pk.Public()).(ed25519.PublicKey)
pe, _ := crockfordEncode(pub)
input := new(Input) switch flag.Arg(0) {
dec := json.NewDecoder(os.Stdin) case "show":
e = dec.Decode(input) fmt.Printf("public key: %s\n", string(pe))
if e != nil { case "sign":
log.Fatal(e) var dec *json.Decoder
}
output, err := Sign(input, *url, pk) input := new(Input)
if err != nil { if *injson == "-" {
log.Fatalf("error signing: %v", err) 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) dec = json.NewDecoder(f)
enc.SetIndent("", " ") }
e = enc.Encode(output) e = dec.Decode(input)
if e != nil { if e != nil {
log.Fatal(e) 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)
} }
} }