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
| package main
import ( "bytes" "encoding/binary" "log" "fmt" "encoding/hex" "crypto/sha256" )
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 ReverseBytes4(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] } }
func main(){
var version int32 = 2
fmt.Printf("%x\n",IntToHex(version))
prev,_ := hex.DecodeString("000000000000000016145aa12fa7e81a304c38aec3d7c5208f1d33b587f966a6") ReverseBytes4(prev) fmt.Printf("%x\n",prev)
merkleroot,_ := hex.DecodeString("3a4f410269fcc4c7885770bc8841ce6781f15dd304ae5d2770fc93a21dbd70d7") ReverseBytes4(merkleroot) fmt.Printf("%x\n",merkleroot)
var time int32 = 1418755780 fmt.Printf("%x\n",IntToHex(time))
var bits int32 = 404454260 fmt.Printf("%x\n",IntToHex(bits))
var nonce int32 = 1865996595 fmt.Printf("%x\n",IntToHex(nonce))
result := bytes.Join([][]byte{IntToHex(version),prev,merkleroot,IntToHex(time),IntToHex(bits),IntToHex(nonce)},[]byte{})
fmt.Printf("%x\n",result)
firsthash := sha256.Sum256(result) resulthash:= sha256.Sum256(firsthash[:])
ReverseBytes4(resulthash[:]) fmt.Printf("%x",resulthash) }
|