-
Notifications
You must be signed in to change notification settings - Fork 0
/
hfile.go
55 lines (51 loc) · 995 Bytes
/
hfile.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
package hfile
import (
"crypto/sha256"
"fmt"
"io"
"math/rand"
"os"
)
const (
SecSize = 4 << 10
)
func Hash(fileName string) (result []byte, err error) {
f, err := os.Open(fileName)
if err != nil {
return
}
defer f.Close()
fs, err := f.Stat()
if err != nil {
return
}
sectionReader := io.NewSectionReader(f, 0, fs.Size())
hash256 := sha256.New()
hash256.Write([]byte(fmt.Sprint(sectionReader.Size())))
sz := sectionReader.Size()
if sz < SecSize {
buff := make([]byte, sz)
sectionReader.Read(buff)
hash256.Write(buff)
} else {
buff := make([]byte, SecSize)
sectionReader.Read(buff)
hash256.Write(buff)
carry := sz / SecSize
rand.Seed(carry)
rd := rand.Int63n(sz - SecSize)
sectionReader.Seek(rd, 1)
sectionReader.Read(buff)
hash256.Write(buff)
}
result = hash256.Sum(nil)
return
}
func HashString(fileName string) (result string, err error) {
sum, err := Hash(fileName)
if err != nil {
return
}
result = fmt.Sprintf("%x", sum)
return
}