From fdf1636b183f94b5f88411f0520c82fd4f01da33 Mon Sep 17 00:00:00 2001 From: tom twinkle Date: Mon, 25 Mar 2024 12:43:09 +0900 Subject: [PATCH 1/3] feat: hashutil --- hashutil/go.mod | 7 +++++ hashutil/go.sum | 4 +++ hashutil/sha256hash.go | 48 ++++++++++++++++++++++++++++ hashutil/sha256hash_test.go | 62 +++++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 hashutil/go.mod create mode 100644 hashutil/go.sum create mode 100644 hashutil/sha256hash.go create mode 100644 hashutil/sha256hash_test.go diff --git a/hashutil/go.mod b/hashutil/go.mod new file mode 100644 index 0000000..57d565b --- /dev/null +++ b/hashutil/go.mod @@ -0,0 +1,7 @@ +module github.com/88labs/go-utils/hashutil + +go 1.22 + +require github.com/go-faker/faker/v4 v4.3.0 + +require golang.org/x/text v0.14.0 // indirect diff --git a/hashutil/go.sum b/hashutil/go.sum new file mode 100644 index 0000000..5efb8bd --- /dev/null +++ b/hashutil/go.sum @@ -0,0 +1,4 @@ +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= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= diff --git a/hashutil/sha256hash.go b/hashutil/sha256hash.go new file mode 100644 index 0000000..3e764bc --- /dev/null +++ b/hashutil/sha256hash.go @@ -0,0 +1,48 @@ +package hashutil + +import ( + "crypto/sha256" + "encoding/base64" +) + +type Hash string + +func (h Hash) Value() string { + return string(h) +} + +func MustGetHash(str string) Hash { + hasher := sha256.New() + if _, err := hasher.Write([]byte(str)); err != nil { + panic(err) + } + sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) + return Hash(sha) +} + +func GetHash(str string) (Hash, error) { + hasher := sha256.New() + if _, err := hasher.Write([]byte(str)); err != nil { + return "", err + } + sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) + return Hash(sha), nil +} + +func MustGetHashByte(bytes []byte) Hash { + hasher := sha256.New() + if _, err := hasher.Write(bytes); err != nil { + panic(err) + } + sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) + return Hash(sha) +} + +func GetHashByte(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 +} diff --git a/hashutil/sha256hash_test.go b/hashutil/sha256hash_test.go new file mode 100644 index 0000000..45aa2e8 --- /dev/null +++ b/hashutil/sha256hash_test.go @@ -0,0 +1,62 @@ +package hashutil_test + +import ( + "testing" + + "github.com/88labs/go-utils/hashutil" + "github.com/go-faker/faker/v4" +) + +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") + } + } + }) +} + +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") + } + } + }) +} + +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") + } + } + }) +} + +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) + } + if 44 != len(h) { + t.Errorf("Expected h to not equal 44") + } + } + }) +} From f3e011bfa0cb614ae101f8792500393d47478726 Mon Sep 17 00:00:00 2001 From: tom twinkle Date: Mon, 25 Mar 2024 12:43:31 +0900 Subject: [PATCH 2/3] hashutil add test --- .github/workflows/test-hashutil.yaml | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 .github/workflows/test-hashutil.yaml diff --git a/.github/workflows/test-hashutil.yaml b/.github/workflows/test-hashutil.yaml new file mode 100644 index 0000000..67bb05c --- /dev/null +++ b/.github/workflows/test-hashutil.yaml @@ -0,0 +1,52 @@ +name: Test hashutil + +on: + push: + branches: + - main + paths: + - "hashutil/**" + pull_request: + types: + - opened + - synchronize + - reopened + paths: + - "hashutil/**" +env: + testdir : ./hashutil + +jobs: + test: + strategy: + matrix: + go-version: [ 1.22.x ] + os: [ ubuntu-latest ] + runs-on: ${{ matrix.os }} + timeout-minutes: 5 + steps: + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go-version }} + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Go Module Download + working-directory: ${{ env.testdir }} + run: | + go install gotest.tools/gotestsum@latest + go mod download + + - name: Test + working-directory: ${{ env.testdir }} + timeout-minutes: 3 + run: | + # shellcheck disable=SC2046 + gotestsum --junitfile unit-tests.xml -- -v ./... -race -coverprofile="coverage.txt" -covermode=atomic -coverpkg=./... + + - uses: codecov/codecov-action@v4 + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: ${{ env.testdir }}/coverage.txt From e4303b49836d719b976965575d7f1c0f675a5bed Mon Sep 17 00:00:00 2001 From: tom twinkle Date: Mon, 25 Mar 2024 12:47:21 +0900 Subject: [PATCH 3/3] release drafter add hashutil --- .github/release-drafter-aws.yml | 3 + .github/release-drafter-cerrors.yml | 3 + .github/release-drafter-envlookup.yml | 3 + .github/release-drafter-errgroup.yml | 3 + .github/release-drafter-hashutil.yml | 113 +++++++++++++++++++++++++ .github/release-drafter-osext.yml | 3 + .github/release-drafter-sql-escape.yml | 3 + .github/release-drafter-tspb_cast.yml | 3 + .github/release-drafter-ulid.yml | 3 + .github/release-drafter-utf8bom.yml | 3 + 10 files changed, 140 insertions(+) create mode 100644 .github/release-drafter-hashutil.yml diff --git a/.github/release-drafter-aws.yml b/.github/release-drafter-aws.yml index f9a6bda..77f57cd 100644 --- a/.github/release-drafter-aws.yml +++ b/.github/release-drafter-aws.yml @@ -108,3 +108,6 @@ autolabeler: - label: lib:utf8bom files: - 'utf8bom/**/*' + - label: lib:hashutil + files: + - 'hashutil/**/*' diff --git a/.github/release-drafter-cerrors.yml b/.github/release-drafter-cerrors.yml index 62c9205..c0a730c 100644 --- a/.github/release-drafter-cerrors.yml +++ b/.github/release-drafter-cerrors.yml @@ -108,3 +108,6 @@ autolabeler: - label: lib:utf8bom files: - 'utf8bom/**/*' + - label: lib:hashutil + files: + - 'hashutil/**/*' diff --git a/.github/release-drafter-envlookup.yml b/.github/release-drafter-envlookup.yml index 4a01517..ae71e42 100644 --- a/.github/release-drafter-envlookup.yml +++ b/.github/release-drafter-envlookup.yml @@ -108,3 +108,6 @@ autolabeler: - label: lib:utf8bom files: - 'utf8bom/**/*' + - label: lib:hashutil + files: + - 'hashutil/**/*' diff --git a/.github/release-drafter-errgroup.yml b/.github/release-drafter-errgroup.yml index dd51324..de2aa38 100644 --- a/.github/release-drafter-errgroup.yml +++ b/.github/release-drafter-errgroup.yml @@ -108,3 +108,6 @@ autolabeler: - label: lib:utf8bom files: - 'utf8bom/**/*' + - label: lib:hashutil + files: + - 'hashutil/**/*' diff --git a/.github/release-drafter-hashutil.yml b/.github/release-drafter-hashutil.yml new file mode 100644 index 0000000..d20f218 --- /dev/null +++ b/.github/release-drafter-hashutil.yml @@ -0,0 +1,113 @@ +name-template: 'hashutil/v$RESOLVED_VERSION' +tag-template: 'hashutil/v$RESOLVED_VERSION' +tag-prefix: hashutil/ +include-paths: + - "hashutil" + +categories: + - title: '🚀 Features' + labels: + - 'feature' + - title: '💪 Enhancement' + labels: + - 'enhancement' + - title: '🐛 Bug Fixes' + labels: + - 'fix' + - 'bugfix' + - 'bug' + - title: '🧰 Maintenance' + label: 'chore' + - title: '🔧 Refactoring' + label: 'refactor' + - title: '📖 Documentation' + label: 'documentation' + - title: '⛓️ Dependency update' + label: 'dependencies' + +change-template: '- $TITLE @$AUTHOR (#$NUMBER)' + +change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks. + +version-resolver: + major: + labels: + - 'major' + minor: + labels: + - 'minor' + patch: + labels: + - 'patch' + default: minor + +template: | + ## Changes + $CHANGES +autolabeler: + - label: feature + branch: + - '/^feat(ure)?[/-].+/' + - label: bug + branch: + - '/^fix[/-].+/' + - '/^hotfix[/-].+/' + - label: chore + branch: + - '/^chore[/-].+/' + - label: refactor + branch: + - '/(refactor|refactoring)[/-].+/' + - label: documentation + branch: + - '/doc(umentation)[/-].+/' + files: + - '*.md' + - label: enhancement + branch: + - '/(enhancement|improve)[/-].+/' + - label: docker + files: + - 'docker/**/*' + - 'docker-compose.yaml' + - 'Dockerfile' + - '.dockerignore' + - label: github + files: + - '.github/**/*' + - label: patch + branch: + - '/^dependabot.+/' + - label: lib:aws + files: + - 'aws/**/*' + - label: lib:cerrors + files: + - 'cerrors/**/*' + - label: lib:envlookup + files: + - 'envlookup/**/*' + - label: lib:errgroup + files: + - 'errgroup/**/*' + - label: lib:osext + files: + - 'osext/**/*' + - label: lib:sql-escape + files: + - 'sql-escape/**/*' + - label: lib:sql-escape + files: + - 'sql-escape/**/*' + - label: lib:tspb_cast + files: + - 'tspb_cast/**/*' + - label: lib:ulid + files: + - 'ulid/**/*' + - label: lib:utf8bom + files: + - 'utf8bom/**/*' + - label: lib:hashutil + files: + - 'hashutil/**/*' diff --git a/.github/release-drafter-osext.yml b/.github/release-drafter-osext.yml index 663acd9..c202fac 100644 --- a/.github/release-drafter-osext.yml +++ b/.github/release-drafter-osext.yml @@ -108,3 +108,6 @@ autolabeler: - label: lib:utf8bom files: - 'utf8bom/**/*' + - label: lib:hashutil + files: + - 'hashutil/**/*' diff --git a/.github/release-drafter-sql-escape.yml b/.github/release-drafter-sql-escape.yml index 53236ff..29097c7 100644 --- a/.github/release-drafter-sql-escape.yml +++ b/.github/release-drafter-sql-escape.yml @@ -108,3 +108,6 @@ autolabeler: - label: lib:utf8bom files: - 'utf8bom/**/*' + - label: lib:hashutil + files: + - 'hashutil/**/*' diff --git a/.github/release-drafter-tspb_cast.yml b/.github/release-drafter-tspb_cast.yml index 3c2fce0..ee60eb0 100644 --- a/.github/release-drafter-tspb_cast.yml +++ b/.github/release-drafter-tspb_cast.yml @@ -108,3 +108,6 @@ autolabeler: - label: lib:utf8bom files: - 'utf8bom/**/*' + - label: lib:hashutil + files: + - 'hashutil/**/*' diff --git a/.github/release-drafter-ulid.yml b/.github/release-drafter-ulid.yml index ba279d5..8ce5a25 100644 --- a/.github/release-drafter-ulid.yml +++ b/.github/release-drafter-ulid.yml @@ -108,3 +108,6 @@ autolabeler: - label: lib:utf8bom files: - 'utf8bom/**/*' + - label: lib:hashutil + files: + - 'hashutil/**/*' diff --git a/.github/release-drafter-utf8bom.yml b/.github/release-drafter-utf8bom.yml index a2adb94..f0c73e6 100644 --- a/.github/release-drafter-utf8bom.yml +++ b/.github/release-drafter-utf8bom.yml @@ -108,3 +108,6 @@ autolabeler: - label: lib:utf8bom files: - 'utf8bom/**/*' + - label: lib:hashutil + files: + - 'hashutil/**/*'