pub fn break_sha1_length_extension(
    b: &[u8],
    k: &[u8],
    digest: &[u8],
) -> Result<Vec<u8>, crate::Error> {
    let suffix = b";admin=true";

    let digest = hex::decode(digest).unwrap();

    let mut H = Vec::new();
    for chunk in digest.chunks(4) {
        H.push(u32::from_be_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]));
    }

    for l in 0..256 {
        // (key || message || padding || new-msg)
        let mut new_msg = Vec::from(vec![0 as u8; l]);
        new_msg.extend_from_slice(b);
        new_msg = md_pad(&new_msg);
        new_msg.extend_from_slice(suffix);

        let new_digest = crate::sha1::sha1(
            suffix,
            Some(new_msg.len() * 8),
            Some(H[0]),
            Some(H[1]),
            Some(H[2]),
            Some(H[3]),
            Some(H[4]),
        );

        if crate::sha1::sha1_mac(k, &new_msg[l..]) == new_digest {
            return Ok(new_digest);
        }
    }

    Err(crate::Error::FailedToCrackKey)
}
/// break_sha1_length_extension breaks the sha 1 using length extension
///
/// Solution for problem 5 from set 4: <https://cryptopals.com/sets/4/challenges/29>
break_sha1_length_extension breaks the sha 1 using length extension
Solution for problem 5 from set 4.
break_sha1_length_extension breaks the sha 1 using length extension Solution for problem 5 from set 4 .
pub fn break_sha1_length_extension(
    b: &[u8],
    k: &[u8],
    digest: &[u8],
) -> Result<Vec<u8>, crate::Error> {
    let suffix = b";admin=true";
    let digest = hex::decode(digest).unwrap();
    let mut H = Vec::new();
    for chunk in digest.chunks(4) {
        H.push(u32::from_be_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]));
    }
    for l in 0..256 {
        // (key || message || padding || new-msg)
        let mut new_msg = Vec::from(vec![0 as u8; l]);
        new_msg.extend_from_slice(b);
        new_msg = md_pad(&new_msg);
        new_msg.extend_from_slice(suffix);
        let new_digest = crate::sha1::sha1(
            suffix,
            Some(new_msg.len() * 8),
            Some(H[0]),
            Some(H[1]),
            Some(H[2]),
            Some(H[3]),
            Some(H[4]),
        );
        if crate::sha1::sha1_mac(k, &new_msg[l..]) == new_digest {
            return Ok(new_digest);
        }
    }
    Err(crate::Error::FailedToCrackKey)
}
pub fn break_sha1_length_extension ( b : & [ u8 ] , k : & [ u8 ] , digest : & [ u8 ] , ) -> Result < Vec < u8 > , crate :: Error > { let suffix = b" " ; let digest = hex :: decode ( digest ) . unwrap ( ) ; let mut H = Vec :: new ( ) ; for chunk in digest . chunks ( 4 ) { H . push ( u32 :: from_be_bytes ( [ chunk [ 0 ] , chunk [ 1 ] , chunk [ 2 ] , chunk [ 3 ] ] ) ) ; } for l in 0 .. 256 { // (key || message || padding || new-msg) let mut new_msg = Vec :: from ( vec ! [ 0 as u8 l ] ) ; new_msg . extend_from_slice ( b ) ; new_msg = md_pad ( & new_msg ) ; new_msg . extend_from_slice ( suffix ) ; let new_digest = crate :: sha1 :: sha1 ( suffix , Some ( new_msg . len ( ) * 8 ) , Some ( H [ 0 ] ) , Some ( H [ 1 ] ) , Some ( H [ 2 ] ) , Some ( H [ 3 ] ) , Some ( H [ 4 ] ) , ) ; if crate :: sha1 :: sha1_mac ( k , & new_msg [ l .. ] ) == new_digest { return Ok ( new_digest ) ; } } Err ( crate :: Error :: FailedToCrackKey ) }
break_sha1_length_extension breaks the sha 1 using length extension
Solution for problem 5 from set 4: <https://cryptopals.com/sets/4/challenges/29>
break_sha1_length_extension breaks the sha 1 using length extension Solution for problem 5 from set 4 : <https : // cryptopals . com / sets / 4 / challenges / 29 >
b
&[u8]
k
&[u8]
digest
&[u8]