wireguard-vanity-key/src/main.rs

103 lines
2.8 KiB
Rust
Raw Permalink Normal View History

2021-11-03 07:34:48 +00:00
use std::{
2021-11-03 13:21:29 +00:00
fs::OpenOptions,
io::Write,
pin::Pin,
2021-11-03 07:34:48 +00:00
thread::{spawn, JoinHandle},
time::Duration,
};
mod cpu;
pub type Key = [u8; 32];
struct ProgressCounter(pub *mut u64);
unsafe impl Send for ProgressCounter {}
unsafe impl Sync for ProgressCounter {}
impl ProgressCounter {
2021-11-03 13:21:29 +00:00
fn incr(&mut self, n: u64) {
2021-11-03 07:34:48 +00:00
unsafe {
2021-11-03 13:21:29 +00:00
*self.0 += n;
2021-11-03 07:34:48 +00:00
}
}
}
fn work(mut progress: ProgressCounter) -> JoinHandle<()> {
spawn(move || {
let mut rng = {
use rand::SeedableRng;
rand_xoshiro::Xoshiro256StarStar::from_rng(rand::thread_rng()).unwrap()
};
2021-11-03 13:21:29 +00:00
const ITERS: u64 = 256;
2021-11-03 07:34:48 +00:00
loop {
2021-11-03 13:21:29 +00:00
for _ in 0..ITERS {
let key = cpu::generate_privkey(&mut rng);
let pub_key = cpu::get_pubkey(&key);
// b64(pub_key).starts_with("char") && b64(pub_key)[4] in ["+", "/"]
if pub_key[0] == 0x72
&& pub_key[1] == 0x16
&& pub_key[2] == 0xab
&& pub_key[3] >= 0xf8
{
let key_str = base64::encode(key);
let pub_key_str = base64::encode(pub_key);
println!("{} -> {}", key_str, pub_key_str);
2021-11-03 07:34:48 +00:00
2021-11-03 13:21:29 +00:00
{
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open("keys.log")
.unwrap();
2021-11-03 07:34:48 +00:00
2021-11-03 13:21:29 +00:00
let _ = writeln!(file, "{} -> {}", key_str, pub_key_str);
}
}
2021-11-03 07:34:48 +00:00
}
2021-11-03 13:21:29 +00:00
progress.incr(ITERS);
2021-11-03 07:34:48 +00:00
}
})
}
2021-11-03 13:21:29 +00:00
const NUM_THREADS: usize = 24;
2021-11-03 07:34:48 +00:00
2021-11-03 13:21:29 +00:00
struct Progress(Pin<Box<[u64; NUM_THREADS]>>);
2021-11-03 07:34:48 +00:00
impl Progress {
fn new() -> Self {
2021-11-03 13:21:29 +00:00
Progress(Box::pin([0; NUM_THREADS]))
}
unsafe fn the_pointer(&mut self) -> *mut u64 {
self.0.as_mut_ptr()
}
fn total(&self) -> u64 {
self.0.iter().sum()
2021-11-03 07:34:48 +00:00
}
}
fn main() {
2021-11-03 13:21:29 +00:00
let mut progress = Progress::new();
2021-11-03 07:34:48 +00:00
let _handles: Vec<_> = (0..NUM_THREADS)
.map(|id| {
2021-11-03 13:21:29 +00:00
let ptr = unsafe { progress.the_pointer().add(id) };
2021-11-03 07:34:48 +00:00
let progress = ProgressCounter(ptr);
work(progress)
})
.collect();
let mut last_total_progress = 0;
2021-11-03 13:21:29 +00:00
let mut sleep_secs = 1.0;
2021-11-03 07:34:48 +00:00
loop {
2021-11-03 13:21:29 +00:00
let total_progress = progress.total();
2021-11-03 07:34:48 +00:00
let delta = total_progress - last_total_progress;
last_total_progress = total_progress;
2021-11-03 13:21:29 +00:00
println!("Guesses/sec ≈ {:.0}", delta as f64 / sleep_secs);
std::thread::sleep(Duration::from_secs_f64(sleep_secs));
sleep_secs *= 1.05;
2021-11-03 07:34:48 +00:00
}
}