File size: 1,733 Bytes
7107f0b |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
package hash_extend
import (
"crypto/sha1"
"encoding"
"fmt"
"hash"
"strconv"
"github.com/alist-org/alist/v3/pkg/utils"
)
var GCID = utils.RegisterHashWithParam("gcid", "GCID", 40, func(a ...any) hash.Hash {
var (
size int64
err error
)
if len(a) > 0 {
size, err = strconv.ParseInt(fmt.Sprint(a[0]), 10, 64)
if err != nil {
panic(err)
}
}
return NewGcid(size)
})
func NewGcid(size int64) hash.Hash {
calcBlockSize := func(j int64) int64 {
var psize int64 = 0x40000
for float64(j)/float64(psize) > 0x200 && psize < 0x200000 {
psize = psize << 1
}
return psize
}
return &gcid{
hash: sha1.New(),
hashState: sha1.New(),
blockSize: int(calcBlockSize(size)),
}
}
type gcid struct {
hash hash.Hash
hashState hash.Hash
blockSize int
offset int
}
func (h *gcid) Write(p []byte) (n int, err error) {
n = len(p)
for len(p) > 0 {
if h.offset < h.blockSize {
var lastSize = h.blockSize - h.offset
if lastSize > len(p) {
lastSize = len(p)
}
h.hashState.Write(p[:lastSize])
h.offset += lastSize
p = p[lastSize:]
}
if h.offset >= h.blockSize {
h.hash.Write(h.hashState.Sum(nil))
h.hashState.Reset()
h.offset = 0
}
}
return
}
func (h *gcid) Sum(b []byte) []byte {
if h.offset != 0 {
if hashm, ok := h.hash.(encoding.BinaryMarshaler); ok {
if hashum, ok := h.hash.(encoding.BinaryUnmarshaler); ok {
tempData, _ := hashm.MarshalBinary()
defer hashum.UnmarshalBinary(tempData)
h.hash.Write(h.hashState.Sum(nil))
}
}
}
return h.hash.Sum(b)
}
func (h *gcid) Reset() {
h.hash.Reset()
h.hashState.Reset()
}
func (h *gcid) Size() int {
return h.hash.Size()
}
func (h *gcid) BlockSize() int {
return h.blockSize
}
|