diff --git a/cmd/avtool/main.go b/cmd/avtool/main.go index 41ad6ca..60a68c1 100644 --- a/cmd/avtool/main.go +++ b/cmd/avtool/main.go @@ -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 ( diff --git a/decrypt.go b/decrypt.go index 74c4004..c1e1be4 100644 --- a/decrypt.go +++ b/decrypt.go @@ -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) { @@ -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() { @@ -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) } /* @@ -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 } diff --git a/encrypt.go b/encrypt.go index cb3f8ad..332dc05 100644 --- a/encrypt.go +++ b/encrypt.go @@ -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) { @@ -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 } @@ -100,4 +100,4 @@ func formatOutput(vaultText string) string { whole := strings.Join(elements, "\n") return whole -} \ No newline at end of file +}