Skip to content

Commit

Permalink
Testing the implementation of PoW using ring's sha256 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielgusn committed Apr 15, 2024
1 parent bfbf8b7 commit 4861ba5
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ mod block;
use block::block_header::BlockHeader;
use chrono::Utc;
use mining_challenge::read_mempool;
// use sha2::{Sha256, Digest};
use ring::digest::{Context, Digest, SHA256};
use std::fmt::Write;
// use hex_literal::hex;

fn main() {
Expand All @@ -12,6 +13,35 @@ fn main() {
let first_block_header: BlockHeader = BlockHeader::new(String::from("00000000000000000000000000000000"), String::from("00000000000000000000000000000000"), Utc::now(), 128);

println!("{}", first_block_header);

let data: &str = "texto 1234";

let mut nonce: u64 = 0_u64;

loop{

let mut context: Context = Context::new(&SHA256);

let target_hash: String = String::from("0000ffff00000000000000000000000000000000000000000000000000000000");

context.update(data.as_bytes());
context.update(&nonce.to_be_bytes());

let digest: Digest = context.finish();
let mut actual_hex: String = String::new();

for &byte in digest.as_ref() {
write!(&mut actual_hex, "{:02x}", byte).expect("Failed to write hex");
}

println!("Nonce: {}, Hash: {}", nonce, actual_hex);

if actual_hex <= target_hash {
println!("Found the nonce for the target Hash! It is: {} and you can attach this block to the blockchain", nonce);
break
}
nonce += 1;
}
}


0 comments on commit 4861ba5

Please sign in to comment.