Skip to content

Commit c7b0a79

Browse files
authored
Merge pull request #6 from knbr13/4-blake2-instead-of-md5
use blake2b instead of md5
2 parents 2594d7a + d00c77e commit c7b0a79

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.24.0
55
require (
66
github.com/charmbracelet/bubbletea v1.3.4
77
github.com/charmbracelet/lipgloss v1.0.0
8+
golang.org/x/crypto v0.36.0
89
)
910

1011
require (
@@ -20,7 +21,7 @@ require (
2021
github.com/muesli/cancelreader v0.2.2 // indirect
2122
github.com/muesli/termenv v0.15.2 // indirect
2223
github.com/rivo/uniseg v0.4.7 // indirect
23-
golang.org/x/sync v0.11.0 // indirect
24-
golang.org/x/sys v0.30.0 // indirect
25-
golang.org/x/text v0.3.8 // indirect
24+
golang.org/x/sync v0.12.0 // indirect
25+
golang.org/x/sys v0.31.0 // indirect
26+
golang.org/x/text v0.23.0 // indirect
2627
)

go.sum

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1n
2727
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
2828
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
2929
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
30-
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
31-
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
30+
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
31+
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
32+
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
33+
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
3234
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3335
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
34-
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
35-
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
36-
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
37-
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
36+
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
37+
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
38+
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
39+
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=

hash.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package main
22

33
import (
4-
"crypto/md5"
54
"encoding/hex"
65
"fmt"
76
"io"
87
"os"
8+
9+
"golang.org/x/crypto/blake2b"
910
)
1011

1112
func createFileHash(filePath string) (string, error) {
@@ -15,7 +16,11 @@ func createFileHash(filePath string) (string, error) {
1516
}
1617
defer file.Close()
1718

18-
hash := md5.New()
19+
hash, err := blake2b.New256(nil)
20+
if err != nil {
21+
return "", fmt.Errorf("failed to create BLAKE2b hash: %w", err)
22+
}
23+
1924
if _, err := io.Copy(hash, file); err != nil {
2025
return "", fmt.Errorf("failed to compute hash: %w", err)
2126
}

hash_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package main
22

33
import (
4-
"crypto/md5"
54
"encoding/hex"
65
"io"
76
"os"
87
"path/filepath"
98
"testing"
9+
10+
"golang.org/x/crypto/blake2b"
1011
)
1112

1213
func TestCreateFileHash(t *testing.T) {
@@ -30,7 +31,10 @@ func TestCreateFileHash(t *testing.T) {
3031
}
3132
contentFile.Close()
3233

33-
h := md5.New()
34+
h, err := blake2b.New256(nil)
35+
if err != nil {
36+
t.Fatalf("Failed to create BLAKE2b hash: %v", err)
37+
}
3438
n, err := io.WriteString(h, content)
3539
if err != nil {
3640
t.Error(err)
@@ -40,7 +44,10 @@ func TestCreateFileHash(t *testing.T) {
4044
}
4145
expectedContentHash := hex.EncodeToString(h.Sum(nil))
4246

43-
emptyHash := md5.New()
47+
emptyHash, err := blake2b.New256(nil)
48+
if err != nil {
49+
t.Fatalf("Failed to create BLAKE2b hash for empty file: %v", err)
50+
}
4451
expectedEmptyHash := hex.EncodeToString(emptyHash.Sum(nil))
4552

4653
tests := []struct {

0 commit comments

Comments
 (0)