my computer is about to die!!!
This commit is contained in:
1 parent
d863dc3bd1
commit
e80f2601ab
13 files changed
+244
-11
No files matched your search
@@ -2,7 +2,8 @@ package contract
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
|
||||||
|
"github.com/didiercrunch/filou/helper"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AcceptedContract struct {
|
type AcceptedContract struct {
|
||||||
@@ -21,13 +22,6 @@ func listContains(lst []string, elm string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func joinAsError(msg []string) error {
|
|
||||||
if len(msg) > 0 {
|
|
||||||
return errors.New(strings.Join(msg, "\n"))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getFirstItemInList1ThatIsAlsoInList2(lst1, lst2 []string) (string, error) {
|
func getFirstItemInList1ThatIsAlsoInList2(lst1, lst2 []string) (string, error) {
|
||||||
for _, elm := range lst1 {
|
for _, elm := range lst1 {
|
||||||
if listContains(lst2, elm) {
|
if listContains(lst2, elm) {
|
||||||
@@ -75,5 +69,5 @@ func GetAcceptedContract(prvCtr *PrivateContract, pubCtr *PublicContract) (*Acce
|
|||||||
errs = append(errs, err.Error())
|
errs = append(errs, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret, joinAsError(errs)
|
return ret, helper.JoinAsError(errs)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package files
|
||||||
|
|
||||||
|
// package that manage the encryption of files and directory
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package files
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/didiercrunch/filou/contract"
|
||||||
|
"github.com/didiercrunch/filou/envelop"
|
||||||
|
"github.com/didiercrunch/filou/payload"
|
||||||
|
)
|
||||||
|
|
||||||
|
func EncryptFile(reader io.Reader, writer io.Writer, acceptedContract *contract.AcceptedContract) error {
|
||||||
|
payload_ := payload.GetDefaultPayload(reader)
|
||||||
|
envelop := envelop.NewEnveloper(nil, payload_, nil)
|
||||||
|
return envelop.WriteToWriter(writer)
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package files
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/didiercrunch/filou/contract"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = fmt.Print
|
||||||
|
|
||||||
|
type encryptor func(reader io.Reader, writer io.Writer, acceptedContract *contract.AcceptedContract) error
|
||||||
|
|
||||||
|
type PathEncryptor struct {
|
||||||
|
EncryptFile encryptor
|
||||||
|
EncryptDirectory encryptor
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *PathEncryptor) EncryptPath(path string, writer io.Writer, acceptedContract *contract.AcceptedContract) error {
|
||||||
|
f, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if fd, err := f.Stat(); err != nil {
|
||||||
|
return err
|
||||||
|
} else if fd.IsDir() {
|
||||||
|
return this.EncryptDirectory(f, writer, acceptedContract)
|
||||||
|
} else {
|
||||||
|
return this.EncryptFile(f, writer, acceptedContract)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package files
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/didiercrunch/filou/contract"
|
||||||
|
"github.com/didiercrunch/filou/helper"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = fmt.Print
|
||||||
|
|
||||||
|
type mockEncryptor struct {
|
||||||
|
N int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *mockEncryptor) Encrypt(reader io.Reader, writer io.Writer, acceptedContract *contract.AcceptedContract) error {
|
||||||
|
this.N += 1
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEncryptPathWrongPath(t *testing.T) {
|
||||||
|
pe := new(PathEncryptor)
|
||||||
|
|
||||||
|
filePath := "/something_that_does_not_exists"
|
||||||
|
if err := pe.EncryptPath(filePath, nil, nil); err == nil {
|
||||||
|
t.Error("should say the file does not exists")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEncryptPathForDirectory(t *testing.T) {
|
||||||
|
dirName := helper.GetTmpEmptyDir()
|
||||||
|
fileEncryptor, dirEncryptor := new(mockEncryptor), new(mockEncryptor)
|
||||||
|
pe := &PathEncryptor{fileEncryptor.Encrypt, dirEncryptor.Encrypt}
|
||||||
|
|
||||||
|
if err := pe.EncryptPath(dirName, nil, nil); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else if fileEncryptor.N != 0 {
|
||||||
|
t.Error("should have 0", fileEncryptor.N)
|
||||||
|
} else if dirEncryptor.N != 1 {
|
||||||
|
t.Error("should have 1", dirEncryptor.N)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEncryptPathForFile(t *testing.T) {
|
||||||
|
file := helper.GetTmpEmptyFile()
|
||||||
|
defer func() {
|
||||||
|
if err := file.Close(); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
fileEncryptor, dirEncryptor := new(mockEncryptor), new(mockEncryptor)
|
||||||
|
pe := &PathEncryptor{fileEncryptor.Encrypt, dirEncryptor.Encrypt}
|
||||||
|
|
||||||
|
if err := pe.EncryptPath(file.Name(), nil, nil); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else if fileEncryptor.N != 1 {
|
||||||
|
t.Error("should have 1", fileEncryptor.N)
|
||||||
|
} else if dirEncryptor.N != 0 {
|
||||||
|
t.Error("should have 0", dirEncryptor.N)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package helper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func JoinAsError(msg []string) error {
|
||||||
|
if len(msg) > 0 {
|
||||||
|
return errors.New(strings.Join(msg, "\n"))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package helper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestJoinAsError(t *testing.T) {
|
||||||
|
var errs []string = nil
|
||||||
|
if JoinAsError(errs) != nil {
|
||||||
|
t.Error("expected no errors")
|
||||||
|
}
|
||||||
|
errs = make([]string, 0)
|
||||||
|
if JoinAsError(errs) != nil {
|
||||||
|
t.Error("expected no errors")
|
||||||
|
}
|
||||||
|
|
||||||
|
errs = []string{"a", "b"}
|
||||||
|
if JoinAsError(errs).Error() != "a\nb" {
|
||||||
|
t.Error("bad error")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,8 +6,10 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"math/big"
|
"math/big"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func B(x int64) *big.Int {
|
func B(x int64) *big.Int {
|
||||||
@@ -120,3 +122,19 @@ func GetRSAPrivateKey(size int) *rsa.PrivateKey {
|
|||||||
return key
|
return key
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetTmpEmptyFile() *os.File {
|
||||||
|
if f, err := ioutil.TempFile("", ""); err != nil {
|
||||||
|
panic(err)
|
||||||
|
} else {
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTmpEmptyDir() string {
|
||||||
|
if dirName, err := ioutil.TempDir("", ""); err != nil {
|
||||||
|
panic(err)
|
||||||
|
} else {
|
||||||
|
return dirName
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,3 +45,11 @@ func (this Block) CreateBlock(rd io.Reader) (cipher.Block, []byte, error) {
|
|||||||
block, err := aes.NewCipher(key)
|
block, err := aes.NewCipher(key)
|
||||||
return block, key, err
|
return block, key, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetBlockByBlockName(blockName string) (Block, error) {
|
||||||
|
ret := Hash(hashName)
|
||||||
|
if _, err := ret.New(); err != nil {
|
||||||
|
return "", errors.New("block encryption " + blockName + " is not supported")
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
@@ -12,6 +12,14 @@ type Hash string
|
|||||||
|
|
||||||
const SHA512 Hash = "sha512"
|
const SHA512 Hash = "sha512"
|
||||||
|
|
||||||
|
func GetHashByHashName(hashName string) (Hash, error) {
|
||||||
|
ret := Hash(hashName)
|
||||||
|
if _, err := ret.New(); err != nil {
|
||||||
|
return "", errors.New("unsupported hash function " + hashName)
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (this Hash) New() (hash.Hash, error) {
|
func (this Hash) New() (hash.Hash, error) {
|
||||||
switch this {
|
switch this {
|
||||||
case SHA512:
|
case SHA512:
|
||||||
|
|||||||
+18
-2
@@ -14,8 +14,6 @@ import (
|
|||||||
"github.com/didiercrunch/filou/helper"
|
"github.com/didiercrunch/filou/helper"
|
||||||
)
|
)
|
||||||
|
|
||||||
var __ = ioutil.Discard
|
|
||||||
|
|
||||||
func TestUnsupportedHash(t *testing.T) {
|
func TestUnsupportedHash(t *testing.T) {
|
||||||
unsupportedHashes := []Hash{"md5"}
|
unsupportedHashes := []Hash{"md5"}
|
||||||
for _, h := range unsupportedHashes {
|
for _, h := range unsupportedHashes {
|
||||||
@@ -77,3 +75,21 @@ func TestHashOnTheWay(t *testing.T) {
|
|||||||
t.Error("bad hash\nexpected: ", expectedHash, "\nreceived:", outputHash)
|
t.Error("bad hash\nexpected: ", expectedHash, "\nreceived:", outputHash)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetHashByHashNameForAcceptedHashFunctions(t *testing.T) {
|
||||||
|
acceptedHashFunctions := []string{"sha512"}
|
||||||
|
for _, h := range acceptedHashFunctions {
|
||||||
|
if _, err := GetHashByHashName(h); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetHashByHashNameForNotAcceptedHashFunctions(t *testing.T) {
|
||||||
|
unaccepterHashFunctions := []string{"md5"}
|
||||||
|
for _, h := range unaccepterHashFunctions {
|
||||||
|
if _, err := GetHashByHashName(h); err == nil {
|
||||||
|
t.Error("should never accept hash function " + h)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,8 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"github.com/didiercrunch/filou/contract"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BlockModeStream func(block cipher.Block, iv []byte) cipher.Stream
|
type BlockModeStream func(block cipher.Block, iv []byte) cipher.Stream
|
||||||
@@ -32,6 +34,17 @@ func GetDefaultPayload(dataSource io.Reader) *Payload {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetPayload(dataSource io.Reader, acceptedContract *contract.AcceptedContract) (*Payload, error) {
|
||||||
|
ret := &Payload{DataSource: dataSource, Random: rand.Reader}
|
||||||
|
errs := make([]error, 0)
|
||||||
|
var err error
|
||||||
|
if ret.HashMethod, err = GetHashByHashName(acceptedContract.Hash); err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
type payloadWriter struct {
|
type payloadWriter struct {
|
||||||
Reader io.Reader
|
Reader io.Reader
|
||||||
Pipewriter io.WriteCloser
|
Pipewriter io.WriteCloser
|
||||||
|
|||||||
@@ -2,12 +2,14 @@ package payload
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/rand"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/didiercrunch/filou/contract"
|
||||||
"github.com/didiercrunch/filou/helper"
|
"github.com/didiercrunch/filou/helper"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -166,3 +168,23 @@ func TestGetDefaultPayload(t *testing.T) {
|
|||||||
errorC <- errors.New("hash has not been computed")
|
errorC <- errors.New("hash has not been computed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetPayload(t *testing.T) {
|
||||||
|
c := &contract.AcceptedContract{Hash: "sha512"}
|
||||||
|
p, err := GetPayload(nil, c)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
expectedPayload := &Payload{
|
||||||
|
DataSource: nil,
|
||||||
|
Random: rand.Reader,
|
||||||
|
HashMethod: SHA512,
|
||||||
|
Block: AES256,
|
||||||
|
BlockMode: OFB,
|
||||||
|
}
|
||||||
|
if p.HashMethod != expectedPayload.HashMethod {
|
||||||
|
t.Error("bad hash method")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user