-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathiomisc.go
More file actions
49 lines (40 loc) · 917 Bytes
/
iomisc.go
File metadata and controls
49 lines (40 loc) · 917 Bytes
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
// iomisc.go -- misc i/o functions
//
// (c) 2016 Sudhi Herle <sudhi@herle.net>
//
// Licensing Terms: GPLv2
//
// If you need a commercial license for this work, please contact
// the author.
//
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
package sigtool
import (
"encoding/binary"
"fmt"
"github.com/opencoff/go-mmap"
"hash"
"os"
)
// Generate file checksum out of hash function h
func fileCksum(fn string, f func() hash.Hash) ([]byte, error) {
fd, err := os.Open(fn)
if err != nil {
return nil, fmt.Errorf("can't open %s: %w", fn, err)
}
defer fd.Close()
h := f()
sz, err := mmap.Reader(fd, func(b []byte) error {
h.Write(b)
return nil
})
if err != nil {
return nil, err
}
var b [8]byte
binary.BigEndian.PutUint64(b[:], uint64(sz))
h.Write(b[:])
return h.Sum(nil), nil
}