-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
146 lines (108 loc) · 2.63 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
package main
import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"time"
"github.com/briandowns/spinner"
"github.com/keybase/go-triplesec"
"github.com/vincent-petithory/dataurl"
)
// Upload structure
type Upload struct {
URL string `json:"url"`
Name string `json:"name"`
}
// Response of API Upload
type Response struct {
ID string `json:"id"`
}
// Generate a random hex string
func generateRandomString(size int) (string, error) {
bytes := make([]byte, size)
_, err := rand.Read(bytes)
return hex.EncodeToString(bytes), err
}
// Encrypt the file
func encryptFile(name string, data []byte, key string) (string, error) {
var encodedStr string
cipher, err := triplesec.NewCipher([]byte(key), nil)
if err != nil {
return encodedStr, err
}
dataURL := dataurl.EncodeBytes(data)
upload := &Upload{
URL: dataURL,
Name: name}
json, _ := json.Marshal(upload)
var encrypted []byte
encrypted, err = cipher.Encrypt(json)
if err != nil {
return encodedStr, err
}
encodedStr = hex.EncodeToString(encrypted)
return encodedStr, nil
}
// Uploaded the encrypted data to the API
func uploadFile(encryptedData string) (Response, error) {
var response Response
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
fileWriter, err := bodyWriter.CreateFormFile("upload", "encrypted")
if err != nil {
return response, err
}
fileWriter.Write([]byte(encryptedData))
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
resp, err := http.Post("https://api.sendsh.it/upload", contentType, bodyBuf)
if err != nil {
return response, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return response, err
}
json.Unmarshal(body, &response)
return response, nil
}
func main() {
path := os.Args[1]
file, err := ioutil.ReadFile(path)
spinner := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
spinner.Start()
spinner.Suffix = " Encrypting some shit..."
spinner.Writer = os.Stderr
if err != nil {
spinner.FinalMSG = "Couldn't read that shit"
spinner.Stop()
os.Exit(1)
}
key, err := generateRandomString(24)
if err != nil {
spinner.FinalMSG = "Couldn't generate a key"
spinner.Stop()
os.Exit(1)
}
encodedStr, err := encryptFile(path, file, key)
if err != nil {
spinner.FinalMSG = "Couldn't encrypt that shit"
spinner.Stop()
os.Exit(1)
}
response, err := uploadFile(encodedStr)
if err != nil {
spinner.FinalMSG = "Couldn't upload that shit"
spinner.Stop()
os.Exit(1)
}
spinner.Stop()
fmt.Printf("https://sendsh.it/#/%s/%s\n", response.ID, key)
}