alephium-web3 2.0.7

A Rust library to interact with the Alephium platform
Documentation
// Copyright 2018 - 2022 The Alephium Authors
// This file is part of the alephium project.

/// DJB2 hash algorithm
pub fn djb2(bytes: &[u8]) -> u32 {
    let mut hash: u32 = 5381;
    for &byte in bytes {
        hash = hash.wrapping_mul(33).wrapping_add(byte as u32);
    }
    hash
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_djb2() {
        let result = djb2(b"hello");
        // DJB2 hash wraps around for 32-bit
        assert_eq!(result, 261238937);
        
        let empty = djb2(&[]);
        assert_eq!(empty, 5381);
    }

    #[test]
    fn test_djb2_consistency() {
        let data = b"test data";
        let hash1 = djb2(data);
        let hash2 = djb2(data);
        assert_eq!(hash1, hash2);
    }
}