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: hashutil newly implemented #475

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .github/release-drafter-aws.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ autolabeler:
- label: lib:utf8bom
files:
- 'utf8bom/**/*'
- label: lib:hashutil
files:
- 'hashutil/**/*'
3 changes: 3 additions & 0 deletions .github/release-drafter-cerrors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ autolabeler:
- label: lib:utf8bom
files:
- 'utf8bom/**/*'
- label: lib:hashutil
files:
- 'hashutil/**/*'
3 changes: 3 additions & 0 deletions .github/release-drafter-envlookup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ autolabeler:
- label: lib:utf8bom
files:
- 'utf8bom/**/*'
- label: lib:hashutil
files:
- 'hashutil/**/*'
3 changes: 3 additions & 0 deletions .github/release-drafter-errgroup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ autolabeler:
- label: lib:utf8bom
files:
- 'utf8bom/**/*'
- label: lib:hashutil
files:
- 'hashutil/**/*'
113 changes: 113 additions & 0 deletions .github/release-drafter-hashutil.yml
Original file line number Diff line number Diff line change
@@ -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/**/*'
3 changes: 3 additions & 0 deletions .github/release-drafter-osext.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ autolabeler:
- label: lib:utf8bom
files:
- 'utf8bom/**/*'
- label: lib:hashutil
files:
- 'hashutil/**/*'
3 changes: 3 additions & 0 deletions .github/release-drafter-sql-escape.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ autolabeler:
- label: lib:utf8bom
files:
- 'utf8bom/**/*'
- label: lib:hashutil
files:
- 'hashutil/**/*'
3 changes: 3 additions & 0 deletions .github/release-drafter-tspb_cast.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ autolabeler:
- label: lib:utf8bom
files:
- 'utf8bom/**/*'
- label: lib:hashutil
files:
- 'hashutil/**/*'
3 changes: 3 additions & 0 deletions .github/release-drafter-ulid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ autolabeler:
- label: lib:utf8bom
files:
- 'utf8bom/**/*'
- label: lib:hashutil
files:
- 'hashutil/**/*'
3 changes: 3 additions & 0 deletions .github/release-drafter-utf8bom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ autolabeler:
- label: lib:utf8bom
files:
- 'utf8bom/**/*'
- label: lib:hashutil
files:
- 'hashutil/**/*'
52 changes: 52 additions & 0 deletions .github/workflows/test-hashutil.yaml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions hashutil/go.mod
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions hashutil/go.sum
Original file line number Diff line number Diff line change
@@ -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=
48 changes: 48 additions & 0 deletions hashutil/sha256hash.go
Original file line number Diff line number Diff line change
@@ -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
}
62 changes: 62 additions & 0 deletions hashutil/sha256hash_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
})
}
Loading