diff --git a/key.go b/key.go index c1d6f52..6f91d4c 100644 --- a/key.go +++ b/key.go @@ -20,7 +20,7 @@ func generate() { } func encrypt() { - key, err := readKey() + key, err := publicKey() if err != nil { exit(1, "couldn't setup key: %v", err) } @@ -28,7 +28,7 @@ func encrypt() { if err != nil { exit(1, "error reading input message: %v", err) } - ctxt, err := rsa.EncryptPKCS1v15(rand.Reader, &key.PublicKey, msg) + ctxt, err := rsa.EncryptPKCS1v15(rand.Reader, key, msg) if err != nil { exit(1, "error encrypting message: %v", err) } @@ -46,7 +46,7 @@ func encrypt() { } func decrypt() { - key, err := readKey() + key, err := privateKey() if err != nil { exit(1, "couldn't setup key: %v", err) } @@ -70,7 +70,28 @@ func decrypt() { fmt.Printf("%s", msg) } -func readKey() (*rsa.PrivateKey, error) { +func publicKey() (*rsa.PublicKey, error) { + if options.publicKey == "" { + priv, err := privateKey() + if err != nil { + return nil, err + } + return &priv.PublicKey, nil + } + f, err := os.Open(options.publicKey) + if err != nil { + return nil, fmt.Errorf("unable to read public key from file %s: %v", options.publicKey, err) + } + defer f.Close() + d1 := json.NewDecoder(f) + var key rsa.PublicKey + if err := d1.Decode(&key); err != nil { + return nil, fmt.Errorf("unable to decode key from file %s: %v", options.key, err) + } + return &key, nil +} + +func privateKey() (*rsa.PrivateKey, error) { f, err := os.Open(options.key) if err != nil { return nil, fmt.Errorf("unable to open private key file at %s: %v", options.key, err) @@ -86,7 +107,7 @@ func readKey() (*rsa.PrivateKey, error) { } func getPublic() { - priv, err := readKey() + priv, err := privateKey() if err != nil { exit(1, "unable to read private key file: %v", err) } diff --git a/whisper.go b/whisper.go index 75f8f8c..f1aa105 100644 --- a/whisper.go +++ b/whisper.go @@ -16,10 +16,11 @@ var ( ) var options struct { - port int - host string - key string - nick string + port int + host string + key string + publicKey string + nick string } func exit(status int, template string, args ...interface{}) { @@ -66,5 +67,6 @@ func init() { flag.IntVar(&options.port, "port", 9000, "port number") flag.StringVar(&options.host, "host", "localhost", "host to connect to") flag.StringVar(&options.key, "key", "whisper_key", "rsa key to use") + flag.StringVar(&options.publicKey, "public-key", "", "public rsa key to use") flag.StringVar(&options.nick, "nick", "", "nick to use in chat") }