Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. // Step 1: Generate 32 random bytes
  2. rand32 := make([]byte, 32)
  3. _, err = rand.Read(rand32)
  4. if err != nil {
  5. // TODO Handle error
  6. }
  7.  
  8. // Step 2: Encode bytes in Base64 and remove special characters
  9. nonce := base64.StdEncoding.EncodeToString(rand32)
  10. re := regexp.MustCompile("[^a-zA-Z0-9]")
  11. nonce = re.ReplaceAllString(nonce, "")
  12.  
  13. // Step 3: Build the base string: {Public key of pool}-{Nonce}-{Block id}-{Difficulty}
  14. base := fmt.Sprintf("%s-%s-%s-%s", info.Data.PublicKey, nonce, info.Data.Block, info.Data.Difficulty)
  15.  
  16. // Step 4: Derive argon key from base (parameters are fixed)
  17. argonBytes := argon2.Key([]byte(base), make([]byte, 0), 1, 524288, 1, 32)
  18. argon := base64.StdEncoding.EncodeToString(argonBytes)
  19.  
  20. // Step 5: Create the initial hash
  21. hash := fmt.Sprintf("%s%s", base, argon)
  22. hashBytes := []byte(hash)
  23.  
  24. // Step 6: Hash with SHA-512 five times
  25. digester := sha512.New()
  26. for i := 0; i < 5; i++ {
  27. _, err := digester.Write(hashBytes)
  28. if err != nil {
  29. log.Fatalf("Hashing with SHA-512 failed: %s", err.Error())
  30. }
  31. hashBytes = digester.Sum(nil)
  32. digester.Reset()
  33. }
  34.  
  35. // Step 7: Convert the final hash into a hex string with 128 characters
  36. hashHex := fmt.Sprintf("%x", hashBytes)
  37.  
  38. // Step 8: Converts pairs of hex characerters into decimals. The final
  39. // slice has 64 elements.
  40. m := make([]int64, 0)
  41. for i := 0; i < len(hashHex); i = i + 2 {
  42. dec, err := strconv.ParseInt(string(hashHex[i:i+2]), 16, 64)
  43. if err != nil {
  44. log.Fatalf("Converting hex into dec failed: %s", err.Error())
  45. }
  46. m = append(m, dec)
  47. }
  48.  
  49. // Step 9: Create the duration by concating selected components
  50. durationString := fmt.Sprintf("%d%d%d%d%d%d%d%d", m[10], m[15], m[20], m[23], m[31], m[40], m[45], m[55])
  51.  
  52. // Step 10: Parse the duration into an integer. We don't care if the
  53. // duration is too big for int64. To find a valid nonce we need a duration
  54. // smaller than the max value of int64 anyway.
  55. duration, _ := strconv.ParseInt(durationString, 10, 64)
  56.  
  57. // Step 11: Finally calculate the result
  58. diff, err := strconv.ParseInt(info.Data.Difficulty, 10, 64)
  59. if err != nil {
  60. log.Fatalf("Parsing to integer failed: ", err.Error())
  61. }
  62. result := duration / diff
  63.  
  64. // Step 12: Check if we found a valid result
  65. if result > 0 && result <= int64(info.Data.Limit) {
  66. log.Println("Found a nonce!")
  67. submit, err := adapter.SubmitNonce(argon[30:], nonce, info.Data.PublicKey, "4iokkZdd6yPeeCu14S4EQ62ZuhBaND7mY94q23itVY3nWrGrMAApQyDfc7HZWWxkebc7sLEYyQADPkEmKhCd3wdf")
  68. if err != nil {
  69. fmt.Printf("Submitting a share failed: %s\n", err.Error())
  70. }
  71. fmt.Printf("Response: %v\n", submit)
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement