Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/avtool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/pbthorste/avtool"
"github.com/smallfish/simpleyaml"
"golang.org/x/crypto/ssh/terminal"
"gopkg.in/urfave/cli.v1"
cli "gopkg.in/urfave/cli.v1"
)

var (
Expand Down
25 changes: 14 additions & 11 deletions decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"golang.org/x/crypto/pbkdf2"
"io/ioutil"
"log"
"strings"

"golang.org/x/crypto/pbkdf2"
)

func check(e error) {
Expand All @@ -24,8 +25,10 @@ func DecryptFile(filename, password string) (result string, err error) {
data, err := ioutil.ReadFile(filename)
check(err)
result, err = Decrypt(string(data), password)
check(err)
return
}

// Decrypt a string containing the ansible vault
func Decrypt(data, password string) (result string, err error) {
defer func() {
Expand All @@ -51,7 +54,7 @@ func Decrypt(data, password string) (result string, err error) {

// in order to support vault files with windows line endings
func replaceCarriageReturn(data string) string {
return strings.Replace(data, "\r","",-1)
return strings.Replace(data, "\r", "", -1)
}

/*
Expand All @@ -77,17 +80,17 @@ https://github.com/ansible/ansible/blob/0b8011436dc7f842b78298848e298f2a57ee8d78
func decodeData(body string) (salt, cryptedHmac, ciphertext []byte) {
decoded, _ := hex.DecodeString(body)
elements := strings.SplitN(string(decoded), "\n", 3)
salt, err1 := hex.DecodeString(elements[0])
if err1 != nil {
panic(err1)
salt, err := hex.DecodeString(elements[0])
if err != nil {
panic(err)
}
cryptedHmac, err2 := hex.DecodeString(elements[1])
if err2 != nil {
panic(err2)
cryptedHmac, err = hex.DecodeString(elements[1])
if err != nil {
panic(err)
}
ciphertext, err3 := hex.DecodeString(elements[2])
if err3 != nil {
panic(err3)
ciphertext, err = hex.DecodeString(elements[2])
if err != nil {
panic(err)
}
return
}
Expand Down
12 changes: 6 additions & 6 deletions encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package avtool

import (
"crypto/aes"
"crypto/rand"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"strings"
"io/ioutil"
"strings"
)

func GenerateRandomBytes(n int) ([]byte, error) {
Expand Down Expand Up @@ -36,15 +36,15 @@ func Encrypt(body, password string) (result string, err error) {
//salt,_ = hex.DecodeString(salt_64)
key1, key2, iv := genKeyInitctr(password, salt)
ciphertext := createCipherText(body, key1, iv)
combined := combineParts(ciphertext,key2,salt)
combined := combineParts(ciphertext, key2, salt)
vaultText := hex.EncodeToString([]byte(combined))
result = formatOutput(vaultText)
return
}

func createCipherText(body string, key1,iv []byte) []byte {
func createCipherText(body string, key1, iv []byte) []byte {
bs := aes.BlockSize
padding := (bs - len(body) % bs)
padding := (bs - len(body)%bs)
if padding == 0 {
padding = bs
}
Expand Down Expand Up @@ -100,4 +100,4 @@ func formatOutput(vaultText string) string {

whole := strings.Join(elements, "\n")
return whole
}
}