Skip to content

Commit

Permalink
Update decryption mecanism to use password from global option [ref #13]
Browse files Browse the repository at this point in the history
This is basically a workaround the global password option system.
A huge refactoring should be made later on to get rid of the whole
environment struct thing.
  • Loading branch information
Oleiade committed Nov 24, 2013
1 parent ba00e58 commit 0773bad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
16 changes: 8 additions & 8 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func AddRecipientAction(c *cli.Context) {

recipient := c.Args()[0]

store, err := NewEncryptedStoreFromFile(gStorePath)
store, err := NewEncryptedStoreFromFile(gStorePath, c.GlobalString("password"))
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func RemoveRecipientAction(c *cli.Context) {

recipient := c.Args()[0]

store, err := NewEncryptedStoreFromFile(gStorePath)
store, err := NewEncryptedStoreFromFile(gStorePath, c.GlobalString("password"))
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -252,7 +252,7 @@ func GetAction(c *cli.Context) {
log.Fatal("Incorrect number of arguments to 'get' command")
}

store, err := NewEncryptedStoreFromFile(gStorePath)
store, err := NewEncryptedStoreFromFile(gStorePath, c.GlobalString("password"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -270,7 +270,7 @@ func SetAction(c *cli.Context) {
log.Fatal("Incorrect number of arguments to 'set' command")
}

store, err := NewEncryptedStoreFromFile(gStorePath)
store, err := NewEncryptedStoreFromFile(gStorePath, c.GlobalString("password"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -293,7 +293,7 @@ func DelAction(c *cli.Context) {
log.Fatal("Incorrect number of arguments to 'del' command")
}

store, err := NewEncryptedStoreFromFile(gStorePath)
store, err := NewEncryptedStoreFromFile(gStorePath, c.GlobalString("password"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -316,7 +316,7 @@ func KeysAction(c *cli.Context) {
log.Fatal("Incorrect number of arguments to 'keys' command")
}

store, err := NewEncryptedStoreFromFile(gStorePath)
store, err := NewEncryptedStoreFromFile(gStorePath, c.GlobalString("password"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -336,7 +336,7 @@ func ShowAction(c *cli.Context) {
log.Fatal("Incorrect number of arguments to 'show' command")
}

store, err := NewEncryptedStoreFromFile(gStorePath)
store, err := NewEncryptedStoreFromFile(gStorePath, c.GlobalString("password"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -356,7 +356,7 @@ func MetaAction(c *cli.Context) {
log.Fatal("Incorrect number of arguments to 'meta' command")
}

store, err := NewEncryptedStoreFromFile(gStorePath)
store, err := NewEncryptedStoreFromFile(gStorePath, c.GlobalString("password"))
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion decryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func initCrypto(keyRingPath, pass string) {
password = pass
}

func decrypt(s string) (string, error) {
func decrypt(s, password string) (string, error) {
if s == "" {
return "", nil
}
Expand Down
16 changes: 9 additions & 7 deletions encrypted_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

type EncryptedStore struct {
DataStore
Data string
Encrypted bool
Data string
Encrypted bool
Passphrase string
}

func NewEncryptedStore(store *DataStore) *EncryptedStore {
Expand All @@ -23,16 +24,17 @@ func NewEncryptedStore(store *DataStore) *EncryptedStore {
}
}

func NewEncryptedStoreFromFile(filePath string) (*EncryptedStore, error) {
func NewEncryptedStoreFromFile(filePath, passphrase string) (*EncryptedStore, error) {
encryptedData, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, errors.New("trousseau data store file ($HOME/.trousseau) not found")
}

encryptedStore := &EncryptedStore{
DataStore: *NewDataStore(),
Data: string(encryptedData),
Encrypted: true,
DataStore: *NewDataStore(),
Data: string(encryptedData),
Encrypted: true,
Passphrase: passphrase,
}

return encryptedStore, nil
Expand Down Expand Up @@ -64,7 +66,7 @@ func (es *EncryptedStore) Decrypt() error {
// Decrypt store data
environment := NewEnvironment()
initCrypto(gSecringFile, environment.Password)
es.Data, err = decrypt(es.Data)
es.Data, err = decrypt(es.Data, es.Passphrase)
if err != nil {
return err
}
Expand Down

0 comments on commit 0773bad

Please sign in to comment.