-
-
Save connorslade/5377d7f5aca4efebdb390b47ca24f972 to your computer and use it in GitHub Desktop.
Cringe Compress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "cringe_compress" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
itertools = "0.10.3" | |
byteorder = "1.4.3" | |
sha2 = "0.10.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs; | |
use std::io::Cursor; | |
use std::path::Path; | |
use byteorder::{BigEndian, ReadBytesExt}; | |
use itertools::Itertools; | |
use sha2::{Digest, Sha256}; | |
fn main() { | |
// compress("test.txt"); | |
decompress("test.txt.cc"); | |
} | |
fn compress<T>(file: T) | |
where | |
T: AsRef<Path>, | |
{ | |
let inp = fs::read(&file).unwrap(); | |
let size = inp.len(); | |
let mut hasher = Sha256::new(); | |
hasher.update(inp); | |
let hash = hasher.finalize().to_vec(); | |
let mut out = hash; | |
out.extend((size as u32).to_be_bytes()); | |
let mut path = file.as_ref().to_string_lossy().to_string(); | |
path.push_str(".cc"); | |
fs::write(path, out).unwrap(); | |
} | |
fn decompress<T>(file: T) | |
where | |
T: AsRef<Path>, | |
{ | |
let inp = fs::read(&file).unwrap(); | |
let parts = inp.split_at(32); | |
let mut rdr = Cursor::new(parts.1); | |
let size = rdr.read_u32::<BigEndian>().unwrap(); | |
let hash = parts | |
.0 | |
.iter() | |
.map(|b| format!("{:02x}", b)) | |
.collect::<Vec<String>>() | |
.join(""); | |
println!("Length: {}", size); | |
println!("Hash: {}", hash); | |
let hash = hash.bytes().collect_vec(); | |
for i in (0..=255).permutations(size as usize) { | |
let mut hasher = Sha256::new(); | |
hasher.update(&i); | |
let this_hash = hasher.finalize().to_vec(); | |
if this_hash == hash { | |
println!("Found!"); | |
fs::write("OUT.txt", i).unwrap(); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment