Skip to content

Commit a900daa

Browse files
committed
Replace getFileMD5 with getFileSHA256
The function was using an MD5 for checking, whereas a more secure option like SHA-256 would be preferred. See https://www.iacr.org/cryptodb/data/paper.php?pubkey=23903 (for example) for the (in-)security. SHA256 was chosen over Blake2 because there's a widely deployed CLI sha256sum on unix systems, and performance is not an issue here. Activates gosec G401 to fix such an issue, activates G501, G502, G503, G505 to tie up loose ends on less-secure hash functions..
1 parent 0b3e9ce commit a900daa

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

.golangci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ linters-settings:
1313
# To select a subset of rules to run.
1414
# Available rules: https://github.com/securego/gosec#available-rules
1515
includes:
16+
- G401
1617
- G402
18+
- G501
19+
- G502
20+
- G503
21+
- G505
1722

1823
linters:
1924
enable:

cmd/bootstrap/transit/cmd/crypto_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestEndToEnd(t *testing.T) {
5959
}
6060
}
6161

62-
func TestMd5(t *testing.T) {
62+
func TestSha256(t *testing.T) {
6363
tmpFile, err := ioutil.TempFile(os.TempDir(), "prefix-")
6464
assert.NoError(t, err)
6565
defer os.Remove(tmpFile.Name())
@@ -70,7 +70,7 @@ func TestMd5(t *testing.T) {
7070

7171
assert.NoError(t, tmpFile.Close())
7272

73-
md5, err := getFileMD5(tmpFile.Name())
73+
hash, err := getFileSHA256(tmpFile.Name())
7474
assert.NoError(t, err)
75-
assert.Equal(t, "1b8e86521e7e04d647faa9e6192a65f5", md5)
75+
assert.Equal(t, "876a3eab5fe740cb864a3d62869b0eefd6fbc34ec331c3064a6ffac0f9485a88", hash)
7676
}

cmd/bootstrap/transit/cmd/pull.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ func pull(cmd *cobra.Command, args []string) {
114114
}
115115
}
116116

117-
// calculate MD5 of rootsnapshot
117+
// calculate SHA256 of rootsnapshot
118118
rootFile := filepath.Join(flagBootDir, model.PathRootProtocolStateSnapshot)
119-
rootMD5, err := getFileMD5(rootFile)
119+
rootSHA256, err := getFileSHA256(rootFile)
120120
if err != nil {
121-
log.Fatal().Err(err).Str("file", rootFile).Msg("failed to calculate md5")
121+
log.Fatal().Err(err).Str("file", rootFile).Msg("failed to calculate SHA256 of root file")
122122
}
123-
log.Info().Str("md5", rootMD5).Msg("calculated MD5 of protocol snapshot")
123+
log.Info().Str("sha256", rootSHA256).Msg("calculated SHA256 of protocol snapshot")
124124
}

cmd/bootstrap/transit/cmd/utils.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package cmd
22

33
import (
4-
"crypto/md5"
54
"crypto/rand"
5+
"crypto/sha256"
66
"fmt"
77
"io"
88
"io/ioutil"
@@ -73,18 +73,17 @@ func getFilesToUpload(role flow.Role) []string {
7373
}
7474
}
7575

76-
func getFileMD5(file string) (string, error) {
76+
func getFileSHA256(file string) (string, error) {
7777
f, err := os.Open(file)
7878
if err != nil {
7979
return "", err
8080
}
8181
defer f.Close()
8282

83-
h := md5.New()
83+
h := sha256.New()
8484
if _, err := io.Copy(h, f); err != nil {
8585
return "", err
8686
}
87-
8887
return fmt.Sprintf("%x", h.Sum(nil)), nil
8988
}
9089

0 commit comments

Comments
 (0)