You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
806 B
38 lines
806 B
7 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"crypto/sha256"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// Block keeps block headers
|
||
|
type Block struct {
|
||
|
Timestamp int64
|
||
|
Data []byte
|
||
|
PrevBlockHash []byte
|
||
|
Hash []byte
|
||
|
}
|
||
|
|
||
|
// SetHash calculates and sets block hash
|
||
|
func (b *Block) SetHash() {
|
||
|
timestamp := []byte(strconv.FormatInt(b.Timestamp, 10))
|
||
|
headers := bytes.Join([][]byte{b.PrevBlockHash, b.Data, timestamp}, []byte{})
|
||
|
hash := sha256.Sum256(headers)
|
||
|
|
||
|
b.Hash = hash[:]
|
||
|
}
|
||
|
|
||
|
// NewBlock creates and returns Block
|
||
|
func NewBlock(data string, prevBlockHash []byte) *Block {
|
||
|
block := &Block{time.Now().Unix(), []byte(data), prevBlockHash, []byte{}}
|
||
|
block.SetHash()
|
||
|
return block
|
||
|
}
|
||
|
|
||
|
// NewGenesisBlock creates and returns genesis Block
|
||
|
func NewGenesisBlock() *Block {
|
||
|
return NewBlock("Genesis Block", []byte{})
|
||
|
}
|