-
-
Save RandyMcMillan/f07f19c13ea437aff8f56e899a663f33 to your computer and use it in GitHub Desktop.
sha2-to-png.rs
This file contains hidden or 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 image::{ImageBuffer, Rgb}; | |
use sha2::{Digest, Sha256}; | |
const COLORS: [&str; 256] = [ | |
"#c4b9b8", "#879f84", "#a6814c", "#ff9966", "#aa1155", "#7a81ff", "#8e473b", "#f3dfb6", | |
"#dfac4c", "#4b3621", "#feff32", "#a57c5b", "#52b4ca", "#fffc79", "#879877", "#bb1133", | |
"#eee9d9", "#52b4d3", "#ffff81", "#4f4554", "#c68f65", "#d2e7ca", "#0000ff", "#2ee8bb", | |
"#eebb88", "#eddd59", "#7f5f00", "#eeaa11", "#35fa00", "#ffefd6", "#bb11aa", "#dab4cc", | |
"#fff0db", "#987d73", "#c4fff7", "#eae0c8", "#e2c779", "#ff4d00", "#334d41", "#f83800", | |
"#ddedbd", "#ffdd44", "#efd7ab", "#66eeee", "#aa3333", "#006663", "#ffb75f", "#c6bb9c", | |
"#685c53", "#fff1dc", "#313390", "#ff6f01", "#8f8ce7", "#cf0234", "#ff9966", "#5f5537", | |
"#342931", "#f6eeed", "#fe019a", "#772299", "#ff6ec7", "#f2e6e1", "#ffb49b", "#252525", | |
"#dcf1c7", "#674876", "#eeaa55", "#c7031e", "#7a2e4d", "#9f0000", "#eed683", "#e6dee6", | |
"#fdd7e4", "#95859c", "#bfa58a", "#16141c", "#7b9a6d", "#cc6666", "#ddece0", "#e398af", | |
"#004488", "#fea993", "#6d5698", "#ef1de7", "#ff028d", "#eae9e7", "#f6e2ea", "#b8b8f8", | |
"#ccddcc", "#b06500", "#ecebe5", "#7fbb9e", "#88b5c4", "#93c572", "#dddd88", "#805b87", | |
"#698890", "#ebe2cf", "#ee2222", "#c95a49", "#220011", "#f1ebc8", "#006400", "#553b39", | |
"#ffa177", "#cbc5c6", "#738f5d", "#32575d", "#fed55d", "#a2bffe", "#98333a", "#d7e7d0", | |
"#958b84", "#ee0000", "#3b2b2c", "#ff8656", "#f0fff0", "#44232f", "#faebd7", "#786e38", | |
"#080813", "#5b6f55", "#99c5c4", "#332e2e", "#7b4d3a", "#536267", "#eff0d3", "#f4d493", | |
"#fcd917", "#e16233", "#f8e0e7", "#e59b34", "#ebe5d0", "#393540", "#e4f3e0", "#d6d7d2", | |
"#c19a13", "#d29380", "#babfbc", "#3b638c", "#ffe29b", "#99eeff", "#eebb33", "#4a3b6a", | |
"#c6bbdb", "#ab6f60", "#ff9b87", "#b08f42", "#a67283", "#b1832f", "#fedbb7", "#fcd7ba", | |
"#ee3366", "#89a203", "#484a46", "#f878f8", "#b66325", "#c1f80a", "#6dbac0", "#4b373a", | |
"#03012d", "#ff2600", "#555570", "#ff7a00", "#8edacc", "#ffcc77", "#cfac47", "#a15325", | |
"#ff4466", "#f9f1dd", "#816d5e", "#5f6957", "#f3e0d8", "#006380", "#c26157", "#f9e3b4", | |
"#c88ca4", "#b87333", "#ea9073", "#ee1133", "#e4d9c5", "#dbe7e3", "#ff0e0e", "#eebe1b", | |
"#d8caa9", "#9bc2b1", "#b0003c", "#bae5d6", "#e38fac", "#f6cbca", "#ee4433", "#73383c", | |
"#3f4250", "#000066", "#ffff33", "#bda58b", "#e0b0ff", "#acddaf", "#a9afaa", "#cee1f2", | |
"#7ad7ad", "#e5dae1", "#906a54", "#938b4b", "#86c4da", "#a17a83", "#008e80", "#ebebeb", | |
"#883377", "#9a0eea", "#11cc55", "#eecc44", "#aaff32", "#e2c9ce", "#f2ab46", "#fd8f79", | |
"#92898a", "#787489", "#a6ab9b", "#cef0cc", "#443388", "#fedc57", "#ed4b00", "#a58459", | |
"#553311", "#ffb07c", "#d4ffff", "#534b4f", "#74857f", "#007fff", "#332266", "#bc6f37", | |
"#aaaa77", "#cce2f3", "#fff8dc", "#5c5d5d", "#f3f4d9", "#c7bba4", "#cf758a", "#fbec5d", | |
"#fffbf8", "#b7d2e3", "#eeee66", "#f6efe1", "#ffe39b", "#4e312d", "#e75480", "#bbb3a2", | |
"#3c2f23", "#8f7f85", "#e3cdc2", "#f3efcd", "#152eff", "#d9eae5", "#dabe82", "#ce2029", | |
]; | |
fn hex_to_rgb(hex: &str) -> Option<Rgb<u8>> { | |
if !hex.starts_with('#') || hex.len() != 7 { | |
return None; | |
} | |
let r = u8::from_str_radix(&hex[1..3], 16).ok()?; | |
let g = u8::from_str_radix(&hex[3..5], 16).ok()?; | |
let b = u8::from_str_radix(&hex[5..7], 16).ok()?; | |
Some(Rgb([r, g, b])) | |
} | |
fn hash_to_color_indices(hash: &[u8]) -> Vec<usize> { | |
hash.iter().map(|&byte| byte as usize % COLORS.len()).collect() | |
} | |
fn create_image_from_indices(indices: &[usize], width: u32, height: u32) -> ImageBuffer<Rgb<u8>, Vec<u8>> { | |
let mut img = ImageBuffer::new(width, height); | |
let num_pixels = width * height; | |
for y in 0..height { | |
for x in 0..width { | |
let pixel_index = (y * width + x) as usize; | |
let color_index = indices[pixel_index % indices.len()]; | |
if let Some(color) = hex_to_rgb(COLORS[color_index]) { | |
img.put_pixel(x, y, color); | |
} else { | |
// Fallback to black if color conversion fails | |
img.put_pixel(x, y, Rgb([0, 0, 0])); | |
} | |
} | |
} | |
img | |
} | |
fn sha256_to_png(hash_str: &str, output_path: &str, width: u32, height: u32) -> Result<(), image::ImageError> { | |
let mut hasher = Sha256::new(); | |
hasher.update(hash_str.as_bytes()); | |
let result = hasher.finalize(); | |
let hash_bytes = result.as_slice(); | |
let color_indices = hash_to_color_indices(hash_bytes); | |
let image = create_image_from_indices(&color_indices, width, height); | |
image.save(output_path) | |
} | |
fn main() -> Result<(), image::ImageError> { | |
let hash_input = "This is some data to hash"; | |
let output_file = "hash_image.png"; | |
let image_width = 256; | |
let image_height = 128; | |
println!("Hashing: {}", hash_input); | |
println!("Creating image: {}", output_file); | |
sha256_to_png(hash_input, output_file, image_width, image_height)?; | |
println!("Image created successfully!"); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment