-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.go
116 lines (110 loc) · 3.04 KB
/
decrypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package commands
import (
"bytes"
"fmt"
"os"
"dev.shib.me/xipher"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
func fromXipherText(xipherText string) ([]byte, error) {
if len(xipherText) < len(xipherTxtPrefix) || xipherText[:len(xipherTxtPrefix)] != xipherTxtPrefix {
return nil, fmt.Errorf("invalid xipher text")
}
return decode(xipherText[len(xipherTxtPrefix):])
}
func decryptCommand() *cobra.Command {
if decryptCmd != nil {
return decryptCmd
}
decryptCmd = &cobra.Command{
Use: "decrypt",
Aliases: []string{"decr", "dec", "de", "d"},
Short: "Decrypts the encrypted data",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
decryptCmd.AddCommand(decryptTextCommand())
decryptCmd.AddCommand(decryptFileCommand())
return decryptCmd
}
func decryptTextCommand() *cobra.Command {
if decryptTxtCmd != nil {
return decryptTxtCmd
}
decryptTxtCmd = &cobra.Command{
Use: "text",
Aliases: []string{"txt", "t", "string", "str", "s"},
Short: "Decrypts a xipher encrypted text",
Run: func(cmd *cobra.Command, args []string) {
xipherText, err := fromXipherText(cmd.Flag(ciphertextFlag.name).Value.String())
if err != nil {
exitOnError(err)
}
var src, dst bytes.Buffer
src.Write(xipherText)
password, err := getPasswordFromUser(false, true)
if err != nil {
exitOnError(err)
}
privKey, err := xipher.NewPrivateKeyForPassword(password)
if err != nil {
exitOnError(err)
}
err = privKey.DecryptStream(&dst, &src)
if err != nil {
exitOnError(err)
}
fmt.Println(color.GreenString(dst.String()))
safeExit()
},
}
decryptTxtCmd.Flags().StringP(ciphertextFlag.name, ciphertextFlag.shorthand, "", ciphertextFlag.usage)
decryptTxtCmd.MarkFlagRequired(ciphertextFlag.name)
return decryptTxtCmd
}
func decryptFileCommand() *cobra.Command {
if decryptFileCmd != nil {
return decryptFileCmd
}
decryptFileCmd = &cobra.Command{
Use: "file",
Aliases: []string{"f"},
Short: "Decrypts a xipher encrypted file",
Run: func(cmd *cobra.Command, args []string) {
srcPath := cmd.Flag(fileFlag.name).Value.String()
dstPath := cmd.Flag(outFlag.name).Value.String()
// Check match between src and dst
if srcPath == dstPath {
exitOnErrorWithMessage("Source and destination paths cannot be the same.")
}
src, err := os.Open(srcPath)
if err != nil {
exitOnError(err)
}
dst, err := os.Create(dstPath)
if err != nil {
exitOnError(err)
}
password, err := getPasswordFromUser(false, true)
if err != nil {
exitOnError(err)
}
privKey, err := xipher.NewPrivateKeyForPassword(password)
if err != nil {
exitOnError(err)
}
err = privKey.DecryptStream(dst, src)
if err != nil {
exitOnError(err)
}
safeExit()
},
}
decryptFileCmd.Flags().StringP(fileFlag.name, fileFlag.shorthand, "", fileFlag.usage)
decryptFileCmd.Flags().StringP(outFlag.name, outFlag.shorthand, "", outFlag.usage)
decryptFileCmd.MarkFlagRequired(fileFlag.name)
decryptFileCmd.MarkFlagRequired(outFlag.name)
return decryptFileCmd
}