lundi 8 juin 2020

if statement keeps triggering

I am fairly new to C# (using it for a crpytographic process). Some help would be greatly appreciated!

I have made a timer that should print out my hash speed every minute. See code below

using System;
using System.Text;
using System.Security.Cryptography;

namespace HashConsoleApp {
  class Program {
    static void Main(string[] args) {
      long Nonce = 19989878659;
      long Noncestart = 19989878659;
      int Tick = 0;
      DateTime start = DateTime.UtcNow;

      while (Tick == 0) {
        string noncestr = Nonce.ToString();
        string plainData = "1" + noncestr + "Sjoerd0000000000000000000000000000000000000000000000000000000000000000";
        string hashedData = ComputeSha256Hash(plainData);

        // if 10-zeroes hash is found, save to disk
        if (hashedData.Substring(0, 10) == "0000000000") {
          Tick = Tick + 1;
          string writestring = "Nonce: " + noncestr + "\n" + "hashed data: " + hashedData;
          System.IO.File.WriteAllText("hash_10.txt", writestring);
        }


        // print hash speed per second, each minute
        DateTime end = DateTime.UtcNow;
        TimeSpan span1 = end.Subtract(start);
        TimeSpan span2 = end.Subtract(start);
        if (span1.Minutes >= 1) {
          long diff = (int)(Nonce - Noncestart) / 60;
          string diffs = diff.ToString();
          Console.Write("Hash speed: " + diffs + " h/s");
          System.IO.File.WriteAllText("test.txt", Nonce.ToString());
          Noncestart = Nonce;
          span1 = TimeSpan.Zero;
        }


        // save Nonce every hour, reset clock
        if (span2.Minutes >= 60) {
          start = DateTime.UtcNow;
          System.IO.File.WriteAllText("hourly_nonce.txt", Nonce.ToString());
          span2 = TimeSpan.Zero;
        }

        //Console.WriteLine("Raw data: {0}", plainData);   
        //Console.WriteLine("Hash {0}", hashedData);  
        //Console.WriteLine(ComputeSha256Hash("1"+noncestr+"Sjoerd0000000000000000000000000000000000000000000000000000000000000000"));
      }
    }

    static string ComputeSha256Hash(string rawData) {
      // Create a SHA256   
      using(SHA256 sha256Hash = SHA256.Create()) {
        // ComputeHash - returns byte array  
        byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));

        // Convert byte array to a string   
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < bytes.Length; i++) {
          builder.Append(bytes[i].ToString("x2"));
        }
        return builder.ToString();
      }
    }
  }
}

However, after the 1 minute mark this repeadetly keeps on printing on my screen. it looks like it gets stuck in the if statement. Is there something wrong with my code?

Aucun commentaire:

Enregistrer un commentaire