rust sha256 solver
cargo run file.txt 2

use sha256::{digest};
use std::fs::File;
use std::io::prelude::*;
use std::env;

fn main() -> std::io::Result<()> {
  let args: Vec<String> = env::args().collect();
  let filename = &args[1];
  let max: u32 = args[2].parse().unwrap();

  let mut file = File::open(filename)?;
  let mut contents = String::new();
  file.read_to_string(&mut contents)?;

  for i in 0..100000 {
    let s: String = i.to_string();
    let mut contents_n = contents.clone();
    contents_n += &s;

    let hash = digest(contents_n);
    let mut chars = hash.chars();


    for i in 0..max {
      let c = chars.next().unwrap();

      if c.eq(&'0') {
        //println!("{} {} {}", c, i, max);
        if i + 1 == max {
          println!("{}", hash);
          return Ok(())
        }
      } else {
        break
      }
    }
  }

  Ok(())
}
Edit
Pub: 02 Feb 2024 03:17 UTC
Views: 155