My first ever Rust program!
Here, for posterity, is my first ever Rust program. It checks the key log on the Upspin Key Server. extern crate sha2; extern crate reqwest; use std::io::Read; use sha2::{Sha256, Digest}; fn main() { let mut resp = match reqwest::get("https://key.upspin.io/log") { Ok(resp) => resp, Err(e) => panic!(e), }; assert!(resp.status().is_success()); let mut content = String::new(); match resp.read_to_string(&mut content) { Ok(_) => {} Err(e) => panic!(e), } let mut hasher = Sha256::default(); let mut ct = 0; let mut last_hash = "".to_string(); for line in content.lines() { if line.starts_with("SHA256:") { let mut fields = line.split(":"); // skip first token match fields.next() { Some(_) => {} _ => { println!("Bad SHA256 line: {}", line); continue; } }; last_hash = fields.next().unwrap().to_string(); let expected = string_to_u8_array(&last_hash); let output = hasher.result(); assert_eq!(output.as_slice(), expected.as_slice()); } else { hasher = Sha256::default(); hasher.input(line.as_bytes()); let newline = "\n".as_bytes(); hasher.input(newline); if last_hash != "" { hasher.input(last_hash.as_bytes()); } } ct += 1; println!("Line {}", ct); } } use std::u8; fn string_to_u8_array(hex: &String) -> Vec { // Make vector of bytes from octets let mut bytes = Vec::new(); for i in 0..(hex.len() / 2) { let res = u8::from_str_radix(&hex[2 * i..2 * i + 2], 16); match res { Ok(v) => bytes.push(v), Err(e) => { println!("Problem with hex: {}", e); return bytes; } }; } return bytes; } I found myself sprinkling mut’s and unpack()’s here and there like the mutation unpacking fairy, hoping something would work. I don’t think that how you are supposed to do it, but we’ll see. ...