pub fn process_line_integrity<'a>(line: &'a str, integrity: &mut Integrity, line_num: usize) -> Result<&'a str, IntegrityError> {
    let data_index = line.rfind(' ').ok_or(IntegrityError::NoExpectedHash { line_num })?;
    let line_data = &line[..data_index];
    let hash_in_file = line[data_index + 1..].trim_end();

    match integrity {
        Integrity::Crc32 => {
            let crc = crc32::checksum_ieee(line_data.as_bytes());
            if crc.to_string() != hash_in_file {
                return Err(IntegrityError::Crc32Error { line_num });
            }
        },
        Integrity::Sha1Chain(hash_of_prev) => {
            let mut current_hash: [u8; 20]  = [0; 20];
            blockchain_sha1(&hash_of_prev[..], line_data.as_bytes(), &mut current_hash);
            if hex::encode(current_hash) != hash_in_file {
                return Err(IntegrityError::Sha1ChainError { line_num });
            }
            *hash_of_prev = current_hash;
        },
        Integrity::Sha256Chain(hash_of_prev) => {
            let mut current_hash: [u8; 32]  = [0; 32];
            blockchain_sha256(&hash_of_prev[..], line_data.as_bytes(), &mut current_hash);
            if hex::encode(current_hash) != hash_in_file {
                return Err(IntegrityError::Sha256ChainError { line_num });
            }
            *hash_of_prev = current_hash;
        },
    }

    Ok(line_data)
}
/// Check data integrity after read from file.
Check data integrity after read from file.
Check data integrity after read from file .
pub fn process_line_integrity<'a>(line: &'a str, integrity: &mut Integrity, line_num: usize) -> Result<&'a str, IntegrityError> {
    let data_index = line.rfind(' ').ok_or(IntegrityError::NoExpectedHash { line_num })?;
    let line_data = &line[..data_index];
    let hash_in_file = line[data_index + 1..].trim_end();
    match integrity {
        Integrity::Crc32 => {
            let crc = crc32::checksum_ieee(line_data.as_bytes());
            if crc.to_string() != hash_in_file {
                return Err(IntegrityError::Crc32Error { line_num });
            }
        },
        Integrity::Sha1Chain(hash_of_prev) => {
            let mut current_hash: [u8; 20]  = [0; 20];
            blockchain_sha1(&hash_of_prev[..], line_data.as_bytes(), &mut current_hash);
            if hex::encode(current_hash) != hash_in_file {
                return Err(IntegrityError::Sha1ChainError { line_num });
            }
            *hash_of_prev = current_hash;
        },
        Integrity::Sha256Chain(hash_of_prev) => {
            let mut current_hash: [u8; 32]  = [0; 32];
            blockchain_sha256(&hash_of_prev[..], line_data.as_bytes(), &mut current_hash);
            if hex::encode(current_hash) != hash_in_file {
                return Err(IntegrityError::Sha256ChainError { line_num });
            }
            *hash_of_prev = current_hash;
        },
    }
    Ok(line_data)
}
pub fn process_line_integrity < ' a > ( line : & ' a str , integrity : & mut Integrity , line_num : usize ) -> Result < & ' a str , IntegrityError > { let data_index = line . rfind ( ' ' ) . ok_or ( IntegrityError :: NoExpectedHash { line_num } ) ? ; let line_data = & line [ .. data_index ] ; let hash_in_file = line [ data_index + 1 .. ] . trim_end ( ) ; match integrity { Integrity :: Crc32 => { let crc = crc32 :: checksum_ieee ( line_data . as_bytes ( ) ) ; if crc . to_string ( ) != hash_in_file { return Err ( IntegrityError :: Crc32Error { line_num } ) ; } } , Integrity :: Sha1Chain ( hash_of_prev ) => { let mut current_hash : [ u8 ; 20 ] = [ 0 ; 20 ] ; blockchain_sha1 ( & hash_of_prev [ .. ] , line_data . as_bytes ( ) , & mut current_hash ) ; if hex :: encode ( current_hash ) != hash_in_file { return Err ( IntegrityError :: Sha1ChainError { line_num } ) ; } * hash_of_prev = current_hash ; } , Integrity :: Sha256Chain ( hash_of_prev ) => { let mut current_hash : [ u8 ; 32 ] = [ 0 ; 32 ] ; blockchain_sha256 ( & hash_of_prev [ .. ] , line_data . as_bytes ( ) , & mut current_hash ) ; if hex :: encode ( current_hash ) != hash_in_file { return Err ( IntegrityError :: Sha256ChainError { line_num } ) ; } * hash_of_prev = current_hash ; } , } Ok ( line_data ) }
Check data integrity after read from file.
Check data integrity after read from file .
line
&'a str
integrity
&mut Integrity
line_num
usize
line
&'a str
integrity
&mut Integrity
line_num
usize