github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/bmt/bmt_r.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 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 package bmt 35 36 import ( 37 "hash" 38 ) 39 40 // 41 type RefHasher struct { 42 maxDataLength int // 43 sectionLength int // 44 hasher hash.Hash // 45 } 46 47 // 48 func NewRefHasher(hasher BaseHasherFunc, count int) *RefHasher { 49 h := hasher() 50 hashsize := h.Size() 51 c := 2 52 for ; c < count; c *= 2 { 53 } 54 return &RefHasher{ 55 sectionLength: 2 * hashsize, 56 maxDataLength: c * hashsize, 57 hasher: h, 58 } 59 } 60 61 // 62 // 63 func (rh *RefHasher) Hash(data []byte) []byte { 64 // 65 d := make([]byte, rh.maxDataLength) 66 length := len(data) 67 if length > rh.maxDataLength { 68 length = rh.maxDataLength 69 } 70 copy(d, data[:length]) 71 return rh.hash(d, rh.maxDataLength) 72 } 73 74 // 75 // 76 // 77 // 78 func (rh *RefHasher) hash(data []byte, length int) []byte { 79 var section []byte 80 if length == rh.sectionLength { 81 // 82 section = data 83 } else { 84 // 85 // 86 length /= 2 87 section = append(rh.hash(data[:length], length), rh.hash(data[length:], length)...) 88 } 89 rh.hasher.Reset() 90 rh.hasher.Write(section) 91 return rh.hasher.Sum(nil) 92 }