Skip to content

Commit 39d4517

Browse files
authored
Kebab case passphrase generation and bug fixes (#2)
* New features added, Document changes and Makefile edit - Kebab case passphrase generation added for generate and encrypt command - Changed some of help definitions - Code refactoring * Bug fix, removed obsure error when encrypting an empty file * Bug fix for GH-1 * Bug fix for GH-1 * new fixtures for test cases * updated deps * Added error feedback for lcrypt.genSalt() instead of printing error to stdout
1 parent 9e6f626 commit 39d4517

19 files changed

Lines changed: 207 additions & 77 deletions

Gopkg.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/lyra/decrypt.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"github.com/azohra/lyra/pkg/lfile"
99
)
1010

11-
const helpstrdec = `
11+
const (
12+
helpstrdec = `
1213
The following examples are all the possible options for the "decrypt" command:
1314
1415
lyra decrypt file
@@ -47,14 +48,15 @@ lyra decrypt --print-only -p "mypassphrase" file
4748
4849
`
4950

50-
const usagePrint = `Prints the deciphered contents of a specified file to stdout, the original
51+
usagePrint = `Prints the deciphered contents of a specified file to stdout, the original
5152
file will be unchanged (i.e still encrypted with the same key).
5253
`
5354

54-
const usagePathDec = `Decrypts the contents of file and save the resulting plaintext in a new file.
55+
usagePathDec = `Decrypts the contents of file and save the resulting plaintext in a new file.
5556
The original specified file will be unchanged (i.e still encrypted with the
5657
same key) if this flag is set.
5758
`
59+
)
5860

5961
type decryptcmd struct {
6062
path string

cmd/lyra/encrypt.go

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,70 +11,89 @@ import (
1111
"github.com/brsmsn/gware/pkg/diceware"
1212
)
1313

14-
const helpstrenc = `
14+
const (
15+
helpstrenc = `
1516
The following exmaples are all the possible options for the "encrypt" command:
16-
17+
1718
lyra encrypt file
18-
19+
1920
Encrypts and overides file (user specified file). Users will be asked to provide
2021
the passphrase via stdin.
21-
22+
2223
lyra encrypt -s file1 file
23-
24+
2425
Encrypts the contents of file (user specified file) and save the resulting output
2526
to file1 (user specified file or specified path). Users will be asked to provide
2627
the passphrase via stdin. Original specified file will still remain in plaintext.
27-
28-
lyra encrypt --auto-gen file
2928
29+
lyra encrypt --auto-gen file
30+
3031
Encrypts and overides file (user specified file) with an auto generated passphrase
3132
and outputs the auto generated passphrase to stdout.
3233
The auto generated passphrase is a 7 word passphrase generated via the diceware
3334
method using the EFF new wordlist. It is imperative that the user keep a record
3435
of the outputted passphrase as there will be no way to decipher the file without it.
3536
36-
lyra encrypt -p "mypassphrase" file
37+
lyra encrypt --gen-str file
3738
39+
Encrypts and overides file (user specified file) with an auto generated passphrase
40+
and outputs the auto generated passphrase to stdout.
41+
Auto generaates a 7 word passphrase in kebab case (no spaces).
42+
43+
lyra encrypt -p "mypassphrase" file
44+
3845
Encrypts and overides file (user specified file) with passphrase "mypassphrase",
3946
this option will disable stdin interaction. Using this option will also disable
4047
passphrase checking, therefore it is critical that you do not misspell or forget
4148
the passphrase.
42-
49+
4350
lyra encrypt --auto-gen -s file1 file
44-
45-
Encrypts file (user specified file) with an auto generated passphrase and save the
46-
resulting output to file1 (user specified file). The auto generated passphrase will
51+
52+
Encrypts file (user specified file) with an auto generated dicewre passphrase and save
53+
the resulting output to file1 (user specified file). The auto generated passphrase will
4754
be outputted to stdout and the original specified file will still remain in plaintext
4855
/decrypted.
49-
56+
57+
lyra encrypt --gen-str -s file1 file
58+
59+
Encrypts file (user specified file) with an auto generated a 7 word passphrase without
60+
spaces and save the resulting output to file1 (user specified file). The auto generated
61+
passphrase will be outputted to stdout and the original specified file will still remain
62+
in plaintext /decrypted.
63+
5064
lyra encrypt -p "mypassphrase" -s file1 file
51-
65+
5266
Encrypts file (user specified file) with passphrase "mypasshphrase" and save the resulting
5367
output to file1. The original specified file will remain in plaintext/decrypted.
5468
Stdin interaction will also be disabled.
55-
69+
5670
`
5771

58-
const usagePass = `Specify a passphrase used to encrypt/decrypt the specified file, if this flag
72+
usagePass = `Specify a passphrase used to encrypt/decrypt the specified file, if this flag
5973
is set, passphrases will not fetched from stdin.
60-
74+
6175
For encryption this flag will disable passphrase verification. Be careful not
6276
to misspell your passphrase as there will be no way to decrypt your files!
6377
`
6478

65-
const usagePathEnc = `Encrypts the contents of file and save the resulting ciphertext in a new file.
79+
usagePathEnc = `Encrypts the contents of file and save the resulting ciphertext in a new file.
6680
The original specified file will be unchanged (i.e still decrypted) if this flag is set.
6781
`
6882

69-
const usageGen = `Auto generates a single 7 word passphrase that will be used as the key for the
83+
usageGenDice = `Auto generates a single 7 word passphrase that will be used as the key for the
7084
encryption of a specified file. The passphrase is a diceware generated passphrase using
7185
the EFF new wordlist.
7286
`
7387

88+
usageGenStr = `Auto generate a single 7 word diceware passphrase as a single no-spaced string.
89+
`
90+
)
91+
7492
type encryptcmd struct {
75-
path string
76-
passphrase string
77-
autogen bool
93+
path string
94+
passphrase string
95+
autogenDice bool
96+
autogenStr bool
7897
}
7998

8099
func (cmd *encryptcmd) CName() string {
@@ -88,7 +107,8 @@ func (cmd *encryptcmd) Help() string {
88107
func (cmd *encryptcmd) RegCFlags(fs *flag.FlagSet) {
89108
fs.StringVar(&cmd.passphrase, "p", "", usagePass)
90109
fs.StringVar(&cmd.path, "s", "", usagePathEnc)
91-
fs.BoolVar(&cmd.autogen, "auto-gen", false, usageGen)
110+
fs.BoolVar(&cmd.autogenDice, "auto-gen", false, usageGenDice)
111+
fs.BoolVar(&cmd.autogenStr, "gen-str", false, usageGenStr)
92112
}
93113

94114
func (cmd *encryptcmd) Run(opt []string) error {
@@ -103,8 +123,8 @@ func (cmd *encryptcmd) Run(opt []string) error {
103123
return err
104124
}
105125

106-
//if autogen was set
107-
if cmd.autogen {
126+
//if autogenDice was set
127+
if cmd.autogenDice || cmd.autogenStr {
108128
cmd.genPass()
109129
if err != nil {
110130
return err
@@ -130,8 +150,10 @@ func (cmd *encryptcmd) Run(opt []string) error {
130150
}
131151

132152
func (cmd *encryptcmd) validateInputs() error {
133-
if cmd.autogen && cmd.passphrase != "" {
153+
if (cmd.autogenDice || cmd.autogenStr) && cmd.passphrase != "" {
134154
return errors.New("Can not specify a passphrase when auto-gen flag has been set")
155+
} else if cmd.autogenDice && cmd.autogenStr {
156+
return errors.New("Can not specify --auto-gen and --gen-str at the same time")
135157
}
136158

137159
return nil
@@ -174,10 +196,17 @@ func encrypt(file, saveTo string, passphrase []byte) error {
174196

175197
//gen a diceware passphrase using eff long wordlist
176198
func (cmd *encryptcmd) genPass() error {
199+
177200
phrase, err := diceware.GeneratePassphrases(1, 7, diceware.EffWorldList)
178201
if err != nil {
179202
return err
180203
}
181-
cmd.passphrase = phrase[0]
204+
205+
if cmd.autogenStr {
206+
cmd.passphrase = removeSpaces(phrase[0])
207+
} else {
208+
cmd.passphrase = phrase[0]
209+
}
210+
182211
return nil
183212
}

cmd/lyra/generate.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,48 @@ import (
55
"flag"
66
"fmt"
77
"os"
8+
"strings"
89

910
"github.com/brsmsn/gware/pkg/diceware"
1011
)
1112

12-
const helpstrgen = `
13+
const (
14+
helpstrgen = `
1315
The following exmaples are all the possible options for the "generate" command:
1416
1517
lyra generate --words 7 --phrases 6
1618
1719
Generate 6 diceware passphrases each containing 7 words.
1820
21+
lyra generate --rm-spaces --words 7 --phrases 6
22+
23+
Generate 6 diceware passphrases each containing 7 words with no spaces
24+
25+
lyra generate --rm-spaces --words 7
26+
27+
Generate 1 diceware passphrase containing 7 words with no spaces
28+
29+
lyra generate --rm-spaces --phrases 7
30+
31+
Generate 7 diceware passphrase containing 7 words with no spaces
32+
1933
`
2034

21-
const usageWords = `Specify the number of words that a passphrase will have.
35+
usageWords = `Specify the number of words that a passphrase will have.
2236
`
2337

24-
const usagePhrases = `Specify the number of passphrases that will be generated.
38+
usagePhrases = `Specify the number of passphrases that will be generated.
2539
`
2640

41+
usageRmSpaces = `Specify removal of spaces, this will replace all spaces
42+
with a hyphen as a delimiter.
43+
`
44+
)
45+
2746
type gencmd struct {
2847
numWords int
2948
numPhrases int
49+
noSpaces bool
3050
}
3151

3252
func (cmd *gencmd) CName() string {
@@ -40,6 +60,7 @@ func (cmd *gencmd) Help() string {
4060
func (cmd *gencmd) RegCFlags(fs *flag.FlagSet) {
4161
fs.IntVar(&cmd.numWords, "words", 7, usageWords)
4262
fs.IntVar(&cmd.numPhrases, "phrases", 1, usagePhrases)
63+
fs.BoolVar(&cmd.noSpaces, "rm-spaces", false, usageRmSpaces)
4364
}
4465

4566
func (cmd *gencmd) Run(opt []string) error {
@@ -58,6 +79,9 @@ func (cmd *gencmd) Run(opt []string) error {
5879
}
5980

6081
for _, val := range list {
82+
if cmd.noSpaces {
83+
val = removeSpaces(val)
84+
}
6185
fmt.Fprint(os.Stdout, val+"\n")
6286
}
6387

@@ -73,3 +97,8 @@ func (cmd *gencmd) validateInputs() error {
7397

7498
return nil
7599
}
100+
101+
//removes spaces from a phrase
102+
func removeSpaces(phrase string) string {
103+
return strings.Replace(phrase, " ", "-", -1)
104+
}

cmd/lyra/lyra.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"reflect"
99
"strings"
10+
"syscall"
1011

1112
"golang.org/x/crypto/ssh/terminal"
1213
)
@@ -27,24 +28,24 @@ Commands:
2728
2829
encrypt Encipher a specified file with inputed passphrase
2930
decrypt Decipher a specified file with inputed passphrase
30-
generate Generate diceware passphrase(s) via the EFF new worldlist
31+
generate Generate passphrase(s)
3132
3233
To get more info on commands do: lyra [Command] --help
3334
`
3435
about = `Lyra is a lightweight tool used to protect sensitive data.
3536
36-
Coded with ❤️ by the Azohra team and made possible by open source software:
37+
Coded with ❤️ by the Azohra team and made possible with open source software:
3738
3839
* gware by brsmsn (BSD-3-clause) https://github.com/brsmsn/gware
3940
* license available @ https://github.com/brsmsn/gware/blob/master/LICENSE
4041
4142
* memguard by awnumar (Apache 2.0) https://github.com/awnumar/memguard
4243
* License available @ https://github.com/awnumar/memguard/blob/master/LICENSE
4344
44-
* crypto by golang.org (BSD-style) https://github.com/golang/crypto
45-
* license available @ https://github.com/golang/crypto/blob/master/LICENSE
45+
* crypto & sys by golang.org (BSD-style) https://github.com/golang/
46+
* license available @ https://github.com/golang/go/blob/master/LICENSE
4647
`
47-
version = `Version: 1.0.1 (April 2018)
48+
version = `Version: 1.1.0 (May 2018)
4849
`
4950
)
5051

@@ -155,17 +156,17 @@ func handleErr(err error) {
155156

156157
func getPassphrase() []byte {
157158
fmt.Println("Enter passphrase: ")
158-
input, err := terminal.ReadPassword(0)
159+
input, err := terminal.ReadPassword(syscall.Stdin)
159160
handleErr(err)
160161
return input
161162
}
162163

163164
func setPassphrase() ([]byte, error) {
164165
fmt.Println("Enter passphrase: ")
165-
in1, err := terminal.ReadPassword(0)
166+
in1, err := terminal.ReadPassword(syscall.Stdin)
166167
handleErr(err)
167168
fmt.Println("Enter passphrase again: ")
168-
in2, err := terminal.ReadPassword(0)
169+
in2, err := terminal.ReadPassword(syscall.Stdin)
169170
handleErr(err)
170171

171172
defer wipe(in2)

makefile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ PKG := "github.com/azohra/$(PROJECT_NAME)"
33
PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/)
44
GOPATH := $(shell go env GOPATH)
55

6-
.PHONY: test race msan coverage dep localbuild buildbins clean install binaries help
6+
.PHONY: test race msan coverage dep localbuild buildbins clean install binaries help build
77

88
test: ## Run all unit tests verbosely
99
@go test -v ./...
@@ -20,24 +20,25 @@ coverage: ## Generate global code coverage report
2020
dep: ## Get the dependencies
2121
@dep ensure -v
2222

23-
localbuild: ## Build the binary file
23+
localbuild: # Build the binary file
2424
@go install ./cmd/${PROJECT_NAME}/...
2525
@ln -s ${GOPATH}/bin/${PROJECT_NAME} /usr/local/bin
2626

27-
buildbins: #build on diff platform
27+
buildbins: # Build on diff platform
2828
@env GOOS=linux GOARCH=amd64 go build -v -o build/bin/linux/amd64/${PROJECT_NAME} ./cmd/${PROJECT_NAME}/...
2929
@env GOOS=darwin GOARCH=amd64 go build -v -o build/bin/darwin/amd64/${PROJECT_NAME} ./cmd/${PROJECT_NAME}/...
3030
@env GOOS=windows GOARCH=amd64 go build -v -o build/bin/win/amd64/${PROJECT_NAME}.exe ./cmd/${PROJECT_NAME}/...
3131

32-
clean: #Remove previous build and undo install
32+
clean: ## Remove previous build and undo install
3333
@rm -f /usr/local/bin/${PROJECT_NAME}
3434
@rm -f ${GOPATH}/bin/${PROJECT_NAME}
3535
@rm -rf build/bin
36+
@rm -f ./lyra
3637

37-
build:
38+
build: ## Build a binary in the current working directory
3839
@go build ./cmd/lyra/...
3940

40-
install: dep test localbuild ## install app
41+
install: dep test localbuild ## Build app and install it into shell PATH
4142

4243
binaries: dep test buildbins # Build diff binaries
4344

0 commit comments

Comments
 (0)