Functional Weave
Code in Rust

crypto.sha1@1.0.0

impl/rust.rs

2,662 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one

/// FIPS 180-4 section 5.3.1: the initial hash value.
const H0: [u32; 5] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];

fn compress(h: &mut [u32; 5], block: &[u8]) {
    let mut w = [0u32; 80];
    for t in 0..16 {
        w[t] = u32::from_be_bytes([block[t * 4], block[t * 4 + 1], block[t * 4 + 2], block[t * 4 + 3]]);
    }
    for t in 16..80 {
        w[t] = (w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16]).rotate_left(1);
    }
    let [mut a, mut b, mut c, mut d, mut e] = *h;
    for (t, &wt) in w.iter().enumerate() {
        let (f, k) = match t {
            0..=19 => ((b & c) | (!b & d), 0x5a827999u32),
            20..=39 => (b ^ c ^ d, 0x6ed9eba1),
            40..=59 => ((b & c) | (b & d) | (c & d), 0x8f1bbcdc),
            _ => (b ^ c ^ d, 0xca62c1d6),
        };
        let temp = a.rotate_left(5).wrapping_add(f).wrapping_add(e).wrapping_add(k).wrapping_add(wt);
        e = d;
        d = c;
        c = b.rotate_left(30);
        b = a;
        a = temp;
    }
    for (slot, v) in h.iter_mut().zip([a, b, c, d, e]) {
        *slot = slot.wrapping_add(v);
    }
}

/// The SHA-1 digest of a byte string (FIPS 180-4 section 6.1), with the
/// standard library only. Only for HMAC-SHA1 in HOTP/TOTP: SHA-1 is broken
/// for collisions.
///
/// # Panics
/// Panics if any value is outside 0-255.
pub fn sha1(bytes: &[i64]) -> Vec<i64> {
    let mut message: Vec<u8> = Vec::with_capacity(bytes.len() + 72);
    for &b in bytes {
        if !(0..=255).contains(&b) {
            panic!("bytes must be a list of integers from 0 to 255");
        }
        message.push(b as u8);
    }
    // Padding (section 5.1.1): a 1 bit, zeros, then the length in bits as a
    // 64-bit big-endian integer, to a multiple of 64 bytes.
    let bit_length = (bytes.len() as u64).wrapping_mul(8);
    message.push(0x80);
    while message.len() % 64 != 56 {
        message.push(0);
    }
    message.extend_from_slice(&bit_length.to_be_bytes());

    let mut h = H0;
    for block in message.chunks_exact(64) {
        compress(&mut h, block);
    }
    h.iter().flat_map(|word| word.to_be_bytes()).map(|b| b as i64).collect()
}

pub fn fune_vector(args: &[Value]) -> Value {
    let bytes: Vec<i64> = match &args[0] {
        Value::Arr(items) => items
            .iter()
            .map(|item| match item {
                Value::Int(i) => *i,
                _ => panic!("bytes must be a list of integers from 0 to 255"),
            })
            .collect(),
        _ => panic!("bytes must be a list of integers from 0 to 255"),
    };
    Value::Arr(sha1(&bytes).into_iter().map(Value::Int).collect())
}