no fucking idea
This commit is contained in:
1 parent
4aca3324c9
commit
7a5c9c79a5
4 files changed
+77
-10
No files matched your search
+1
-1
@@ -42,7 +42,7 @@ else than a zip file containing the following shit.
|
|||||||
| | Paiload: Zip file, no compression, symetrcally encrypted
|
| | Paiload: Zip file, no compression, symetrcally encrypted
|
||||||
| | _____________________________________________________________________
|
| | _____________________________________________________________________
|
||||||
| | |
|
| | |
|
||||||
| | | paiload intern metadata: Data signature, sender, message,...
|
| | | paiload intern metadata: Data signature, sender, message, IV, ...
|
||||||
| | |______________________________________________________________________
|
| | |______________________________________________________________________
|
||||||
| |
|
| |
|
||||||
| | _______________________________________________________________________
|
| | _______________________________________________________________________
|
||||||
|
|||||||
+6
-1
@@ -95,7 +95,12 @@ func (this *Enveloper) WriteToWriter(w io.Writer) error {
|
|||||||
defer z.Close()
|
defer z.Close()
|
||||||
if err := this.WritePayload(z); err != nil {
|
if err := this.WritePayload(z); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if err := this.WriteMetadata(z); err != nil {
|
}
|
||||||
|
if err := this.CreateEnvelopCompletely(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := this.WriteMetadata(z); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if err := this.WriteEncryptedKey(z); err != nil {
|
} else if err := this.WriteEncryptedKey(z); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
+69
-7
@@ -3,38 +3,82 @@ package envelop
|
|||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/didiercrunch/ecrypto/helper"
|
"github.com/didiercrunch/ecrypto/helper"
|
||||||
"io"
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mockPayload struct{}
|
var _ = fmt.Print
|
||||||
|
|
||||||
|
type mockPayload struct {
|
||||||
|
MethodsCall []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMockPayload() *mockPayload {
|
||||||
|
return &mockPayload{make([]string, 0)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *mockPayload) addMethodCall(methodName string) {
|
||||||
|
this.MethodsCall = append(this.MethodsCall, methodName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// assert firstToBeCalled is called before secondToBeCalled. Calls can be made
|
||||||
|
// between them and defore firstToBeCalled and after secondToBeCalled.
|
||||||
|
func (this *mockPayload) assertMethodCallOrder(firstToBeCalled, secondToBeCalled string) error {
|
||||||
|
var firstToBeCalledIdx, secondToBeCalledIdx = -1, -1
|
||||||
|
for i, name := range this.MethodsCall {
|
||||||
|
if firstToBeCalledIdx == -1 && name == firstToBeCalled {
|
||||||
|
firstToBeCalledIdx = i
|
||||||
|
}
|
||||||
|
if secondToBeCalledIdx == -1 && name == secondToBeCalled {
|
||||||
|
secondToBeCalledIdx = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if firstToBeCalledIdx == -1 {
|
||||||
|
return errors.New(firstToBeCalled + " is never called")
|
||||||
|
}
|
||||||
|
if secondToBeCalledIdx == -1 {
|
||||||
|
return errors.New(secondToBeCalled + " is never called")
|
||||||
|
}
|
||||||
|
if firstToBeCalledIdx >= secondToBeCalledIdx {
|
||||||
|
return errors.New(firstToBeCalled + " is called after " + secondToBeCalled)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (this *mockPayload) GetKey() []byte {
|
func (this *mockPayload) GetKey() []byte {
|
||||||
|
this.addMethodCall("GetKey")
|
||||||
return []byte{1, 2, 3}
|
return []byte{1, 2, 3}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *mockPayload) GetHash() []byte {
|
func (this *mockPayload) GetHash() []byte {
|
||||||
|
this.addMethodCall("GetHash")
|
||||||
return []byte{10, 9, 8}
|
return []byte{10, 9, 8}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *mockPayload) GetAlgorithm() string {
|
func (this *mockPayload) GetAlgorithm() string {
|
||||||
|
this.addMethodCall("GetAlgorithm")
|
||||||
symetricEncryptionAlgorithm := "DES"
|
symetricEncryptionAlgorithm := "DES"
|
||||||
return symetricEncryptionAlgorithm
|
return symetricEncryptionAlgorithm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *mockPayload) GetMode() string {
|
func (this *mockPayload) GetMode() string {
|
||||||
|
this.addMethodCall("GetMode")
|
||||||
symetricEncryptionMode := "OFB"
|
symetricEncryptionMode := "OFB"
|
||||||
return symetricEncryptionMode
|
return symetricEncryptionMode
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *mockPayload) GetHashMethod() string {
|
func (this *mockPayload) GetHashMethod() string {
|
||||||
|
this.addMethodCall("GetHashMethod")
|
||||||
theHashMethodUsedInThePayLoad := "SHA1"
|
theHashMethodUsedInThePayLoad := "SHA1"
|
||||||
return theHashMethodUsedInThePayLoad
|
return theHashMethodUsedInThePayLoad
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *mockPayload) GetPayloadData() io.Reader {
|
func (this *mockPayload) GetPayloadData() io.Reader {
|
||||||
|
this.addMethodCall("GetPayloadData")
|
||||||
someData := `some data must be here. In reality, the data
|
someData := `some data must be here. In reality, the data
|
||||||
would be the encrypted payload. This will be
|
would be the encrypted payload. This will be
|
||||||
the big chunk of the data.
|
the big chunk of the data.
|
||||||
@@ -67,7 +111,7 @@ func (this *mockSigner) Sign(data []byte) ([]byte, error) {
|
|||||||
func TestEncryptKey(t *testing.T) {
|
func TestEncryptKey(t *testing.T) {
|
||||||
env := &Enveloper{
|
env := &Enveloper{
|
||||||
encryptor: &mockEncryptor{},
|
encryptor: &mockEncryptor{},
|
||||||
payload: &mockPayload{},
|
payload: newMockPayload(),
|
||||||
}
|
}
|
||||||
if enc, err := env.EncryptKey(); err != nil {
|
if enc, err := env.EncryptKey(); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
@@ -79,7 +123,7 @@ func TestEncryptKey(t *testing.T) {
|
|||||||
func TestCreateEnvelopCompletelyEncryptKey(t *testing.T) {
|
func TestCreateEnvelopCompletelyEncryptKey(t *testing.T) {
|
||||||
env := &Enveloper{
|
env := &Enveloper{
|
||||||
encryptor: &mockEncryptor{},
|
encryptor: &mockEncryptor{},
|
||||||
payload: &mockPayload{},
|
payload: newMockPayload(),
|
||||||
signer: &mockSigner{},
|
signer: &mockSigner{},
|
||||||
}
|
}
|
||||||
if err := env.CreateEnvelopCompletely(); err != nil {
|
if err := env.CreateEnvelopCompletely(); err != nil {
|
||||||
@@ -104,7 +148,7 @@ func TestSignPayload(t *testing.T) {
|
|||||||
|
|
||||||
func TestCreateEnvelopCompletelySignHash(t *testing.T) {
|
func TestCreateEnvelopCompletelySignHash(t *testing.T) {
|
||||||
env := &Enveloper{
|
env := &Enveloper{
|
||||||
payload: &mockPayload{},
|
payload: newMockPayload(),
|
||||||
encryptor: &mockEncryptor{},
|
encryptor: &mockEncryptor{},
|
||||||
signer: &mockSigner{},
|
signer: &mockSigner{},
|
||||||
}
|
}
|
||||||
@@ -117,7 +161,7 @@ func TestCreateEnvelopCompletelySignHash(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateMetadata(t *testing.T) {
|
func TestCreateMetadata(t *testing.T) {
|
||||||
env := &Enveloper{payload: &mockPayload{}}
|
env := &Enveloper{payload: newMockPayload()}
|
||||||
exptectedMetaData := &Metadata{
|
exptectedMetaData := &Metadata{
|
||||||
BlockAlgorithm: "DES",
|
BlockAlgorithm: "DES",
|
||||||
BlockMode: "OFB",
|
BlockMode: "OFB",
|
||||||
@@ -133,7 +177,7 @@ func TestCreateMetadata(t *testing.T) {
|
|||||||
func TestCreateEnvelopCompletelyMetadata(t *testing.T) {
|
func TestCreateEnvelopCompletelyMetadata(t *testing.T) {
|
||||||
env := &Enveloper{
|
env := &Enveloper{
|
||||||
encryptor: &mockEncryptor{},
|
encryptor: &mockEncryptor{},
|
||||||
payload: &mockPayload{},
|
payload: newMockPayload(),
|
||||||
signer: &mockSigner{},
|
signer: &mockSigner{},
|
||||||
}
|
}
|
||||||
if err := env.CreateEnvelopCompletely(); err != nil {
|
if err := env.CreateEnvelopCompletely(); err != nil {
|
||||||
@@ -146,7 +190,9 @@ func TestCreateEnvelopCompletelyMetadata(t *testing.T) {
|
|||||||
|
|
||||||
func TestWriteToWriter(t *testing.T) {
|
func TestWriteToWriter(t *testing.T) {
|
||||||
env := &Enveloper{
|
env := &Enveloper{
|
||||||
payload: &mockPayload{},
|
payload: newMockPayload(),
|
||||||
|
signer: &mockSigner{},
|
||||||
|
encryptor: &mockEncryptor{},
|
||||||
}
|
}
|
||||||
w := helper.NewMockIoWriter()
|
w := helper.NewMockIoWriter()
|
||||||
if err := env.WriteToWriter(w); err != nil {
|
if err := env.WriteToWriter(w); err != nil {
|
||||||
@@ -161,3 +207,19 @@ func TestWriteToWriter(t *testing.T) {
|
|||||||
t.Error("wrong file number")
|
t.Error("wrong file number")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMethodOrder(t *testing.T) {
|
||||||
|
mockPayload := newMockPayload()
|
||||||
|
env := &Enveloper{
|
||||||
|
payload: mockPayload,
|
||||||
|
signer: &mockSigner{},
|
||||||
|
encryptor: &mockEncryptor{},
|
||||||
|
}
|
||||||
|
w := helper.NewMockIoWriter()
|
||||||
|
if err := env.WriteToWriter(w); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if err := mockPayload.assertMethodCallOrder("GetPayloadData", "GetHashMethod"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -23,7 +23,7 @@ type Payload struct {
|
|||||||
DataSource io.Reader
|
DataSource io.Reader
|
||||||
Hash crypto.Hash
|
Hash crypto.Hash
|
||||||
Block cipher.Block
|
Block cipher.Block
|
||||||
BlockMode cipher.BlockMode
|
BlockMode cipher.Stream
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Payload) GetHashMethod() string {
|
func (this *Payload) GetHashMethod() string {
|
||||||
|
|||||||
Reference in new issue
Block a user