Skip to content

Instantly share code, notes, and snippets.

@Chaz6
Created June 20, 2024 18:04
Show Gist options
  • Save Chaz6/81523edaed6f31aa609daa919ff6b8cc to your computer and use it in GitHub Desktop.
Save Chaz6/81523edaed6f31aa609daa919ff6b8cc to your computer and use it in GitHub Desktop.
SHAllenge in Rust
[package]
name = "shallenge"
version = "0.1.0"
edition = "2021"
[dependencies]
sha2 = "0.11.0-pre.3"
use sha2::{Digest, Sha256};
use std::env;
use std::process::exit;
use std::str;
fn main() {
let args: Vec<String> = env::args().collect();
let tmp1 = args[1].clone() + "/";
let prefix = tmp1.as_bytes();
let res = &args[2].parse::<i32>();
if let Err(_e) = res {
println!("{} is not a number!", &args[2]);
exit(1);
}
let start = res.as_ref().unwrap();
let res = &args[3].parse::<i32>();
if let Err(_e) = res {
println!("{} is not a number!", &args[3]);
exit(1);
}
let end = res.as_ref().unwrap();
if end <= start {
println!("end must be greater than start!")
}
let mut lowest: [u8; 32] = [255; 32];
let mut i = start.clone();
while i < *end {
let mut hasher = Sha256::new();
let vec1 = [&prefix[..], &i.to_string().as_bytes()].concat();
hasher.update(vec1.clone());
let result = hasher.finalize();
let hash = result.to_vec();
if result < lowest.into() {
let tmp = str::from_utf8(&vec1);
lowest = result.into();
println!("{} {}", tmp.unwrap(), hex::encode(&hash));
}
i = i + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment