master
Jordan Orelli 10 years ago
parent dbb21f4b94
commit cb475af77a

@ -20,7 +20,7 @@ func generate() {
} }
func encrypt() { func encrypt() {
key, err := readKey() key, err := publicKey()
if err != nil { if err != nil {
exit(1, "couldn't setup key: %v", err) exit(1, "couldn't setup key: %v", err)
} }
@ -28,7 +28,7 @@ func encrypt() {
if err != nil { if err != nil {
exit(1, "error reading input message: %v", err) 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 { if err != nil {
exit(1, "error encrypting message: %v", err) exit(1, "error encrypting message: %v", err)
} }
@ -46,7 +46,7 @@ func encrypt() {
} }
func decrypt() { func decrypt() {
key, err := readKey() key, err := privateKey()
if err != nil { if err != nil {
exit(1, "couldn't setup key: %v", err) exit(1, "couldn't setup key: %v", err)
} }
@ -70,7 +70,28 @@ func decrypt() {
fmt.Printf("%s", msg) 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) f, err := os.Open(options.key)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to open private key file at %s: %v", options.key, err) 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() { func getPublic() {
priv, err := readKey() priv, err := privateKey()
if err != nil { if err != nil {
exit(1, "unable to read private key file: %v", err) exit(1, "unable to read private key file: %v", err)
} }

@ -19,6 +19,7 @@ var options struct {
port int port int
host string host string
key string key string
publicKey string
nick string nick string
} }
@ -66,5 +67,6 @@ func init() {
flag.IntVar(&options.port, "port", 9000, "port number") flag.IntVar(&options.port, "port", 9000, "port number")
flag.StringVar(&options.host, "host", "localhost", "host to connect to") flag.StringVar(&options.host, "host", "localhost", "host to connect to")
flag.StringVar(&options.key, "key", "whisper_key", "rsa key to use") 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") flag.StringVar(&options.nick, "nick", "", "nick to use in chat")
} }

Loading…
Cancel
Save