-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #475 from 88labs/feat/hashutil
feat: hashutil newly implemented
- Loading branch information
Showing
15 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,3 +108,6 @@ autolabeler: | |
- label: lib:utf8bom | ||
files: | ||
- 'utf8bom/**/*' | ||
- label: lib:hashutil | ||
files: | ||
- 'hashutil/**/*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,3 +108,6 @@ autolabeler: | |
- label: lib:utf8bom | ||
files: | ||
- 'utf8bom/**/*' | ||
- label: lib:hashutil | ||
files: | ||
- 'hashutil/**/*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,3 +108,6 @@ autolabeler: | |
- label: lib:utf8bom | ||
files: | ||
- 'utf8bom/**/*' | ||
- label: lib:hashutil | ||
files: | ||
- 'hashutil/**/*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,3 +108,6 @@ autolabeler: | |
- label: lib:utf8bom | ||
files: | ||
- 'utf8bom/**/*' | ||
- label: lib:hashutil | ||
files: | ||
- 'hashutil/**/*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/**/*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,3 +108,6 @@ autolabeler: | |
- label: lib:utf8bom | ||
files: | ||
- 'utf8bom/**/*' | ||
- label: lib:hashutil | ||
files: | ||
- 'hashutil/**/*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,3 +108,6 @@ autolabeler: | |
- label: lib:utf8bom | ||
files: | ||
- 'utf8bom/**/*' | ||
- label: lib:hashutil | ||
files: | ||
- 'hashutil/**/*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,3 +108,6 @@ autolabeler: | |
- label: lib:utf8bom | ||
files: | ||
- 'utf8bom/**/*' | ||
- label: lib:hashutil | ||
files: | ||
- 'hashutil/**/*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,3 +108,6 @@ autolabeler: | |
- label: lib:utf8bom | ||
files: | ||
- 'utf8bom/**/*' | ||
- label: lib:hashutil | ||
files: | ||
- 'hashutil/**/*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,3 +108,6 @@ autolabeler: | |
- label: lib:utf8bom | ||
files: | ||
- 'utf8bom/**/*' | ||
- label: lib:hashutil | ||
files: | ||
- 'hashutil/**/*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
}) | ||
} |