From 96a9dbe785eb70cb3b883a349766d23e0599b1a0 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 22 Apr 2018 20:58:37 +0200 Subject: [PATCH] [lib.scha] made 'Hash()' salt and pepper arguments optional --- .../schastsp/client/client.internal.go | 4 ++-- .../schastsp/client/keyset/keyset.go | 2 +- .../schastsp/client/keyset/keyset_test.go | 4 ++-- src/git.xdrm.io/schastsp/lib/scha/hash.go | 17 +++++++++++++++-- src/git.xdrm.io/schastsp/lib/scha/hash_test.go | 18 +++++++++--------- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/git.xdrm.io/schastsp/client/client.internal.go b/src/git.xdrm.io/schastsp/client/client.internal.go index 16ccfb2..b3c11c3 100644 --- a/src/git.xdrm.io/schastsp/client/client.internal.go +++ b/src/git.xdrm.io/schastsp/client/client.internal.go @@ -232,7 +232,7 @@ func (c *T) generateRequest(x1 []byte, x2 []byte) error { binary.BigEndian.PutUint32(timeIdBytes, timeId) /* (2) Get digest of time id */ - hashedTimeId, err := scha.Hash(timeIdBytes, 1, nil, nil) + hashedTimeId, err := scha.Hash(timeIdBytes, 1) /* (5) Calculate x1 and x2 ---------------------------------------------------------*/ @@ -293,7 +293,7 @@ func (c *T) rescue(y1 []byte, y2 []byte) error { binary.BigEndian.PutUint32(timeIdBytes, timeId) /* (3) Hash timeId */ - hashedTimeId, err := scha.Hash(timeIdBytes, 1, nil, nil) + hashedTimeId, err := scha.Hash(timeIdBytes, 1) if err != nil { return err } diff --git a/src/git.xdrm.io/schastsp/client/keyset/keyset.go b/src/git.xdrm.io/schastsp/client/keyset/keyset.go index 22c5565..583fd7f 100644 --- a/src/git.xdrm.io/schastsp/client/keyset/keyset.go +++ b/src/git.xdrm.io/schastsp/client/keyset/keyset.go @@ -57,7 +57,7 @@ func Create(ctx *context.T) (*T, error) { func (s T) CurrentHash() ([]byte, error) { /* (1) Get digest */ - digest, err := scha.Hash(s.sec, uint(s.depth), nil, nil) + digest, err := scha.Hash(s.sec, uint(s.depth)) /* (2) Dispatch error */ if err != nil { diff --git a/src/git.xdrm.io/schastsp/client/keyset/keyset_test.go b/src/git.xdrm.io/schastsp/client/keyset/keyset_test.go index c3b38dd..2a760be 100644 --- a/src/git.xdrm.io/schastsp/client/keyset/keyset_test.go +++ b/src/git.xdrm.io/schastsp/client/keyset/keyset_test.go @@ -64,7 +64,7 @@ func TestSchaDecrementingProperty(t *testing.T) { } /* (4) Try to guess h1 from h2 */ - hcheck, err = scha.Hash(h2, 1, nil, nil) + hcheck, err = scha.Hash(h2, 1) if err != nil { t.Errorf("Do not expected an error, got: %s", err) } @@ -113,7 +113,7 @@ func TestDecrementMinimum(t *testing.T) { } /* (4) Try to guess h1 from h2 */ - hcheck, err = scha.Hash(h2, 1, nil, nil) + hcheck, err = scha.Hash(h2, 1) if err != nil { t.Errorf("Do not expected an error, got: %s", err) } diff --git a/src/git.xdrm.io/schastsp/lib/scha/hash.go b/src/git.xdrm.io/schastsp/lib/scha/hash.go index eadbca6..975e783 100644 --- a/src/git.xdrm.io/schastsp/lib/scha/hash.go +++ b/src/git.xdrm.io/schastsp/lib/scha/hash.go @@ -42,12 +42,15 @@ func hash(input []byte) []byte{ /* (2) Public hashing interface * * @input<[]byte> Byte array input +* @depth Number of time to hash recursively (must be > 1) +* @salt<[]byte> [OPT] Optional salt +* @pepper<[]byte> [OPT] Optional pepper * * @return digest<[]byte]> Byte array digest * @return err If consistence error * ---------------------------------------------------------*/ -func Hash(input []byte, depth uint, salt []byte, pepper []byte) ([]byte, error) { +func Hash(input []byte, depth uint, options... []byte) ([]byte, error) { /* (1) Manage errors errors @@ -63,8 +66,18 @@ func Hash(input []byte, depth uint, salt []byte, pepper []byte) ([]byte, error) } + /* (2) Extract optional arguments + ---------------------------------------------------------*/ + /* (1) Optional salt */ + salt := make([]byte, 0) + if len(options) > 0 { salt = options[0] } - /* (2) Process cyclic hash + /* (2) Optional pepper */ + pepper := make([]byte, 0) + if len(options) > 0 { pepper = options[0] } + + + /* (3) Process cyclic hash ---------------------------------------------------------*/ /* (1) Initialise digest */ digest := make([]byte, 0, HSIZE) diff --git a/src/git.xdrm.io/schastsp/lib/scha/hash_test.go b/src/git.xdrm.io/schastsp/lib/scha/hash_test.go index daa6bb7..65c50b2 100644 --- a/src/git.xdrm.io/schastsp/lib/scha/hash_test.go +++ b/src/git.xdrm.io/schastsp/lib/scha/hash_test.go @@ -11,7 +11,7 @@ func TestSimpleHash(t *testing.T){ 0x47, 0x06, 0xf3, 0x49, 0x30, 0x8a, 0x90, 0x8e, 0xd2, 0xff, 0xc2, 0x6d, 0xee, 0xaa, 0xd6, 0x45, 0xd8, 0xb3, 0x17, 0xe3, 0xb9, 0x45, 0x29, 0x26, 0xe2, 0x8e, 0x99, 0x50, 0x94, 0x49, 0x90, 0x02, 0xa5, 0x61, 0x4a, 0x3f, 0x5e, 0xfa}; - got, err := Hash(input, 1, nil, nil); + got, err := Hash(input, 1); digestLength := uint(len(got)); /* (2) Fail on errors */ @@ -43,7 +43,7 @@ func TestDepth1Plus1Equals2(t *testing.T){ input := []byte("someOtherPlainText"); /* (1) Calculate H1 */ - h1, err := Hash(input, 1, nil, nil) + h1, err := Hash(input, 1) if err != nil { t.Errorf("Expected no error"); @@ -51,7 +51,7 @@ func TestDepth1Plus1Equals2(t *testing.T){ } /* (2) Calculate H1(H1) */ - h11, err := Hash(h1, 1, nil, nil); + h11, err := Hash(h1, 1); if err != nil { t.Errorf("Expected no error"); @@ -59,7 +59,7 @@ func TestDepth1Plus1Equals2(t *testing.T){ } /* (3) Calculate H2 */ - h2, err := Hash(input, 1+1, nil, nil); + h2, err := Hash(input, 1+1); if err != nil { t.Errorf("Expected no error"); @@ -89,7 +89,7 @@ func TestDepth52Plus64Equals116(t *testing.T){ input := []byte("someOtherPlainText"); /* (1) Calculate H52 */ - h52, err := Hash(input, 52, nil, nil) + h52, err := Hash(input, 52) if err != nil { t.Errorf("Expected no error"); @@ -97,7 +97,7 @@ func TestDepth52Plus64Equals116(t *testing.T){ } /* (2) Calculate H52(H64) */ - h5264, err := Hash(h52, 64, nil, nil); + h5264, err := Hash(h52, 64); if err != nil { t.Errorf("Expected no error"); @@ -105,7 +105,7 @@ func TestDepth52Plus64Equals116(t *testing.T){ } /* (3) Calculate H116 */ - h116, err := Hash(input, 52+64, nil, nil); + h116, err := Hash(input, 52+64); if err != nil { t.Errorf("Expected no error"); @@ -133,7 +133,7 @@ func TestDepth52Plus64Equals116(t *testing.T){ func TestDepthError(t *testing.T){ input := []byte("somePlainText"); - _, err := Hash(input, 0, nil, nil); + _, err := Hash(input, 0); /* (2) Fail on errors */ if err == nil { @@ -146,7 +146,7 @@ func TestDepthError(t *testing.T){ func TestEmptyInputError(t *testing.T){ - _, err := Hash(nil, 1, nil, nil); + _, err := Hash(nil, 1); /* (2) Fail on errors */ if err == nil {