-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.go
64 lines (53 loc) · 1.19 KB
/
checksum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package rom
import (
"crypto/md5"
"crypto/sha1"
"hash/crc32"
"io"
"path/filepath"
)
// Checksum is used to specify a checksum/hash type
type Checksum int
// Supported checksum/hash types
const (
CRC32 Checksum = iota
MD5
SHA1
)
func checksum(r io.Reader) ([][]byte, error) {
c := crc32.NewIEEE()
m := md5.New()
s := sha1.New()
if _, err := io.Copy(io.MultiWriter(c, m, s), r); err != nil {
return nil, err
}
return [][]byte{c.Sum(nil)[:], m.Sum(nil)[:], s.Sum(nil)[:]}, nil
}
var extensionToChecksum = map[string]func(io.Reader) ([][]byte, error){
lynxExtension: func(r io.Reader) ([][]byte, error) {
var err error
if r, _, err = lynxReader(r); err != nil {
return nil, err
}
return checksum(r)
},
nesExtension: func(r io.Reader) ([][]byte, error) {
var err error
if r, _, err = nesReader(r); err != nil {
return nil, err
}
return checksum(r)
},
}
func needsDirectChecksum(filename string) bool {
if _, ok := extensionToChecksum[filepath.Ext(filename)]; ok {
return true
}
return false
}
func checksumFunction(filename string) func(io.Reader) ([][]byte, error) {
if f, ok := extensionToChecksum[filepath.Ext(filename)]; ok {
return f
}
return checksum
}