1616package updater
1717
1818import (
19+ "bytes"
1920 "context"
2021 "crypto/sha256"
2122 "encoding/hex"
@@ -137,8 +138,8 @@ func (c *Client) FetchZip(ctx context.Context, zipURL string) (io.ReadCloser, in
137138// DownloadFile downloads a file from a URL into the specified path. An optional config and options may be passed (or nil to use the defaults).
138139// A DownloadProgressCB callback function must be passed to monitor download progress.
139140// If a not empty queryParameter is passed, it is appended to the URL for analysis purposes.
140- func DownloadFile (ctx context.Context , path * paths.Path , url string , label string , downloadCB flasher.DownloadProgressCB , config downloader.Config , options ... downloader.DownloadOptions ) (returnedError error ) {
141- downloadCB .Start (url , label )
141+ func DownloadFile (ctx context.Context , path * paths.Path , rel * Release , downloadCB flasher.DownloadProgressCB , config downloader.Config , options ... downloader.DownloadOptions ) (returnedError error ) {
142+ downloadCB .Start (rel . Url , rel . Version )
142143 defer func () {
143144 if returnedError == nil {
144145 downloadCB .End (true , "" )
@@ -147,7 +148,7 @@ func DownloadFile(ctx context.Context, path *paths.Path, url string, label strin
147148 }
148149 }()
149150
150- d , err := downloader .DownloadWithConfigAndContext (ctx , path .String (), url , config , options ... )
151+ d , err := downloader .DownloadWithConfigAndContext (ctx , path .String (), rel . Url , config , options ... )
151152 if err != nil {
152153 return err
153154 }
@@ -165,5 +166,23 @@ func DownloadFile(ctx context.Context, path *paths.Path, url string, label strin
165166 return fmt .Errorf ("%s" , msg )
166167 }
167168
169+ // Check the hash
170+ checksum := sha256 .New ()
171+ tmpZipFile , err := path .Open ()
172+ if err != nil {
173+ return fmt .Errorf ("could not open archive: %w" , err )
174+ }
175+ defer tmpZipFile .Close ()
176+
177+ _ , err = io .Copy (checksum , tmpZipFile )
178+ if err != nil {
179+ return err
180+ }
181+ if sha256Byte , err := hex .DecodeString (rel .Sha256 ); err != nil {
182+ return fmt .Errorf ("could not convert sha256 from hex to bytes: %w" , err )
183+ } else if s := checksum .Sum (nil ); ! bytes .Equal (s , sha256Byte ) {
184+ return fmt .Errorf ("bad hash: %x (expected %x)" , s , sha256Byte )
185+ }
186+
168187 return nil
169188}
0 commit comments