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.

51 lines
1.1 KiB

  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. /*peerJson, err := json.Marshal(p)
  33. check(err)
  34. peerString := string(peerJson)*/
  35. peerString := p.IP + ":" + p.Port
  36. h := sha256.New()
  37. h.Write([]byte(peerString))
  38. return base64.URLEncoding.EncodeToString(h.Sum(nil))
  39. }
  40. func hashBlock(b Block) string {
  41. blockJson, err := json.Marshal(b)
  42. check(err)
  43. blockString := string(blockJson)
  44. h := sha256.New()
  45. h.Write([]byte(blockString))
  46. return base64.URLEncoding.EncodeToString(h.Sum(nil))
  47. }