Official cwtch.im peer and server implementations. https://cwtch.im
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.3KB

  1. package v1
  2. import (
  3. "git.openprivacy.ca/openprivacy/log"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. )
  8. // fileStore stores a cwtchPeer in an encrypted file
  9. type fileStore struct {
  10. directory string
  11. filename string
  12. key [32]byte
  13. }
  14. // FileStore is a primitive around storing encrypted files
  15. type FileStore interface {
  16. Write([]byte) error
  17. Read() ([]byte, error)
  18. Delete()
  19. ChangeKey(newkey [32]byte)
  20. }
  21. // NewFileStore instantiates a fileStore given a filename and a password
  22. func NewFileStore(directory string, filename string, key [32]byte) FileStore {
  23. filestore := new(fileStore)
  24. filestore.key = key
  25. filestore.filename = filename
  26. filestore.directory = directory
  27. return filestore
  28. }
  29. // write serializes a cwtchPeer to a file
  30. func (fps *fileStore) Write(data []byte) error {
  31. encryptedbytes, err := encryptFileData(data, fps.key)
  32. if err != nil {
  33. return err
  34. }
  35. err = ioutil.WriteFile(path.Join(fps.directory, fps.filename), encryptedbytes, 0600)
  36. return err
  37. }
  38. func (fps *fileStore) Read() ([]byte, error) {
  39. return readEncryptedFile(fps.directory, fps.filename, fps.key)
  40. }
  41. func (fps *fileStore) Delete() {
  42. err := os.Remove(path.Join(fps.directory, fps.filename))
  43. if err != nil {
  44. log.Errorf("Deleting file %v\n", err)
  45. }
  46. }
  47. func (fps *fileStore) ChangeKey(newkey [32]byte) {
  48. fps.key = newkey
  49. }