Guest User

Untitled

a guest
Jan 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. func (b *Block) GenerateHash() {
  2.  
  3. index := strconv.Itoa(b.Index)
  4. nonce := strconv.Itoa(b.Nonce)
  5.  
  6. b.Hash = fmt.Sprintf("%x", sha256.Sum256([]byte(index+b.PreviousHash+b.Data+b.Timestamp.String()+nonce)))
  7. }
  8.  
  9. func (b *Block) Mine() {
  10. prefix := getPrefix(b.Difficulty)
  11.  
  12. for {
  13. b.GenerateHash()
  14.  
  15. if strings.HasPrefix(b.Hash, prefix) {
  16. break
  17. } else {
  18. b.Nonce = b.Nonce + 1
  19. b.GenerateHash()
  20. }
  21. }
  22. }
  23.  
  24. func getPrefix(length int) string {
  25. letterBytes := "0"
  26. b := make([]byte, length)
  27.  
  28. for i := range b {
  29. b[i] = letterBytes[0]
  30. }
  31.  
  32. return string(b)
  33. }
Add Comment
Please, Sign In to add comment