my computer is about to die!!!

This commit is contained in:
didier.amyot committed 2014-11-01 16:00:06 -07:00
1 parent d863dc3bd1
commit e80f2601ab
13 files changed
+244 -11

No files matched your search

+8
View File
@@ -45,3 +45,11 @@ func (this Block) CreateBlock(rd io.Reader) (cipher.Block, []byte, error) {
block, err := aes.NewCipher(key)
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
}
+8
View File
@@ -12,6 +12,14 @@ type Hash string
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) {
switch this {
case SHA512:
+18 -2
View File
@@ -14,8 +14,6 @@ import (
"github.com/didiercrunch/filou/helper"
)
var __ = ioutil.Discard
func TestUnsupportedHash(t *testing.T) {
unsupportedHashes := []Hash{"md5"}
for _, h := range unsupportedHashes {
@@ -77,3 +75,21 @@ func TestHashOnTheWay(t *testing.T) {
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)
}
}
}
+13
View File
@@ -6,6 +6,8 @@ import (
"crypto/rand"
"encoding/json"
"io"
"github.com/didiercrunch/filou/contract"
)
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 {
Reader io.Reader
Pipewriter io.WriteCloser
+22
View File
@@ -2,12 +2,14 @@ package payload
import (
"bytes"
"crypto/rand"
"errors"
"fmt"
"io/ioutil"
"reflect"
"testing"
"github.com/didiercrunch/filou/contract"
"github.com/didiercrunch/filou/helper"
)
@@ -166,3 +168,23 @@ func TestGetDefaultPayload(t *testing.T) {
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")
}
}