-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
177 lines (143 loc) · 3.43 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
// "context"
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
"log"
"os"
shell "github.com/ipfs/go-ipfs-api"
"github.com/urfave/cli/v2"
)
func main() {
// Where your local node is running on localhost:5001
sh := shell.NewShell("localhost:5001")
app := &cli.App{
Name: "ipfs-go-encrypt",
Usage: "Upload and download encrypted files from IPFS",
Commands: []*cli.Command{
{
Name: "upload",
Aliases: []string{"a"},
Usage: "upload a file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "key",
Value: "key",
Usage: "encyrption key (use aes-128)",
Required: true,
},
},
Action: func(cCtx *cli.Context) error {
// Get the file
file, err := os.ReadFile(cCtx.Args().First())
if err != nil {
log.Fatal(err)
}
key := []byte(cCtx.String("key"))
// Encrypt the file
encryptedFile := Encrypt(key, file)
// Upload the file
cid, err := sh.Add(bytes.NewReader(encryptedFile))
if err != nil {
log.Fatal(err)
}
fmt.Println(cid)
return nil
},
},
{
Name: "download",
Aliases: []string{"d"},
Usage: "download a file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "out",
Value: "out",
Usage: "output file",
},
&cli.StringFlag{
Name: "key",
Value: "key",
Usage: "encyrption key (use aes-128)",
Required: true,
},
},
Action: func(cCtx *cli.Context) error {
data, err := sh.Cat(cCtx.Args().First())
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
os.Exit(1)
}
// Use a buffer to read the data from the reader
buf := new(bytes.Buffer)
buf.ReadFrom(data)
key := []byte(cCtx.String("key"))
// Decrypt the data
decrypted := Decrypt(key, buf.Bytes())
// Write the data to the output file
err = os.WriteFile(cCtx.String("out"), decrypted, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
os.Exit(1)
}
return nil
},
},
{
Name: "keygen",
Aliases: []string{"k"},
Usage: "generate a key",
Action: func(cCtx *cli.Context) error {
key := make([]byte, 16)
_, err := rand.Read(key)
if err != nil {
log.Fatal(err)
}
// format key to hex (base 16)
fmt.Printf("%x\n", key)
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func Encrypt(key []byte, data []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
log.Fatalf("cipher err: %v", err.Error())
}
gcm, err := cipher.NewGCM(block)
if err != nil {
log.Fatalf("cipher GCM err: %v", err.Error())
}
nonce := make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
log.Fatalf("nonce err: %v", err.Error())
}
ciphertext := gcm.Seal(nonce, nonce, data, nil)
return ciphertext
}
func Decrypt(key []byte, data []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
log.Fatalf("cipher err: %v", err.Error())
}
gcm, err := cipher.NewGCM(block)
if err != nil {
log.Fatalf("cipher GCM err: %v", err.Error())
}
nonceSize := gcm.NonceSize()
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
log.Fatalf("cipher GCM Open err: %v", err.Error())
}
return plaintext
}