golang[44]-blockchain-targetHash

比特币目标hash计算过程

以之前的bits:181B7B74为例子

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
func main(){

bits,_:= hex.DecodeString("181B7B74")


fmt.Printf("%x",CalculateTargetFast(bits))

}
//18   1B7B74
func CalculateTargetFast(bits []byte) []byte{

var result []byte
//第一个字节  计算指数
exponent := bits[:1]
fmt.Printf("%x\n",exponent)

//计算后面3个字节 系数
coeffient:= bits[1:]
fmt.Printf("%x\n",coeffient)


//将字节,他的16进制为"18"  转化为了string "18"
str:= hex.EncodeToString(exponent)  //"18"
fmt.Printf("str=%s\n",str)
  //将字符串18转化为了10进制int64 24
exp,_:=strconv.ParseInt(str,16,8)

fmt.Printf("exp=%d\n",exp)
//拼接,计算出目标hash
result  = append(bytes.Repeat([]byte{0x00},32-int(exp)),coeffient...)
result  =  append(result,bytes.Repeat([]byte{0x00},32-len(result))...)


return result
}

猜你喜欢

转载自blog.51cto.com/13784902/2330173
44