Skip to content

Commit

Permalink
Read encryption key from json file
Browse files Browse the repository at this point in the history
  • Loading branch information
sharma1612harshit committed Jun 3, 2020
1 parent 052e0ba commit 3c2e89a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,18 @@ Optional Arguments:
1) -k (encryption key):
gocrypt -w filename -k encryption_key
gocrypt -r filename -k encryption_key
if -k is not provided then the default key is used
if key is not provided then the default key is used

2) -f (file name of file containing encryption key)
gocrypt -w filename -f /home/username/encryptionkey.json
gocrypt -r filename -f /home/username/encryptionkey.json
```
Writing keys in file, while using `-f` option: <br />
The file should be a valid json file with a key value pair where both key and value are strings.
```shell script
{
"key": "MyENCRYptionkey123"
}
```
3 changes: 3 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Optional Arguments:
gocrypt -w filename -k encryption_key
gocrypt -r filename -k encryption_key
if key is not provided then the default key is used
2) -f (file name of file containing encryption key)
gocrypt -w filename -f <path to json file>
gocrypt -r filename -f <path to json file>
`
)

Expand Down
21 changes: 21 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"encoding/json"
"errors"
"io/ioutil"
"os/exec"
Expand Down Expand Up @@ -58,6 +59,26 @@ func ArgParse(args []string) ([]string, error) {
key := args[4]
return []string{option, filename, key}, nil
}
} else if keyOpt == "-f" {
if len(args) == 4 {
return []string{option, filename, ""}, errors.New("no file name supplied ")
} else {
confFile := args[4]
keyConfig, err := ReadFromFile(confFile)

if err != nil {
return []string{"", "", ""}, errors.New(err.Error())
}

keyModal := make(map[string]string, 0)
err = json.Unmarshal(keyConfig, &keyModal)

if err != nil {
return []string{"", "", ""}, errors.New(err.Error())
}

return []string{option, filename, keyModal["key"]}, nil
}
} else { // throws error if supplied options are not recognised
return []string{"", "", ""}, errors.New("invalid option, use -h to see available options ")
}
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.0.0
v1.1.0

0 comments on commit 3c2e89a

Please sign in to comment.