### Execute TurboSHAKE Examples Source: https://github.com/itzmeanjan/turboshake/blob/master/README.md Run the provided library examples using the make command to verify the TurboSHAKE128 and TurboSHAKE256 implementations. ```bash make example ``` -------------------------------- ### Complete Hashing Workflow Source: https://context7.com/itzmeanjan/turboshake/llms.txt A comprehensive example demonstrating the full lifecycle of TurboSHAKE hashing, including helper functions for both 128 and 256 variants and proper error handling. ```rust use turboshake::{TurboShake128, TurboShake256}; fn hash_with_turboshake128(data: &[u8], output_len: usize) -> Vec { let mut hasher = TurboShake128::default(); hasher.absorb(data).expect("absorption failed"); hasher.finalize::<{ TurboShake128::DEFAULT_DOMAIN_SEPARATOR }>() .expect("finalization failed"); let mut output = vec![0u8; output_len]; hasher.squeeze(&mut output).expect("squeezing failed"); output } fn hash_with_turboshake256(data: &[u8], output_len: usize) -> Vec { let mut hasher = TurboShake256::default(); hasher.absorb(data).expect("absorption failed"); hasher.finalize::<{ TurboShake256::DEFAULT_DOMAIN_SEPARATOR }>() .expect("finalization failed"); let mut output = vec![0u8; output_len]; hasher.squeeze(&mut output).expect("squeezing failed"); output } fn main() { let message = b"The quick brown fox jumps over the lazy dog"; let hash128 = hash_with_turboshake128(message, 32); println!("TurboSHAKE128 (32 bytes): {}", hash128.iter().map(|b| format!("{:02x}", b)).collect::()); let hash256 = hash_with_turboshake256(message, 64); println!("TurboSHAKE256 (64 bytes): {}", hash256.iter().map(|b| format!("{:02x}", b)).collect::()); } ``` -------------------------------- ### Initialize TurboSHAKE Instances Source: https://context7.com/itzmeanjan/turboshake/llms.txt Demonstrates how to instantiate TurboShake128 and TurboShake256 objects. These instances are initialized in the absorption phase and ready for input. ```rust use turboshake::{TurboShake128, TurboShake256}; fn main() { let mut hasher128 = TurboShake128::default(); let mut hasher256 = TurboShake256::default(); } ``` -------------------------------- ### Initialize and Use TurboSHAKE Xof Source: https://github.com/itzmeanjan/turboshake/blob/master/README.md Demonstrates the full lifecycle of a TurboSHAKE hasher, including initialization, absorbing message chunks, finalizing the state with a domain separator, and squeezing the resulting digest. ```rust use turboshake; fn main() { let msg = [1u8; 8]; let mut dig = [0u8; 32]; let mut hasher = turboshake::TurboShake128::default(); // Absorb message chunks hasher.absorb(&msg[..2]).expect("data absorption must not fail"); hasher.absorb(&msg[2..4]).expect("data absorption must not fail"); hasher.absorb(&msg[4..]).expect("data absorption must not fail"); // Finalize state hasher.finalize::<{ turboshake::TurboShake128::DEFAULT_DOMAIN_SEPARATOR }>().expect("finalization must not fail"); // Squeeze output hasher.squeeze(&mut dig[..16]).expect("data squeezing must not fail"); hasher.squeeze(&mut dig[16..]).expect("data squeezing must not fail"); } ``` -------------------------------- ### Configure TurboSHAKE Dependency Source: https://github.com/itzmeanjan/turboshake/blob/master/README.md Add the turboshake crate to your Cargo.toml file to enable the Xof API functionality. ```toml [dependencies] turboshake = "0.5.0" ``` -------------------------------- ### Squeeze Output from TurboSHAKE128 Source: https://context7.com/itzmeanjan/turboshake/llms.txt Demonstrates how to extract arbitrary-length output from a finalized TurboSHAKE128 state. The squeeze method can be called multiple times or in chunks to retrieve the desired amount of output data. ```rust use turboshake::TurboShake128; fn main() { let mut hasher = TurboShake128::default(); hasher.absorb(b"input data").expect("absorption must not fail"); hasher.finalize::<{ TurboShake128::DEFAULT_DOMAIN_SEPARATOR }>() .expect("finalization must not fail"); let mut digest = [0u8; 32]; hasher.squeeze(&mut digest).expect("squeezing must not fail"); println!("32-byte digest: {:02x?}", digest); let mut extended_output = [0u8; 64]; hasher.squeeze(&mut extended_output).expect("squeezing must not fail"); println!("Additional 64 bytes: {:02x?}", extended_output); let mut key_material = vec![0u8; 256]; for chunk in key_material.chunks_mut(32) { hasher.squeeze(chunk).expect("squeezing must not fail"); } } ``` -------------------------------- ### Handle TurboShakeError States Source: https://context7.com/itzmeanjan/turboshake/llms.txt Illustrates how the library enforces state transitions. Attempting to squeeze before finalization, or absorb/finalize after the state is closed, results in a TurboShakeError. ```rust use turboshake::{TurboShake128, TurboShakeError}; fn main() { let mut hasher = TurboShake128::default(); let mut output = [0u8; 32]; let result = hasher.squeeze(&mut output); assert!(result.is_err()); hasher.absorb(b"data").expect("must absorb"); hasher.finalize::<{ TurboShake128::DEFAULT_DOMAIN_SEPARATOR }>() .expect("must finalize"); let result = hasher.absorb(b"more data"); assert!(result.is_err()); let result = hasher.finalize::<{ TurboShake128::DEFAULT_DOMAIN_SEPARATOR }>(); assert!(result.is_err()); } ``` -------------------------------- ### Derive Multiple Keys with TurboShake256 Source: https://context7.com/itzmeanjan/turboshake/llms.txt Shows how to use TurboSHAKE as an extendable output function (XOF) to derive multiple cryptographic keys from a single master secret. This is useful for generating encryption keys, MAC keys, and IVs from one source. ```rust use turboshake::TurboShake256; fn derive_keys(master_secret: &[u8], info: &[u8]) -> ([u8; 32], [u8; 32], [u8; 16]) { let mut hasher = TurboShake256::default(); hasher.absorb(master_secret).expect("absorption failed"); hasher.absorb(info).expect("absorption failed"); hasher.finalize::<{ TurboShake256::DEFAULT_DOMAIN_SEPARATOR }>() .expect("finalization failed"); let mut encryption_key = [0u8; 32]; let mut mac_key = [0u8; 32]; let mut iv = [0u8; 16]; hasher.squeeze(&mut encryption_key).expect("squeeze failed"); hasher.squeeze(&mut mac_key).expect("squeeze failed"); hasher.squeeze(&mut iv).expect("squeeze failed"); (encryption_key, mac_key, iv) } fn main() { let master_secret = b"super_secret_master_key_12345678"; let context = b"application-specific-context"; let (enc_key, mac_key, iv) = derive_keys(master_secret, context); println!("Encryption key: {:02x?}", enc_key); println!("MAC key: {:02x?}", mac_key); println!("IV: {:02x?}", iv); } ``` -------------------------------- ### TurboShake128::default Source: https://context7.com/itzmeanjan/turboshake/llms.txt Creates a new TurboSHAKE128 instance with a 128-bit security level. The hasher is initialized and ready to absorb input data. ```APIDOC ## TurboShake128::default ### Description Creates a new TurboSHAKE128 extendable output function instance with 128-bit security level. The hasher is initialized in the absorption phase, ready to accept input data. ### Method Associated function (constructor) ### Endpoint N/A (Rust function) ### Parameters None ### Request Example ```rust use turboshake::TurboShake128; fn main() { // Create a new TurboSHAKE128 instance let mut hasher = TurboShake128::default(); // The hasher is now ready to absorb data // Security level: 128 bits // Rate: 168 bytes (1344 bits) } ``` ### Response #### Success Response (200) N/A (Rust object creation) #### Response Example N/A ``` -------------------------------- ### Perform Incremental Hashing with TurboShake128 Source: https://context7.com/itzmeanjan/turboshake/llms.txt Demonstrates how to process large data streams or files by absorbing chunks incrementally. This approach is memory-efficient for handling data that does not fit into a single buffer. ```rust use turboshake::TurboShake128; fn main() { let mut hasher = TurboShake128::default(); let file_chunks = [ b"First chunk of data...".as_slice(), b"Second chunk of data...".as_slice(), b"Final chunk of data.".as_slice(), ]; for chunk in file_chunks { hasher.absorb(chunk).expect("absorption must not fail"); } hasher.finalize::<{ TurboShake128::DEFAULT_DOMAIN_SEPARATOR }>() .expect("finalization must not fail"); let mut digest = [0u8; 32]; hasher.squeeze(&mut digest).expect("squeezing must not fail"); println!("File hash: {:02x?}", digest); } ``` -------------------------------- ### TurboShakeError Handling Source: https://context7.com/itzmeanjan/turboshake/llms.txt Explains the error types returned when the sponge is in an invalid state. ```APIDOC ## TYPE TurboShakeError ### Description Error type returned by TurboSHAKE operations when the sponge is in an invalid state for the requested operation. ### Error Variants - **StillInDataAbsorptionPhase** - Returned when attempting to squeeze before finalization. - **DataAbsorptionPhaseAlreadyFinalized** - Returned when attempting to absorb or finalize after the state is already finalized. ``` -------------------------------- ### Squeeze Output from TurboSHAKE256 Source: https://context7.com/itzmeanjan/turboshake/llms.txt Shows the usage of TurboSHAKE256 to generate output with 256-bit security. The process requires absorption followed by finalization before the squeeze operation can be performed. ```rust use turboshake::TurboShake256; fn main() { let mut hasher = TurboShake256::default(); hasher.absorb(b"high security input").expect("absorption must not fail"); hasher.finalize::<{ TurboShake256::DEFAULT_DOMAIN_SEPARATOR }>() .expect("finalization must not fail"); let mut digest = [0u8; 64]; hasher.squeeze(&mut digest).expect("squeezing must not fail"); println!("64-byte digest: {:02x?}", digest); } ``` -------------------------------- ### TurboShake256::default Source: https://context7.com/itzmeanjan/turboshake/llms.txt Creates a new TurboSHAKE256 instance with a 256-bit security level. This variant offers stronger security at the cost of a lower throughput rate. ```APIDOC ## TurboShake256::default ### Description Creates a new TurboSHAKE256 extendable output function instance with 256-bit security level. Provides stronger security guarantees at the cost of a smaller rate (lower throughput). ### Method Associated function (constructor) ### Endpoint N/A (Rust function) ### Parameters None ### Request Example ```rust use turboshake::TurboShake256; fn main() { // Create a new TurboSHAKE256 instance let mut hasher = TurboShake256::default(); // The hasher is now ready to absorb data // Security level: 256 bits // Rate: 136 bytes (1088 bits) } ``` ### Response #### Success Response (200) N/A (Rust object creation) #### Response Example N/A ``` -------------------------------- ### Absorb Data into Sponge State Source: https://context7.com/itzmeanjan/turboshake/llms.txt Shows how to absorb arbitrary-length byte slices into the sponge state. This method supports incremental processing of large data streams or binary chunks. ```rust use turboshake::TurboShake128; fn main() { let mut hasher = TurboShake128::default(); hasher.absorb(b"Hello, ").expect("absorption failed"); hasher.absorb(b"World!").expect("absorption failed"); } ``` -------------------------------- ### Finalize Sponge State with Domain Separator Source: https://context7.com/itzmeanjan/turboshake/llms.txt Finalizes the sponge state to transition from absorption to squeezing. It accepts a domain separator byte (0x01-0x7f) to ensure cryptographic domain separation. ```rust use turboshake::TurboShake128; fn main() { let mut hasher = TurboShake128::default(); hasher.absorb(b"data").unwrap(); hasher.finalize::<{ TurboShake128::DEFAULT_DOMAIN_SEPARATOR }>().expect("finalization failed"); } ``` -------------------------------- ### TurboShake128::squeeze Source: https://context7.com/itzmeanjan/turboshake/llms.txt Squeezes arbitrary-length output bytes from the finalized TurboSHAKE128 sponge state. ```APIDOC ## METHOD squeeze ### Description Squeezes arbitrary-length output bytes from the finalized TurboSHAKE128 sponge state. Can be called multiple times to extract additional output. ### Method Rust Method ### Endpoint TurboShake128::squeeze(&mut self, output: &mut [u8]) -> Result<(), TurboShakeError> ### Parameters #### Request Body - **output** (mutable slice) - Required - The buffer to fill with squeezed bytes. ### Response #### Success Response (Ok) - **Result** (()) - Returns Ok if successful. #### Error Response (Err) - **TurboShakeError** - Returns error if called before finalization. ``` -------------------------------- ### TurboShake256::squeeze Source: https://context7.com/itzmeanjan/turboshake/llms.txt Squeezes arbitrary-length output bytes from the finalized TurboSHAKE256 sponge state with 256-bit security. ```APIDOC ## METHOD squeeze ### Description Squeezes arbitrary-length output bytes from the finalized TurboSHAKE256 sponge state. Provides 256-bit security for the output. ### Method Rust Method ### Endpoint TurboShake256::squeeze(&mut self, output: &mut [u8]) -> Result<(), TurboShakeError> ### Parameters #### Request Body - **output** (mutable slice) - Required - The buffer to fill with squeezed bytes. ### Response #### Success Response (Ok) - **Result** (()) - Returns Ok if successful. ``` -------------------------------- ### TurboShake128::finalize Source: https://context7.com/itzmeanjan/turboshake/llms.txt Finalizes the TurboSHAKE128 sponge state by absorbing a domain separator byte. This transitions the state from absorption to squeezing. A domain separator (0x01-0x7f) is used for distinguishing different hashing contexts. `DEFAULT_DOMAIN_SEPARATOR` (0x1f) can be used when domain separation is not required. ```APIDOC ## TurboShake128::finalize ### Description Finalizes the TurboSHAKE128 sponge state with a domain separator byte, transitioning from absorption to squeezing phase. The domain separator (0x01-0x7f) enables domain separation for multiple TurboSHAKE instances. Use `DEFAULT_DOMAIN_SEPARATOR` (0x1f) if not using multiple instances. ### Method `finalize` ### Endpoint N/A (Rust method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use turboshake::TurboShake128; fn main() { let mut hasher = TurboShake128::default(); hasher.absorb(b"message to hash").expect("absorption must not fail"); // Finalize with default domain separator (0x1f) hasher.finalize::<{ TurboShake128::DEFAULT_DOMAIN_SEPARATOR }>() .expect("finalization must not fail"); // Alternative: Use custom domain separator for domain separation // Valid range: 0x01 to 0x7f let mut hasher2 = TurboShake128::default(); hasher2.absorb(b"message to hash").expect("absorption must not fail"); hasher2.finalize::<0x02>().expect("custom domain separator"); // After finalization, only squeezing is allowed // Calling absorb() or finalize() again will return an error } ``` ### Response #### Success Response (200) N/A (Modifies hasher state) #### Response Example N/A ``` -------------------------------- ### TurboShake256::absorb Source: https://context7.com/itzmeanjan/turboshake/llms.txt Absorbs arbitrary-length input bytes into the TurboSHAKE256 sponge state. This method has an identical API to `TurboShake128::absorb` but operates with the 256-bit security parameters. ```APIDOC ## TurboShake256::absorb ### Description Absorbs arbitrary-length input bytes into the TurboSHAKE256 sponge state. Identical API to TurboShake128::absorb but operates with 256-bit security parameters. ### Method `absorb` ### Endpoint N/A (Rust method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **data** (bytes) - Required - The input data to absorb. ### Request Example ```rust use turboshake::TurboShake256; fn main() { let mut hasher = TurboShake256::default(); // Process a large message in chunks let message = vec![0xabu8; 8192]; // 8KB message for chunk in message.chunks(1024) { hasher.absorb(chunk).expect("absorption must not fail"); } // Continue with finalization... } ``` ### Response #### Success Response (200) N/A (Modifies hasher state) #### Response Example N/A ``` -------------------------------- ### TurboShake256::finalize Source: https://context7.com/itzmeanjan/turboshake/llms.txt Finalizes the TurboSHAKE256 sponge state by absorbing a domain separator byte. This method has identical behavior to `TurboShake128::finalize` but operates on the 256-bit security variant. ```APIDOC ## TurboShake256::finalize ### Description Finalizes the TurboSHAKE256 sponge state with a domain separator byte. Identical behavior to TurboShake128::finalize but operates on the 256-bit security variant. ### Method `finalize` ### Endpoint N/A (Rust method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use turboshake::TurboShake256; fn main() { let mut hasher = TurboShake256::default(); hasher.absorb(b"secure message").expect("absorption must not fail"); // Finalize with default domain separator hasher.finalize::<{ TurboShake256::DEFAULT_DOMAIN_SEPARATOR }>() .expect("finalization must not fail"); } ``` ### Response #### Success Response (200) N/A (Modifies hasher state) #### Response Example N/A ``` -------------------------------- ### TurboShake128::absorb Source: https://context7.com/itzmeanjan/turboshake/llms.txt Absorbs arbitrary-length input bytes into the TurboSHAKE128 sponge state. This method can be called multiple times for incremental data processing. It returns an error if called after the state has been finalized. ```APIDOC ## TurboShake128::absorb ### Description Absorbs arbitrary-length input bytes into the TurboSHAKE128 sponge state. Can be called multiple times to incrementally process data. Returns an error if called after finalization. ### Method `absorb` ### Endpoint N/A (Rust method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **data** (bytes) - Required - The input data to absorb. ### Request Example ```rust use turboshake::TurboShake128; fn main() { let mut hasher = TurboShake128::default(); // Absorb data in chunks - useful for streaming large files let data_part1 = b"Hello, "; let data_part2 = b"World!"; hasher.absorb(data_part1).expect("absorption must not fail"); hasher.absorb(data_part2).expect("absorption must not fail"); // Can also absorb empty data hasher.absorb(&[]).expect("empty absorption is valid"); // Absorb binary data let binary_data: [u8; 16] = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]; hasher.absorb(&binary_data).expect("binary absorption must not fail"); } ``` ### Response #### Success Response (200) N/A (Modifies hasher state) #### Response Example N/A ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.