Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Issue 4 #8

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ var (
// The proof is issued too far into the future.
ErrFuture = errors.New("proof is issued too far into the future")

// The hash function is not available
ErrHashFnNotAvailable = errors.New("provided hash function is not available, please check binaries and linker")

// When using hash util func, errors
// if more than one hash is provided in variadic args
ErrTooManyArgs = errors.New("too many arguments for function")


// The proof claims are not of correct type
ErrIncorrectClaimsType = errors.New("incorrect claims type")

Expand Down
56 changes: 56 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package dpop

import (
"crypto"
"encoding/hex"
)

// type MockHash struct {}

// func NewMockHash() hash.Hash {
// return &MockHash{}
// }

// func (m *MockHash) Write(p []byte) (n int, err error) {
// return 0, nil
// }

// func (m *MockHash) Sum(b []byte) []byte {
// return nil
// }
zg009 marked this conversation as resolved.
Show resolved Hide resolved

// func (m *MockHash) Reset() {}

// func (m *MockHash) Size() int {
// return 0
// }

// func (m *MockHash) BlockSize() int {
// return 0
// }

// func (m *MockHash) Available() bool {
// return false
// }

zg009 marked this conversation as resolved.
Show resolved Hide resolved
func HashEquals(inString string, encryptedString string, args ...crypto.Hash) (bool, error) {
var hashFn crypto.Hash
if len(args) > 1 {
return false, ErrTooManyArgs
} else if len(args) < 1 {
zg009 marked this conversation as resolved.
Show resolved Hide resolved
hashFn = crypto.SHA256
} else {
hashFn = args[0]
}
if !hashFn.Available() {
return false, ErrHashFnNotAvailable
}
hashFnInst := hashFn.HashFunc().New()
zg009 marked this conversation as resolved.
Show resolved Hide resolved
// helped me here https://forum.golangbridge.org/t/help-with-sha256-code-solved/8210/4
zg009 marked this conversation as resolved.
Show resolved Hide resolved
firstBytes, _ := hex.DecodeString(inString)
zg009 marked this conversation as resolved.
Show resolved Hide resolved
hashFnInst.Write(firstBytes)
zg009 marked this conversation as resolved.
Show resolved Hide resolved
zg009 marked this conversation as resolved.
Show resolved Hide resolved

res := hashFnInst.Sum(nil)
zg009 marked this conversation as resolved.
Show resolved Hide resolved
out := hex.EncodeToString(res)
zg009 marked this conversation as resolved.
Show resolved Hide resolved
return (encryptedString == out), nil
}
73 changes: 73 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package dpop_test

import (
"crypto"
"testing"

"github.com/AxisCommunications/go-dpop"
)


const (
// Arrange
inString = "testString"
encryptedString = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
)

func TestHashEq_TooManyArgs(t *testing.T) {

// Act
_, err := dpop.HashEquals(inString, encryptedString, crypto.Hash(crypto.SHA224), crypto.Hash(crypto.SHA3_384))

// Assert
if err != dpop.ErrTooManyArgs {
t.Errorf("wanted %e, got %e", dpop.ErrTooManyArgs, err)
}
}

func TestHashEq_NoArgs(t *testing.T) {
// Act
got, _ := dpop.HashEquals(inString, encryptedString)

// Assert
if got != true {
t.Errorf("wanted %t, got %t", true, got)
}
}

func TestHashEq_CustomCryptoArg(t *testing.T) {
// Arrange
outString := `38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b`
// Act
got, _ := dpop.HashEquals(inString, outString, crypto.SHA384)

// Assert
if got != true {
t.Errorf("wanted %t, got %t", true, got)
}
}

func TestHashEq_IncorrectEncryptedString(t *testing.T) {
// Arrange
outString := "this is definitely wrong : )"

// Act
got, _ := dpop.HashEquals(inString, outString)

// Assert
if got != false {
t.Errorf("wanted %t, got %t", false, got)
}
}

// func TestHashEq_AlgNotAvailable(t *testing.T) {

// crypto.RegisterHash(99, dpop.NewMockHash)
// // Act
// _, err := dpop.HashEquals(inString, encryptedString, 99)

// if err != dpop.ErrHashFnNotAvailable {
// t.Errorf("wanted %e, got %e", dpop.ErrHashFnNotAvailable, err)
// }
// }

Loading