-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0944f8
commit 9838e1f
Showing
5 changed files
with
127 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,72 @@ | ||
package checksum | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/jreisinger/ghrel/asset" | ||
) | ||
|
||
// Checksum represents a line from a checksums file. The line looks like: | ||
// ba47c83b6038dda089dd1410b9e97d1de7e4adea7620c856f9b74a782048e272 checkip_0.45.1_linux_amd64.tar.gz | ||
type Checksum struct { | ||
Checksum []byte // in hex | ||
Name string // filename | ||
// Sha256 calculates SHA256 checksum of filename. Checksum is in hex format. | ||
func Sha256(filename string) (string, error) { | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer file.Close() | ||
|
||
hash := sha256.New() | ||
if _, err := io.Copy(hash, file); err != nil { | ||
return "", err | ||
} | ||
sum := hash.Sum(nil) | ||
return fmt.Sprintf("%x", sum), nil | ||
} | ||
|
||
// Get extracts checksums and corresponding filenames from the checksums files. | ||
func Get(assets []asset.Asset) ([]Checksum, error) { | ||
var checksums []Checksum | ||
for _, a := range assets { | ||
if !a.IsChecksumsFile { | ||
continue | ||
} | ||
// Pair represents a line from a checksum file. The line looks like: | ||
// | ||
// ba47c83b6038dda089dd1410b9e97d1de7e4adea7620c856f9b74a782048e272 checkip_0.45.1_linux_amd64.tar.gz | ||
type Pair struct { | ||
Checksum string // in hex | ||
Filename string | ||
} | ||
|
||
file, err := os.Open(a.Name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
// Parse parses a checksum file into checksum/filename pairs. | ||
func Parse(checksumFile string) ([]Pair, error) { | ||
var checksums []Pair | ||
|
||
b, err := io.ReadAll(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
file, err := os.Open(checksumFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
cs, err := parseChecksumsLines(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
checksums = append(checksums, cs...) | ||
b, err := io.ReadAll(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cs := parseChecksumLines(b) | ||
checksums = append(checksums, cs...) | ||
|
||
return checksums, nil | ||
} | ||
|
||
// parseChecksumsLines parses bytes from a checksums file that look like: | ||
// parseChecksumLines parses bytes from a checksums file. The bytes look like: | ||
// 712f37d14687e10ae0425bf7e5d0faf17c49f9476b8bb6a96f2a3f91b0436db2 checkip_0.45.1_linux_armv6.tar.gz | ||
// ba47c83b6038dda089dd1410b9e97d1de7e4adea7620c856f9b74a782048e272 checkip_0.45.1_linux_amd64.tar.gz | ||
func parseChecksumsLines(b []byte) ([]Checksum, error) { | ||
var checksums []Checksum | ||
func parseChecksumLines(b []byte) []Pair { | ||
var checksums []Pair | ||
for _, line := range strings.Split(string(b), "\n") { | ||
fields := strings.Fields(line) | ||
if len(fields) != 2 { | ||
continue | ||
} | ||
c, err := hex.DecodeString(fields[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
checksums = append(checksums, Checksum{ | ||
Checksum: c, | ||
Name: fields[1], | ||
checksums = append(checksums, Pair{ | ||
Checksum: fields[0], | ||
Filename: fields[1], | ||
}) | ||
} | ||
return checksums, nil | ||
} | ||
|
||
// Verify opens the filename from c, computes its SHA256 checksum and compares it to the checksum from c. | ||
func (c Checksum) Verify() (ok bool, err error) { | ||
file, err := os.Open(c.Name) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer file.Close() | ||
|
||
hash := sha256.New() | ||
if _, err := io.Copy(hash, file); err != nil { | ||
return false, err | ||
} | ||
sum := hash.Sum(nil) | ||
|
||
return bytes.Equal(c.Checksum, sum), nil | ||
return checksums | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.