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.

48 lines
1002 B

  1. package main
  2. import (
  3. "crypto/sha256"
  4. "encoding/base64"
  5. "encoding/json"
  6. "math/rand"
  7. "net"
  8. "strings"
  9. )
  10. func getIPPortFromConn(conn net.Conn) string {
  11. ip := getIPFromConn(conn)
  12. port := getPortFromConn(conn)
  13. return ip + ":" + port
  14. }
  15. func getIPFromConn(conn net.Conn) string {
  16. s := conn.RemoteAddr().String()
  17. s = strings.Split(s, ":")[0]
  18. s = strings.Trim(s, ":")
  19. return s
  20. }
  21. func getPortFromConn(conn net.Conn) string {
  22. s := conn.RemoteAddr().String()
  23. s = strings.Split(s, ":")[1]
  24. s = strings.Trim(s, ":")
  25. return s
  26. }
  27. func randInt(min int, max int) int {
  28. r := rand.Intn(max-min) + min
  29. return r
  30. }
  31. func hashPeer(p Peer) string {
  32. peerString := p.IP + ":" + p.Port
  33. h := sha256.New()
  34. h.Write([]byte(peerString))
  35. return base64.URLEncoding.EncodeToString(h.Sum(nil))
  36. }
  37. func hashBlock(b Block) string {
  38. blockJson, err := json.Marshal(b)
  39. check(err)
  40. blockString := string(blockJson)
  41. h := sha256.New()
  42. h.Write([]byte(blockString))
  43. return base64.URLEncoding.EncodeToString(h.Sum(nil))
  44. }