aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2015-02-20 10:45:42 +0100
committerGibheer <gibheer@gmail.com>2015-02-20 10:45:42 +0100
commit075865c417c387783d37a6705d66034f2fd9ff4a (patch)
tree106a183e55b732d84bd9532751587e6ae978173f /main.go
parent1c621c063c26205e23af7bc7e3da4b5064856d4c (diff)
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.
Diffstat (limited to 'main.go')
-rw-r--r--main.go29
1 files changed, 26 insertions, 3 deletions
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)
}