└── togett /togett: -------------------------------------------------------------------------------- 1 | use sha2::{Digest, Sha256}; 2 | use std::time::{SystemTime, UNIX_EPOCH}; 3 | 4 | #[derive(Debug)] 5 | struct Block { 6 | index: u64, 7 | timestamp: u64, 8 | data: String, 9 | previous_hash: String, 10 | hash: String, 11 | } 12 | 13 | impl Block { 14 | fn new(index: u64, data: String, previous_hash: String) -> Self { 15 | let timestamp = SystemTime::now() 16 | .duration_since(UNIX_EPOCH) 17 | .unwrap() 18 | .as_secs(); 19 | let mut hasher = Sha256::new(); 20 | hasher.update(format!("{}{}{}", index, timestamp, &previous_hash)); 21 | let hash = format!("{:x}", hasher.finalize()); 22 | 23 | Self { 24 | index, 25 | timestamp, 26 | data, 27 | previous_hash, 28 | hash, 29 | } 30 | } 31 | } 32 | 33 | fn main() { 34 | let genesis_block = Block::new(0, "Genesis Block".to_string(), "0".to_string()); 35 | println!("{:?}", genesis_block); 36 | } 37 | --------------------------------------------------------------------------------