my computer will still die soon
This commit is contained in:
1 parent
e80f2601ab
commit
52a13b9cab
7 files changed
+62
-6
No files matched your search
@@ -11,3 +11,13 @@ func JoinAsError(msg []string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func JoinErrors(errs []error) error {
|
||||||
|
msgs := make([]string, 0, len(errs))
|
||||||
|
for _, err := range errs {
|
||||||
|
if err != nil {
|
||||||
|
msgs = append(msgs, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return JoinAsError(msgs)
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package helper
|
package helper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,3 +20,19 @@ func TestJoinAsError(t *testing.T) {
|
|||||||
t.Error("bad error")
|
t.Error("bad error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestJoinErrors(t *testing.T) {
|
||||||
|
var errs []error = nil
|
||||||
|
if JoinErrors(errs) != nil {
|
||||||
|
t.Error("expected no errors")
|
||||||
|
}
|
||||||
|
errs = make([]error, 0)
|
||||||
|
if JoinErrors(errs) != nil {
|
||||||
|
t.Error("expected no errors")
|
||||||
|
}
|
||||||
|
|
||||||
|
errs = []error{errors.New("a"), errors.New("b")}
|
||||||
|
if JoinErrors(errs).Error() != "a\nb" {
|
||||||
|
t.Error("bad error")
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
-3
@@ -47,9 +47,11 @@ func (this Block) CreateBlock(rd io.Reader) (cipher.Block, []byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetBlockByBlockName(blockName string) (Block, error) {
|
func GetBlockByBlockName(blockName string) (Block, error) {
|
||||||
ret := Hash(hashName)
|
ret := Block(blockName)
|
||||||
if _, err := ret.New(); err != nil {
|
switch ret {
|
||||||
|
case AES256:
|
||||||
|
return ret, nil
|
||||||
|
default:
|
||||||
return "", errors.New("block encryption " + blockName + " is not supported")
|
return "", errors.New("block encryption " + blockName + " is not supported")
|
||||||
}
|
}
|
||||||
return ret, nil
|
|
||||||
}
|
}
|
||||||
@@ -43,3 +43,13 @@ func (this BlockMode) GetCipherStream(block cipher.Block, rd io.Reader) (stream
|
|||||||
}
|
}
|
||||||
return streamCreator(block, iv), iv, nil
|
return streamCreator(block, iv), iv, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetBlockModeByName(blockModeName string) (BlockMode, error) {
|
||||||
|
ret := BlockMode(blockModeName)
|
||||||
|
switch ret {
|
||||||
|
case OFB:
|
||||||
|
return ret, nil
|
||||||
|
default:
|
||||||
|
return "", errors.New("block mode " + blockModeName + " is not supported")
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -25,7 +25,7 @@ func (this Hash) New() (hash.Hash, error) {
|
|||||||
case SHA512:
|
case SHA512:
|
||||||
return cryptoSHA512.New(), nil
|
return cryptoSHA512.New(), nil
|
||||||
default:
|
default:
|
||||||
msg := fmt.Sprintf("hash '%s' is not supported")
|
msg := fmt.Sprintf("hash '%s' is not supported", this)
|
||||||
return nil, errors.New(msg)
|
return nil, errors.New(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-1
@@ -8,6 +8,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/didiercrunch/filou/contract"
|
"github.com/didiercrunch/filou/contract"
|
||||||
|
"github.com/didiercrunch/filou/helper"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BlockModeStream func(block cipher.Block, iv []byte) cipher.Stream
|
type BlockModeStream func(block cipher.Block, iv []byte) cipher.Stream
|
||||||
@@ -41,8 +42,14 @@ func GetPayload(dataSource io.Reader, acceptedContract *contract.AcceptedContrac
|
|||||||
if ret.HashMethod, err = GetHashByHashName(acceptedContract.Hash); err != nil {
|
if ret.HashMethod, err = GetHashByHashName(acceptedContract.Hash); err != nil {
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
}
|
}
|
||||||
|
if ret.Block, err = GetBlockByBlockName(acceptedContract.BlockCipher); err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
if ret.BlockMode, err = GetBlockModeByName(acceptedContract.BlockCipherMode); err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
|
||||||
return ret, nil
|
return ret, helper.JoinErrors(errs)
|
||||||
}
|
}
|
||||||
|
|
||||||
type payloadWriter struct {
|
type payloadWriter struct {
|
||||||
|
|||||||
+11
-1
@@ -170,7 +170,11 @@ func TestGetDefaultPayload(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetPayload(t *testing.T) {
|
func TestGetPayload(t *testing.T) {
|
||||||
c := &contract.AcceptedContract{Hash: "sha512"}
|
c := &contract.AcceptedContract{
|
||||||
|
Hash: "sha512",
|
||||||
|
BlockCipher: "aes256",
|
||||||
|
BlockCipherMode: "ofb",
|
||||||
|
}
|
||||||
p, err := GetPayload(nil, c)
|
p, err := GetPayload(nil, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
@@ -186,5 +190,11 @@ func TestGetPayload(t *testing.T) {
|
|||||||
if p.HashMethod != expectedPayload.HashMethod {
|
if p.HashMethod != expectedPayload.HashMethod {
|
||||||
t.Error("bad hash method")
|
t.Error("bad hash method")
|
||||||
}
|
}
|
||||||
|
if p.Block != expectedPayload.Block {
|
||||||
|
t.Error("bad block method", p.Block)
|
||||||
|
}
|
||||||
|
if p.BlockMode != expectedPayload.BlockMode {
|
||||||
|
t.Error("bad block method", p.Block)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user