[lib.scha] made 'Hash()' salt and pepper arguments optional

This commit is contained in:
xdrm-brackets 2018-04-22 20:58:37 +02:00
parent 18dc9c786a
commit 96a9dbe785
5 changed files with 29 additions and 16 deletions

View File

@ -232,7 +232,7 @@ func (c *T) generateRequest(x1 []byte, x2 []byte) error {
binary.BigEndian.PutUint32(timeIdBytes, timeId) binary.BigEndian.PutUint32(timeIdBytes, timeId)
/* (2) Get digest of time id */ /* (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 /* (5) Calculate x1 and x2
---------------------------------------------------------*/ ---------------------------------------------------------*/
@ -293,7 +293,7 @@ func (c *T) rescue(y1 []byte, y2 []byte) error {
binary.BigEndian.PutUint32(timeIdBytes, timeId) binary.BigEndian.PutUint32(timeIdBytes, timeId)
/* (3) Hash timeId */ /* (3) Hash timeId */
hashedTimeId, err := scha.Hash(timeIdBytes, 1, nil, nil) hashedTimeId, err := scha.Hash(timeIdBytes, 1)
if err != nil { if err != nil {
return err return err
} }

View File

@ -57,7 +57,7 @@ func Create(ctx *context.T) (*T, error) {
func (s T) CurrentHash() ([]byte, error) { func (s T) CurrentHash() ([]byte, error) {
/* (1) Get digest */ /* (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 */ /* (2) Dispatch error */
if err != nil { if err != nil {

View File

@ -64,7 +64,7 @@ func TestSchaDecrementingProperty(t *testing.T) {
} }
/* (4) Try to guess h1 from h2 */ /* (4) Try to guess h1 from h2 */
hcheck, err = scha.Hash(h2, 1, nil, nil) hcheck, err = scha.Hash(h2, 1)
if err != nil { if err != nil {
t.Errorf("Do not expected an error, got: %s", err) 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 */ /* (4) Try to guess h1 from h2 */
hcheck, err = scha.Hash(h2, 1, nil, nil) hcheck, err = scha.Hash(h2, 1)
if err != nil { if err != nil {
t.Errorf("Do not expected an error, got: %s", err) t.Errorf("Do not expected an error, got: %s", err)
} }

View File

@ -42,12 +42,15 @@ func hash(input []byte) []byte{
/* (2) Public hashing interface /* (2) Public hashing interface
* *
* @input<[]byte> Byte array input * @input<[]byte> Byte array input
* @depth<uint> 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 digest<[]byte]> Byte array digest
* @return err<error> If consistence error * @return err<error> 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 /* (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 */ /* (1) Initialise digest */
digest := make([]byte, 0, HSIZE) digest := make([]byte, 0, HSIZE)

View File

@ -11,7 +11,7 @@ func TestSimpleHash(t *testing.T){
0x47, 0x06, 0xf3, 0x49, 0x30, 0x8a, 0x90, 0x8e, 0xd2, 0xff, 0xc2, 0x6d, 0xee, 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, 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}; 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)); digestLength := uint(len(got));
/* (2) Fail on errors */ /* (2) Fail on errors */
@ -43,7 +43,7 @@ func TestDepth1Plus1Equals2(t *testing.T){
input := []byte("someOtherPlainText"); input := []byte("someOtherPlainText");
/* (1) Calculate H1 */ /* (1) Calculate H1 */
h1, err := Hash(input, 1, nil, nil) h1, err := Hash(input, 1)
if err != nil { if err != nil {
t.Errorf("Expected no error"); t.Errorf("Expected no error");
@ -51,7 +51,7 @@ func TestDepth1Plus1Equals2(t *testing.T){
} }
/* (2) Calculate H1(H1) */ /* (2) Calculate H1(H1) */
h11, err := Hash(h1, 1, nil, nil); h11, err := Hash(h1, 1);
if err != nil { if err != nil {
t.Errorf("Expected no error"); t.Errorf("Expected no error");
@ -59,7 +59,7 @@ func TestDepth1Plus1Equals2(t *testing.T){
} }
/* (3) Calculate H2 */ /* (3) Calculate H2 */
h2, err := Hash(input, 1+1, nil, nil); h2, err := Hash(input, 1+1);
if err != nil { if err != nil {
t.Errorf("Expected no error"); t.Errorf("Expected no error");
@ -89,7 +89,7 @@ func TestDepth52Plus64Equals116(t *testing.T){
input := []byte("someOtherPlainText"); input := []byte("someOtherPlainText");
/* (1) Calculate H52 */ /* (1) Calculate H52 */
h52, err := Hash(input, 52, nil, nil) h52, err := Hash(input, 52)
if err != nil { if err != nil {
t.Errorf("Expected no error"); t.Errorf("Expected no error");
@ -97,7 +97,7 @@ func TestDepth52Plus64Equals116(t *testing.T){
} }
/* (2) Calculate H52(H64) */ /* (2) Calculate H52(H64) */
h5264, err := Hash(h52, 64, nil, nil); h5264, err := Hash(h52, 64);
if err != nil { if err != nil {
t.Errorf("Expected no error"); t.Errorf("Expected no error");
@ -105,7 +105,7 @@ func TestDepth52Plus64Equals116(t *testing.T){
} }
/* (3) Calculate H116 */ /* (3) Calculate H116 */
h116, err := Hash(input, 52+64, nil, nil); h116, err := Hash(input, 52+64);
if err != nil { if err != nil {
t.Errorf("Expected no error"); t.Errorf("Expected no error");
@ -133,7 +133,7 @@ func TestDepth52Plus64Equals116(t *testing.T){
func TestDepthError(t *testing.T){ func TestDepthError(t *testing.T){
input := []byte("somePlainText"); input := []byte("somePlainText");
_, err := Hash(input, 0, nil, nil); _, err := Hash(input, 0);
/* (2) Fail on errors */ /* (2) Fail on errors */
if err == nil { if err == nil {
@ -146,7 +146,7 @@ func TestDepthError(t *testing.T){
func TestEmptyInputError(t *testing.T){ func TestEmptyInputError(t *testing.T){
_, err := Hash(nil, 1, nil, nil); _, err := Hash(nil, 1);
/* (2) Fail on errors */ /* (2) Fail on errors */
if err == nil { if err == nil {