Skip to content

Latest commit

 

History

History
41 lines (30 loc) · 3.16 KB

rust_hashing_code_detailed.md

File metadata and controls

41 lines (30 loc) · 3.16 KB

You can alter the definition of XorHasher with the following fields. This XorHasher is for demonstration purpose. You can create a new Hasher type with the following fields which may potentially affect hash value of the input.

  1. state: [u32; 8]:

    • The state field is an array of 8 u32 values that represent the internal state of the hash function.
    • It is initialized with specific initial values in the new() function.
    • During the hash computation process, the state is iteratively updated based on the input data.
    • The final hash value is derived from the state after processing all the input data.
  2. block: [u8; BLOCK_SIZE]:

    • The block field is an array of BLOCK_SIZE bytes (64 bytes in this case) that serves as a buffer to store the input data.
    • It is used to accumulate the input data until a full block is obtained.
    • When a full block is available, it is processed by the process_block() function to update the state.
    • After processing a block, the block is cleared or reused for the next block of data.
  3. block_len: usize:

    • The block_len field keeps track of the number of bytes currently stored in the block buffer.
    • It is used to determine when a full block of data is available for processing.
    • When block_len reaches BLOCK_SIZE, it indicates that a complete block is ready to be processed by the process_block() function.
    • After processing a block, block_len is reset to 0 to start accumulating the next block of data.
  4. length: u64:

    • The length field represents the total number of bytes processed by the hash function so far.
    • It keeps track of the cumulative length of the input data.
    • The length is updated whenever new data is added to the block buffer.
    • In the finalize() function, the length is used to append the message length as a 64-bit value to the final block before processing.

You can add or remove fields as you desire.

These fields work together to maintain the state and handle the input data during the hash computation process:

  • The update() function takes the input data and incrementally fills the block buffer. It updates block_len and length accordingly. When a full block is available, it calls process_block() to update the state based on the current block.

  • The process_block() function performs the core hash computation logic. It takes the current block and updates the state based on the block data. The specific processing logic depends on the hash algorithm being implemented.

  • The finalize() function is called to complete the hash computation. It appends the padding bytes and the message length to the final block, processes the final block, and returns the resulting hash value derived from the state.

The state, block, block_len, and length fields collectively maintain the necessary information and data structures to perform the hash computation incrementally and produce the final hash value.

It's important to note that the provided code is a simplified example and does not represent a complete or secure hash function implementation. It is meant to illustrate the basic structure and components of a hash function.