Skip to content

Commit e0ac4aa

Browse files
Flasher: use sha256 algorithm to check the integrity of the Debian image (#706)
1 parent 43d3a92 commit e0ac4aa

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

arduino-flasher-cli/updater/download_image.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package updater
33
import (
44
"bytes"
55
"context"
6-
"crypto/md5"
6+
"crypto/sha256"
77
"encoding/hex"
88
"fmt"
99
"io"
@@ -19,7 +19,7 @@ type Manifest struct {
1919
Latest struct {
2020
Version string `json:"version"`
2121
Url string `json:"url"`
22-
Md5sum string `json:"md5sum"`
22+
Sha256 string `json:"sha256"`
2323
} `json:"latest"`
2424
}
2525

@@ -122,16 +122,16 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
122122

123123
// Download and keep track of the progress
124124
src := &PassThru{Reader: download, length: size, progressCB: func(f float64) { feedback.Printf("Download progress: %.2f %%", f) }}
125-
md5 := md5.New()
126-
if _, err := io.Copy(io.MultiWriter(md5, tmpZipFile), src); err != nil {
125+
checksum := sha256.New()
126+
if _, err := io.Copy(io.MultiWriter(checksum, tmpZipFile), src); err != nil {
127127
return nil, "", err
128128
}
129129

130130
// Check the hash
131-
if md5Byte, err := hex.DecodeString(manifest.Latest.Md5sum); err != nil {
132-
return nil, "", fmt.Errorf("could not convert md5 from hex to bytes: %w", err)
133-
} else if s := md5.Sum(nil); !bytes.Equal(s, md5Byte) {
134-
return nil, "", fmt.Errorf("bad hash: %x (expected %x)", s, md5Byte)
131+
if sha256Byte, err := hex.DecodeString(manifest.Latest.Sha256); err != nil {
132+
return nil, "", fmt.Errorf("could not convert sha256 from hex to bytes: %w", err)
133+
} else if s := checksum.Sum(nil); !bytes.Equal(s, sha256Byte) {
134+
return nil, "", fmt.Errorf("bad hash: %x (expected %x)", s, sha256Byte)
135135
}
136136

137137
slog.Info("Download of Debian image completed", "path", temp)

arduino-flasher-cli/updater/http_client.go

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

33
import (
4-
"crypto/md5"
4+
"crypto/sha256"
55
"encoding/hex"
66
"encoding/json"
77
"fmt"
@@ -83,10 +83,10 @@ func (c *Client) GetInfoManifest() (Manifest, error) {
8383
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
8484
return Manifest{}, fmt.Errorf("invalid manifest JSON: %w", err)
8585
}
86-
if md5Byte, err := hex.DecodeString(res.Latest.Md5sum); err != nil {
87-
return Manifest{}, fmt.Errorf("could not convert md5 from hex to bytes: %w", err)
88-
} else if len(md5Byte) != md5.Size {
89-
return Manifest{}, fmt.Errorf("bad md5sum in manifest: got %d bytes", len(md5Byte))
86+
if sha256Byte, err := hex.DecodeString(res.Latest.Sha256); err != nil {
87+
return Manifest{}, fmt.Errorf("could not convert sha256 from hex to bytes: %w", err)
88+
} else if len(sha256Byte) != sha256.Size {
89+
return Manifest{}, fmt.Errorf("bad sha256sum in manifest: got %d bytes", len(sha256Byte))
9090
}
9191
return res, nil
9292
}

0 commit comments

Comments
 (0)