diff --git a/hashutil/go.mod b/hashutil/go.mod index 57d565b..4472286 100644 --- a/hashutil/go.mod +++ b/hashutil/go.mod @@ -2,6 +2,14 @@ module github.com/88labs/go-utils/hashutil go 1.22 -require github.com/go-faker/faker/v4 v4.3.0 +require ( + github.com/go-faker/faker/v4 v4.3.0 + github.com/stretchr/testify v1.9.0 +) -require golang.org/x/text v0.14.0 // indirect +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/hashutil/go.sum b/hashutil/go.sum index 5efb8bd..1545e1c 100644 --- a/hashutil/go.sum +++ b/hashutil/go.sum @@ -1,4 +1,14 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-faker/faker/v4 v4.3.0 h1:UXOW7kn/Mwd0u6MR30JjUKVzguT20EB/hBOddAAO+DY= github.com/go-faker/faker/v4 v4.3.0/go.mod h1:F/bBy8GH9NxOxMInug5Gx4WYeG6fHJZ8Ol/dhcpRub4= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/hashutil/sha256hash.go b/hashutil/sha256hash.go index 3e764bc..8c38e03 100644 --- a/hashutil/sha256hash.go +++ b/hashutil/sha256hash.go @@ -3,6 +3,7 @@ package hashutil import ( "crypto/sha256" "encoding/base64" + "fmt" ) type Hash string @@ -11,7 +12,7 @@ func (h Hash) Value() string { return string(h) } -func MustGetHash(str string) Hash { +func MustString(str string) Hash { hasher := sha256.New() if _, err := hasher.Write([]byte(str)); err != nil { panic(err) @@ -20,7 +21,7 @@ func MustGetHash(str string) Hash { return Hash(sha) } -func GetHash(str string) (Hash, error) { +func String(str string) (Hash, error) { hasher := sha256.New() if _, err := hasher.Write([]byte(str)); err != nil { return "", err @@ -29,7 +30,7 @@ func GetHash(str string) (Hash, error) { return Hash(sha), nil } -func MustGetHashByte(bytes []byte) Hash { +func MustByte(bytes []byte) Hash { hasher := sha256.New() if _, err := hasher.Write(bytes); err != nil { panic(err) @@ -38,7 +39,7 @@ func MustGetHashByte(bytes []byte) Hash { return Hash(sha) } -func GetHashByte(bytes []byte) (Hash, error) { +func Byte(bytes []byte) (Hash, error) { hasher := sha256.New() if _, err := hasher.Write(bytes); err != nil { return "", err @@ -46,3 +47,21 @@ func GetHashByte(bytes []byte) (Hash, error) { sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) return Hash(sha), nil } + +func MustStruct(i interface{}) Hash { + hasher := sha256.New() + if _, err := fmt.Fprintf(hasher, "%v", i); err != nil { + panic(err) + } + sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) + return Hash(sha) +} + +func Struct(i interface{}) (Hash, error) { + hasher := sha256.New() + if _, err := fmt.Fprintf(hasher, "%v", i); err != nil { + return "", err + } + sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) + return Hash(sha), nil +} diff --git a/hashutil/sha256hash_test.go b/hashutil/sha256hash_test.go index 45aa2e8..48885f8 100644 --- a/hashutil/sha256hash_test.go +++ b/hashutil/sha256hash_test.go @@ -3,6 +3,8 @@ package hashutil_test import ( "testing" + "github.com/stretchr/testify/assert" + "github.com/88labs/go-utils/hashutil" "github.com/go-faker/faker/v4" ) @@ -11,10 +13,8 @@ func TestMustGetHash(t *testing.T) { t.Run("get hash", func(t *testing.T) { for i := 0; i < 100; i++ { data := faker.Paragraph() - h := hashutil.MustGetHash(data) - if 44 != len(h) { - t.Errorf("Expected h to not equal 44") - } + h := hashutil.MustString(data) + assert.Equal(t, 44, len(h)) } }) } @@ -23,13 +23,9 @@ func TestGetHash(t *testing.T) { t.Run("get hash", func(t *testing.T) { for i := 0; i < 100; i++ { data := faker.Paragraph() - h, err := hashutil.GetHash(data) - if err != nil { - t.Error(err) - } - if 44 != len(h) { - t.Errorf("Expected h to not equal 44") - } + h, err := hashutil.String(data) + assert.NoError(t, err) + assert.Equal(t, 44, len(h)) } }) } @@ -38,10 +34,8 @@ func TestMustGetHashByte(t *testing.T) { t.Run("get hash byte", func(t *testing.T) { for i := 0; i < 100; i++ { data := []byte(faker.Paragraph()) - h := hashutil.MustGetHashByte(data) - if 44 != len(h) { - t.Errorf("Expected h to not equal 44") - } + h := hashutil.MustByte(data) + assert.Equal(t, 44, len(h)) } }) } @@ -50,13 +44,101 @@ func TestGetHashByte(t *testing.T) { t.Run("get hash byte", func(t *testing.T) { for i := 0; i < 100; i++ { data := []byte(faker.Paragraph()) - h, err := hashutil.GetHashByte(data) - if err != nil { - t.Error(err) + h, err := hashutil.Byte(data) + assert.NoError(t, err) + assert.Equal(t, 44, len(h)) + } + }) +} + +func TestMustGetHashStruct(t *testing.T) { + type Test struct { + ID string + hoge string + } + t.Run("get hash", func(t *testing.T) { + for i := 0; i < 100; i++ { + data1 := Test{ + ID: faker.Paragraph(), + hoge: faker.Paragraph(), + } + data2 := Test{ + ID: faker.Paragraph(), + hoge: faker.Paragraph(), + } + h1 := hashutil.MustStruct(data1) + assert.Equal(t, 44, len(h1)) + h2 := hashutil.MustStruct(data1) + assert.Equal(t, h1, h2) + h3 := hashutil.MustStruct(data2) + assert.NotEqual(t, h1, h3) + } + }) + t.Run("get hash pointer", func(t *testing.T) { + for i := 0; i < 100; i++ { + data1 := &Test{ + ID: faker.Paragraph(), + hoge: faker.Paragraph(), + } + data2 := &Test{ + ID: faker.Paragraph(), + hoge: faker.Paragraph(), + } + h1 := hashutil.MustStruct(data1) + assert.Equal(t, 44, len(h1)) + h2 := hashutil.MustStruct(data1) + assert.Equal(t, h1, h2) + h3 := hashutil.MustStruct(data2) + assert.NotEqual(t, h1, h3) + } + }) +} + +func TestGetHashStruct(t *testing.T) { + type Test struct { + ID string + hoge string + } + t.Run("get hash", func(t *testing.T) { + for i := 0; i < 100; i++ { + data1 := Test{ + ID: faker.Paragraph(), + hoge: faker.Paragraph(), + } + data2 := Test{ + ID: faker.Paragraph(), + hoge: faker.Paragraph(), + } + h1, err := hashutil.Struct(data1) + assert.NoError(t, err) + assert.Equal(t, 44, len(h1)) + h2, err := hashutil.Struct(data1) + assert.NoError(t, err) + assert.Equal(t, h1, h2) + h3, err := hashutil.Struct(data2) + assert.NoError(t, err) + assert.NotEqual(t, h1, h3) + } + }) + t.Run("get hash pointer", func(t *testing.T) { + for i := 0; i < 100; i++ { + data1 := &Test{ + ID: faker.Paragraph(), + hoge: faker.Paragraph(), } - if 44 != len(h) { - t.Errorf("Expected h to not equal 44") + data2 := &Test{ + ID: faker.Paragraph(), + hoge: faker.Paragraph(), } + h1, err := hashutil.Struct(data1) + assert.NoError(t, err) + assert.Equal(t, 44, len(h1)) + h2, err := hashutil.Struct(data1) + assert.NoError(t, err) + assert.Equal(t, h1, h2) + h3, err := hashutil.Struct(data2) + assert.NoError(t, err) + assert.NotEqual(t, h1, h3) } }) }