visualhash/
lib.rs

1extern crate argon2;
2extern crate image;
3
4/// Convert some bytes into a (hopefully) unique looking image.
5/// The returned image can easily be saved to a file with `.save("filename.format")`.
6pub fn visualize(key: &[u8]) -> Result<image::RgbImage, argon2::Error> {
7    let hash = hash(key)?;
8    let img = generate_image(6, 6, &hash);
9
10    Ok(img)
11}
12
13/// Use KDF (to increase expense to find keys with valid look-alike images)
14/// with consistent parameters (to be deterministic).
15fn hash(key: &[u8]) -> Result<Vec<u8>, argon2::Error> {
16    let salt: &[u8] = &[0; 8];
17    let config = argon2::Config {
18        variant: argon2::Variant::Argon2d,
19        version: argon2::Version::Version13,
20        mem_cost: 65536,
21        time_cost: 12,
22        lanes: 4,
23        thread_mode: argon2::ThreadMode::Parallel,
24        secret: &[],
25        ad: &[],
26        hash_length: 128
27    };
28    let hash = argon2::hash_raw(key, salt, &config)?;
29
30    Ok(hash)
31}
32
33/// For each RGB value for each pixel in the image, use a byte from the hash.
34/// Therefore, the length of the data must exceed the number of pixels.
35fn generate_image(height: u32, width: u32, data: &Vec<u8>) -> image::RgbImage {
36    assert!(data.len() as u32 >= height*width);
37    let mut img: image::RgbImage = image::ImageBuffer::new(width, height);
38    let mut data_iter = data.iter();
39    for (_x, _y, pixel) in img.enumerate_pixels_mut() {
40        let r = data_iter.next().unwrap();
41        let g = data_iter.next().unwrap();
42        let b = data_iter.next().unwrap();
43        *pixel = image::Rgb([*r, *g, *b]);
44    }
45
46    img
47}