Add symmetric OpenPGP encryption

This commit is contained in:
Aaron Lindsay 2013-09-11 10:40:25 -04:00
parent e2ae508382
commit b5a525efb7
2 changed files with 71 additions and 4 deletions

View File

@ -31,6 +31,8 @@ type AsinkGlobals struct {
port int port int
username string username string
password string password string
encrypted bool
key string
} }
var globals AsinkGlobals var globals AsinkGlobals
@ -99,6 +101,12 @@ func StartClient(args []string) {
globals.username, err = config.GetString("server", "username") globals.username, err = config.GetString("server", "username")
globals.password, err = config.GetString("server", "password") globals.password, err = config.GetString("server", "password")
//TODO check errors on encryption settings
globals.encrypted, err = config.GetBool("encryption", "enabled")
if globals.encrypted {
globals.key, err = config.GetString("encryption", "key")
}
globals.db, err = GetAndInitDB(config) globals.db, err = GetAndInitDB(config)
if err != nil { if err != nil {
panic(err) panic(err)
@ -209,15 +217,28 @@ func ProcessLocalEvent(globals AsinkGlobals, event *asink.Event) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer uploadWriteCloser.Close()
uploadFile, err := os.Open(cachedFilename) uploadFile, err := os.Open(cachedFilename)
if err != nil { if err != nil {
uploadWriteCloser.Close()
panic(err) panic(err)
} }
defer uploadFile.Close()
if globals.encrypted {
encrypter, err := NewEncrypter(uploadWriteCloser, globals.key)
if err != nil {
uploadWriteCloser.Close()
uploadFile.Close()
panic(err)
}
_, err = io.Copy(encrypter, uploadFile)
encrypter.Close()
} else {
_, err = io.Copy(uploadWriteCloser, uploadFile) _, err = io.Copy(uploadWriteCloser, uploadFile)
}
uploadFile.Close()
uploadWriteCloser.Close()
StatStopUpload() StatStopUpload()
if err != nil { if err != nil {
panic(err) panic(err)
@ -289,7 +310,15 @@ func ProcessRemoteEvent(globals AsinkGlobals, event *asink.Event) {
panic(err) panic(err)
} }
defer downloadReadCloser.Close() defer downloadReadCloser.Close()
if globals.encrypted {
decrypter, err := NewDecrypter(downloadReadCloser, globals.key)
if err != nil {
panic(err)
}
_, err = io.Copy(outfile, decrypter)
} else {
_, err = io.Copy(outfile, downloadReadCloser) _, err = io.Copy(outfile, downloadReadCloser)
}
outfile.Close() outfile.Close()
StatStopDownload() StatStopDownload()

38
asink/encryption.go Normal file
View File

@ -0,0 +1,38 @@
/*
Copyright (C) 2013 Aaron Lindsay <aaron@aclindsay.com>
*/
package main
import (
"code.google.com/p/go.crypto/openpgp"
"io"
)
func NewEncrypter(writer io.WriteCloser, key string) (plaintextWriter io.WriteCloser, err error) {
return openpgp.SymmetricallyEncrypt(writer, []byte(key), nil, nil)
}
type Decrypter struct {
details *openpgp.MessageDetails
}
func NewDecrypter(ciphertextReader io.ReadCloser, key string) (decrypter io.Reader, err error) {
prompt := func(keys []openpgp.Key, symmetric bool) ([]byte, error) {
return []byte(key), nil
}
details, err := openpgp.ReadMessage(ciphertextReader, nil, prompt, nil)
if err != nil {
decrypter = nil
return
}
decrypter = Decrypter{details}
return
}
func (d Decrypter) Read(p []byte) (n int, err error) {
return d.details.UnverifiedBody.Read(p)
}