forked from dreamerjackson/BuildingBlockChain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
118 lines (100 loc) · 2.6 KB
/
utils.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"bytes"
"encoding/binary"
"log"
"crypto/sha256"
"fmt"
"encoding/hex"
)
// IntToHex converts an int32 to a byte array
//将int32转换为字节数组
func IntToHex(num int32) []byte {
buff := new(bytes.Buffer)
//还可以大小段模式
err := binary.Write(buff, binary.LittleEndian, num)
if err != nil {
log.Panic(err)
}
return buff.Bytes()
}
func IntToHex64(num int64) []byte {
buff := new(bytes.Buffer)
//还可以大小段模式
err := binary.Write(buff, binary.LittleEndian, num)
if err != nil {
log.Panic(err)
}
return buff.Bytes()
}
//将32位的int转换为16进制,16进制的每个数字转成4位
func change( num int32) [4]byte{
var buffle [8]byte
for i:=0;i<8;i++{
buff := new(bytes.Buffer)
binary.Write(buff, binary.BigEndian, num%16)
//buffle = bytes.Join([][]byte{buffle,buff.Bytes()}, []byte{})
result := (buff.Bytes())[3]
buffle[7-i]=result
num = num >> 4
}
var buffle2 [4]byte
var j uint = 0;
for ;j<8;j++{
buffle2[j/2] = buffle2[j/2] | ( buffle[j]<<(4*((j+1)%2)) );
}
return buffle2
}
// ReverseBytes reverses a byte array
//大小端转换第1种方式。
func ReverseBytes(data []byte) {
for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 {
data[i], data[j] = data[j], data[i]
}
}
//大小端转换第2种方式
func reverse2(data []byte) []byte{
var s [][]byte
for i:=len(data);i>0;i--{
data1 :=data[i-1:i]
s = append(s,data1)
}
sep := []byte("")
result :=bytes.Join(s,sep)
return result
}
func testActualBiton(){
////版本
version :=IntToHex(2)
////前一个区块链的hash
PrevBlockHash,_:=hex.DecodeString("00000000000000000A2940884E0C3BC96510CAD11912A527E9D15DF42F0E1D67")
PrevBlockHash = reverse2(PrevBlockHash)
////默克尔根
////timestamp := []byte(strconv.FormatInt(int64((time-25569)*86400), 16))
////fmt.Printf("%x\n",[]byte((time-25569)*86400)
// fmt.Printf("%x\n",PrevBlockHash)
MerkleRoot,_:=hex.DecodeString("2E99F445C007A9158207CC30CEBAD2B3D26C45FDAB2EBDF50D261335FC00D92C")
MerkleRoot = reverse2(MerkleRoot)
////时间
var time2 int32
time2=1418753140
var buffletime [4]byte
buffletime = change(time2)
reverstime := reverse2(buffletime[:])
//难度
var bits int32 = 404454260
var buff [4]byte
buff = change(bits)
reversbuff := reverse2(buff[:])
//随机Nonce
var Nonce int32 = 123
var buffNonce [4]byte
buffNonce = change(Nonce)
reversbuffNonce := reverse2(buffNonce[:])
//字节
result := bytes.Join([][]byte{version,PrevBlockHash,MerkleRoot,reverstime,reversbuff,reversbuffNonce}, []byte{})
//双hash
hash := sha256.Sum256(result)
rev := sha256.Sum256(hash[:])
fmt.Printf("%x",rev)
}