2019-05-28 01:16:50 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2019-07-11 06:05:59 +00:00
|
|
|
"github.com/mitchell/selfpass/sp/crypto"
|
|
|
|
"github.com/mitchell/selfpass/sp/types"
|
2019-05-28 01:16:50 +00:00
|
|
|
)
|
|
|
|
|
2019-06-02 08:47:19 +00:00
|
|
|
func makeDecrypt(repo types.ConfigRepo) *cobra.Command {
|
2019-05-28 01:16:50 +00:00
|
|
|
decryptCmd := &cobra.Command{
|
|
|
|
Use: "decrypt [file]",
|
|
|
|
Short: "Decrypt a file using your masterpass and secret key",
|
|
|
|
Long: `Decrypt a file using your masterpass and secret key, and replace the old file with
|
|
|
|
the new file.`,
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-06-02 08:47:19 +00:00
|
|
|
masterpass, cfg, err := repo.OpenConfig()
|
|
|
|
check(err)
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
file := args[0]
|
|
|
|
fileout := file
|
|
|
|
|
|
|
|
if file[len(file)-4:] == ".enc" {
|
|
|
|
fileout = file[:len(file)-4]
|
|
|
|
}
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadFile(file)
|
|
|
|
check(err)
|
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
key := cfg.GetString(types.KeyPrivateKey)
|
|
|
|
passkey := crypto.GeneratePBKDF2Key([]byte(masterpass), []byte(key))
|
2019-05-28 01:16:50 +00:00
|
|
|
|
2019-06-07 09:03:15 +00:00
|
|
|
contents, err = crypto.GCMDecrypt(passkey, contents)
|
2019-05-28 01:16:50 +00:00
|
|
|
check(err)
|
|
|
|
|
|
|
|
check(ioutil.WriteFile(fileout, contents, 0600))
|
|
|
|
check(os.Remove(file))
|
|
|
|
|
|
|
|
fmt.Println("Decrypted file: ", fileout)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return decryptCmd
|
|
|
|
}
|