Skip to content

Commit

Permalink
Merge pull request #476 from 88labs/feat/hashutil-struct
Browse files Browse the repository at this point in the history
feat: hashutil struct, change func name
  • Loading branch information
tomtwinkle authored Mar 25, 2024
2 parents 92b775c + a390a82 commit f0a4935
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 26 deletions.
12 changes: 10 additions & 2 deletions hashutil/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 10 additions & 0 deletions hashutil/go.sum
Original file line number Diff line number Diff line change
@@ -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=
27 changes: 23 additions & 4 deletions hashutil/sha256hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hashutil
import (
"crypto/sha256"
"encoding/base64"
"fmt"
)

type Hash string
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -38,11 +39,29 @@ 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
}
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
}
122 changes: 102 additions & 20 deletions hashutil/sha256hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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))
}
})
}
Expand All @@ -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))
}
})
}
Expand All @@ -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))
}
})
}
Expand All @@ -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)
}
})
}

0 comments on commit f0a4935

Please sign in to comment.