A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/arduino/arduino-fwuploader/commit/b7f323ad97e26ec7806701edef058ff380d3a463 below:

Add generic function to verify checksum (#120) · arduino/arduino-fwuploader@b7f323a · GitHub

File tree Expand file treeCollapse file tree 1 file changed

+18

-9

lines changed

Filter options

Expand file treeCollapse file tree 1 file changed

+18

-9

lines changed Original file line number Diff line number Diff line change

@@ -22,6 +22,7 @@ import (

22 22

"bytes"

23 23

"crypto"

24 24

"encoding/hex"

25 +

"errors"

25 26

"fmt"

26 27

"hash"

27 28

"io"

@@ -153,10 +154,10 @@ func Download(d *downloader.Downloader) error {

153 154

return nil

154 155

}

155 156 156 -

// VerifyFileChecksum is taken and adapted from https://github.com/arduino/arduino-cli/blob/59b6277a4d6731a1c1579d43aef6df2a46a771d5/arduino/resources/checksums.go

157 -

func VerifyFileChecksum(checksum string, filePath *paths.Path) error {

157 +

// VerifyChecksum is taken and adapted from https://github.com/arduino/arduino-cli/blob/59b6277a4d6731a1c1579d43aef6df2a46a771d5/arduino/resources/checksums.go

158 +

func VerifyChecksum(checksum string, file io.Reader) error {

158 159

if checksum == "" {

159 -

return fmt.Errorf("missing checksum for: %s", filePath)

160 +

return errors.New("missing checksum")

160 161

}

161 162

split := strings.SplitN(checksum, ":", 2)

162 163

if len(split) != 2 {

@@ -180,21 +181,29 @@ func VerifyFileChecksum(checksum string, filePath *paths.Path) error {

180 181

return fmt.Errorf("unsupported hash algorithm: %s", split[0])

181 182

}

182 183 183 -

file, err := filePath.Open()

184 -

if err != nil {

185 -

return fmt.Errorf("opening file: %s", err)

186 -

}

187 -

defer file.Close()

188 184

if _, err := io.Copy(algo, file); err != nil {

189 185

return fmt.Errorf("computing hash: %s", err)

190 186

}

191 -

if bytes.Compare(algo.Sum(nil), digest) != 0 {

187 +

if !bytes.Equal(algo.Sum(nil), digest) {

192 188

return fmt.Errorf("archive hash differs from hash in index")

193 189

}

194 190 195 191

return nil

196 192

}

197 193 194 +

// VerifyFileChecksum checks if the passed checksum matches the passed file checksum

195 +

func VerifyFileChecksum(checksum string, filePath *paths.Path) error {

196 +

file, err := filePath.Open()

197 +

if err != nil {

198 +

return fmt.Errorf("opening file: %s", err)

199 +

}

200 +

defer file.Close()

201 +

if err = VerifyChecksum(checksum, file); err != nil {

202 +

return fmt.Errorf("verifying checksum of file %s: %w", filePath, err)

203 +

}

204 +

return nil

205 +

}

206 + 198 207

// VerifyFileSize is taken and adapted from https://github.com/arduino/arduino-cli/blob/59b6277a4d6731a1c1579d43aef6df2a46a771d5/arduino/resources/checksums.go

199 208

func VerifyFileSize(size int64, filePath *paths.Path) error {

200 209

info, err := filePath.Stat()

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4