0

¿como podria hacer un hash en formato de vector, con un vector de bytes? (sin la libreria crypto-hash)

Yo tengo un vector u8 con bytes y necesitaria un vector u8 con sha256 .

use sha2::{Sha256, Digest};

pub trait Hashable {
    fn bytes (&self) -> Vec<u8>;

    /// NEED TO IMPLEMENT THE HASH
    fn hash (&self) -> Vec<u8>{
        let mut hasher = Sha256::new();
        hasher.update(&self.bytes);
        let result = hasher.finalize();
        println!("{:?}", result);
    }
}

1 respuesta 1

0
use sha256::digest_bytes;

fn vector_to_sha256(vector: Vec<u8>) -> Vec<u8> {
    let val: String = digest_bytes(&vector).to_owned();
    let result: Vec<u8> = val.into_bytes();
    return result;
}

pub trait Hashable {
    fn bytes (&self) -> Vec<u8>;
    fn hash (&self) -> Vec<u8>{
        vector_to_sha256(self.bytes())
    }
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.