From 075865c417c387783d37a6705d66034f2fd9ff4a Mon Sep 17 00:00:00 2001 From: Gibheer Date: Fri, 20 Feb 2015 10:45:42 +0100 Subject: add verification of messages This commit adds back the possibility to verify a message through a public key and a signature. It works a little bit different than before as it always prints the base64 version, but it makes it easier to use. --- main.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 3898417..c509fd7 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,7 @@ func main() { case "create-private": create_private_key() case "create-public": create_public_key() case "sign-input": sign_input() -// case "verify-signature": verify_signature() + case "verify-signature": verify_input() // case "create-cert-sign": create_sign_request() // case "sign-request": sign_request() case "help": print_modules() @@ -73,8 +73,8 @@ func create_public_key() { func sign_input() { fs := NewFlags("sign-input") fs.AddPrivateKey() - fs.AddOutput() fs.AddInput() + fs.AddOutput() err := fs.Parse(program_args()) if err != nil { os.Exit(2) } @@ -92,6 +92,29 @@ func sign_input() { } } +// verify a message using a signature and a public key +func verify_input() { + fs := NewFlags("sign-input") + fs.AddPublicKey() + fs.AddInput() + fs.AddOutput() + fs.AddSignature() + err := fs.Parse(program_args()) + if err != nil { os.Exit(2) } + + signature := fs.Flags.Signature + message, err := ioutil.ReadAll(fs.Flags.Input) + if err != nil { crash_with_help(2, "Error reading input: %s", err) } + valid, err := fs.Flags.PublicKey.Verify(message, signature, crypto.SHA256) + if err != nil { crash_with_help(2, "Could not verify message with signature: %s", err) } + if valid { + fmt.Println("valid") + os.Exit(0) + } + fmt.Println("invalid") + os.Exit(1) +} + // print the module help func print_modules() { fmt.Printf(`Usage: %s command args @@ -110,7 +133,7 @@ where 'command' is one of: // crash and provide a helpful message func crash_with_help(code int, message string, args ...interface{}) { - fmt.Fprintln(os.Stderr, message) + fmt.Fprintf(os.Stderr, message + "\n", args...) print_modules() os.Exit(code) } -- cgit v1.2.3-70-g09d2