Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Vault
Commits
3a8f0c2c
Commit
3a8f0c2c
authored
3 years ago
by
Vinay Gopalan
Browse files
Options
Download
Email Patches
Plain Diff
add initial stub for encrypt
parent
eee6d622
Branches unavailable
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
command/commands.go
+5
-0
command/commands.go
command/encrypt.go
+137
-0
command/encrypt.go
with
142 additions
and
0 deletions
+142
-0
command/commands.go
+
5
-
0
View file @
3a8f0c2c
...
...
@@ -283,6 +283,11 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
BaseCommand
:
getBaseCommand
(),
},
nil
},
"encrypt"
:
func
()
(
cli
.
Command
,
error
)
{
return
&
EncryptCommand
{
BaseCommand
:
getBaseCommand
(),
},
nil
},
"lease"
:
func
()
(
cli
.
Command
,
error
)
{
return
&
LeaseCommand
{
BaseCommand
:
getBaseCommand
(),
...
...
This diff is collapsed.
Click to expand it.
command/encrypt.go
0 → 100644
+
137
-
0
View file @
3a8f0c2c
package
command
import
(
"fmt"
"log"
"strings"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"io"
"io/ioutil"
"github.com/mitchellh/cli"
)
var
_
cli
.
Command
=
(
*
EncryptCommand
)(
nil
)
type
EncryptCommand
struct
{
*
BaseCommand
aes128
bool
// outfile string
}
func
(
c
*
EncryptCommand
)
Synopsis
()
string
{
return
"Encrypts a file using AES 128bit or 256bit encryption."
}
func
(
c
*
EncryptCommand
)
Help
()
string
{
helpText
:=
`
Usage: vault encrypt [options] [filename]
Encrypts a file using AES encryption.
Encrypt a single file:
$ vault encrypt -o foo.enc foo.txt
`
return
strings
.
TrimSpace
(
helpText
)
}
func
(
c
*
EncryptCommand
)
Flags
()
*
FlagSets
{
set
:=
c
.
flagSet
(
FlagSetHTTP
)
f
:=
set
.
NewFlagSet
(
"Command Options"
)
f
.
BoolVar
(
&
BoolVar
{
Name
:
"aes128"
,
Target
:
&
c
.
aes128
,
Default
:
false
,
Usage
:
"Encrypt the file using 128bit encryption instead of the default aes256."
,
})
// f.StringVar(&StringVar{
// Name: "out",
// Aliases: []string{"o"},
// Target: &c.outfile,
// Default: "output.enc",
// Usage: "Specify the name of the output file.",
// })
return
set
}
func
(
c
*
EncryptCommand
)
Run
(
args
[]
string
)
int
{
f
:=
c
.
Flags
()
if
err
:=
f
.
Parse
(
args
);
err
!=
nil
{
c
.
UI
.
Error
(
err
.
Error
())
return
1
}
args
=
f
.
Args
()
switch
{
case
len
(
args
)
<
1
:
c
.
UI
.
Error
(
fmt
.
Sprintf
(
"Not enough arguments (expected 1, got %d)"
,
len
(
args
)))
return
1
case
len
(
args
)
>
1
:
c
.
UI
.
Error
(
fmt
.
Sprintf
(
"Too many arguments (expected 1, got %d)"
,
len
(
args
)))
return
1
}
filename
:=
strings
.
TrimSpace
(
args
[
0
])
data
,
err
:=
ioutil
.
ReadFile
(
filename
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
if
!
c
.
aes128
{
key
:=
make
([]
byte
,
32
)
if
_
,
err
:=
rand
.
Read
(
key
);
err
!=
nil
{
fmt
.
Errorf
(
err
.
Error
())
}
keyString
:=
hex
.
EncodeToString
(
key
)
// TODO Write keyString to file
fmt
.
Printf
(
"Key written to file: %s"
,
keyString
)
encrypt
(
data
,
key
)
}
else
{
// Create 128bit key
}
return
0
}
func
encrypt
(
dataToEncrypt
[]
byte
,
key
[]
byte
)
{
// Create a new Cipher Block using key
block
,
err
:=
aes
.
NewCipher
(
key
)
if
err
!=
nil
{
fmt
.
Errorf
(
err
.
Error
())
}
// Create a new GCM
aesGCM
,
err
:=
cipher
.
NewGCM
(
block
)
if
err
!=
nil
{
fmt
.
Errorf
(
err
.
Error
())
}
// Create a nonce from GCM
nonce
:=
make
([]
byte
,
aesGCM
.
NonceSize
())
if
_
,
err
=
io
.
ReadFull
(
rand
.
Reader
,
nonce
);
err
!=
nil
{
fmt
.
Errorf
(
err
.
Error
())
}
// Encrypt and write to file
ciphertext
:=
aesGCM
.
Seal
(
nonce
,
nonce
,
dataToEncrypt
,
nil
)
err
=
ioutil
.
WriteFile
(
"output.enc"
,
ciphertext
,
0644
)
if
err
!=
nil
{
fmt
.
Errorf
(
err
.
Error
())
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help