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 makeEncrypt(repo types.ConfigRepo) *cobra.Command {
|
2019-05-28 01:16:50 +00:00
|
|
|
encryptCmd := &cobra.Command{
|
|
|
|
Use: "encrypt [file]",
|
|
|
|
Short: "Encrypt a file using your masterpass and secret key",
|
|
|
|
Long: `Encrypt 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]
|
|
|
|
fileEnc := file + ".enc"
|
|
|
|
|
|
|
|
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.GCMEncrypt(passkey, contents)
|
2019-05-28 01:16:50 +00:00
|
|
|
check(err)
|
|
|
|
|
|
|
|
check(ioutil.WriteFile(fileEnc, contents, 0600))
|
|
|
|
check(os.Remove(file))
|
|
|
|
|
|
|
|
fmt.Println("Encrypted file: ", fileEnc)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return encryptCmd
|
|
|
|
}
|