### Configure Argonautica Crate in Cargo.toml Source: https://context7.com/bcmyers/argonautica/llms.txt Shows different ways to add the Argonautica crate to your Rust project's Cargo.toml, including basic installation and enabling optional features like SIMD acceleration and serde support. ```toml [dependencies] # Basic installation (cross-platform compatible) argonautica = "0.2" # With SIMD acceleration (recommended for local builds) argonautica = { version = "0.2", features = ["simd"] } # With serde serialization support argonautica = { version = "0.2", features = ["serde"] } # With both features argonautica = { version = "0.2", features = ["serde", "simd"] } ``` -------------------------------- ### Configure Hasher and Verifier Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Demonstrates configuring the Argon2 version, hashing a password, and verifying it using the Hasher and Verifier classes. ```python hasher.version = Version._0x13 # Default is Version._0x13 # 👆 Argon2 has two versions: 0x10 and 0x13. The latest version is 0x13 (as of 5/18). # Unless you have a very specific reason not to, you should use the latest # version (0x13), which is also the default hash = hasher.hash( password='P@ssw0rd', salt='somesalt', # You can set your own salt, or use the default: RandomSalt(32) ) assert(hash == '$argon2id$v=19$m=4096,t=192,p=2$c29tZXNhbHQ$8nD3gRm+NeOcIiIrlnzDAdnK4iD+K0mVqFXowGs13M4') verifier = Verifier(secret_key=None) verifier.additional_data = None # As with Hasher, you can configure a Verifier's additional data verifier.backend = Backend.C # As with Hasher, you can configure a Verifier's backend verifier.threads = 2 # As with Hasher, you can configure a Verifier's threads is_valid = verifier.verify( hash=hash, password='P@ssw0rd' ) assert(is_valid) ``` -------------------------------- ### Add Argonautica to Cargo.toml (Local Build with Serde) Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-rs/README.md Include this in your Cargo.toml when building for your own machine, with 'serde' and 'simd' features. ```toml argonautica = { version = "0.2", features = ["serde", "simd"] } ``` -------------------------------- ### Python: Basic Hashing with Secret Key Source: https://context7.com/bcmyers/argonautica/llms.txt Demonstrates basic password hashing using the Hasher class with a secret key. A random salt is used by default. ```python from argonautica import Hasher # Basic hashing with secret key hasher = Hasher(secret_key='somesecret') hash = hasher.hash(password='P@ssw0rd') print(hash) # Output: $argon2id$v=19$m=4096,t=192,p=2$$ ``` -------------------------------- ### Use Argon2 Convenience Class Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Instantiate the Argon2 class to handle both hashing and verification in a single object. ```python from argonautica import Argon2 argon2 = Argon2(secret_key='somesecret') hash = argon2.hash(password='P@ssw0rd') print(hash) is_valid = argon2.verify(hash=hash, password='P@ssw0rd') assert(is_valid) ``` -------------------------------- ### Python: Hashing with Custom Salt Source: https://context7.com/bcmyers/argonautica/llms.txt Shows how to hash a password with a specific salt for deterministic results. This is useful for testing or specific use cases. ```python from argonautica import Hasher # Hashing with custom salt (for deterministic results) hasher = Hasher(secret_key='somesecret') hash = hasher.hash(password='P@ssw0rd', salt='somesalt') print(hash) # Output: $argon2id$v=19$m=4096,t=192,p=2$c29tZXNhbHQ$8nD3gRm+NeOcIiIrlnzDAdnK4iD+K0mVqFXowGs13M4 ``` -------------------------------- ### Configuration Options Source: https://context7.com/bcmyers/argonautica/llms.txt The Hasher can be tuned using various configuration methods to adjust security and performance parameters. ```APIDOC ## Configuration ### Methods - **configure_backend(backend: Backend)**: Sets the underlying implementation backend. - **configure_hash_len(len: usize)**: Sets the output hash length in bytes. - **configure_iterations(iters: u32)**: Sets the time cost (CPU iterations). - **configure_memory_size(mem: u32)**: Sets the memory cost in kibibytes. - **configure_lanes(lanes: u32)**: Sets the parallelism factor. - **configure_threads(threads: u32)**: Sets the number of threads. - **configure_variant(variant: Variant)**: Sets the Argon2 variant (Argon2d, Argon2i, Argon2id). - **configure_version(version: Version)**: Sets the Argon2 version. - **configure_password_clearing(bool)**: Enables/disables zeroing the password after hashing. - **configure_secret_key_clearing(bool)**: Enables/disables zeroing the secret key after hashing. - **opt_out_of_secret_key(bool)**: Toggles requirement for a secret key. ``` -------------------------------- ### Configure Hasher and Generate Hash Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-rs/README.md Configures the Hasher with various security parameters and performs a password hash operation. ```rust .configure_password_clearing(false) // Default is `false` .configure_secret_key_clearing(false) // Default is `false` .configure_threads(2) // Default is number of logical cores on your machine .configure_variant(Variant::Argon2id) // Default is `Variant::Argon2id` .configure_version(Version::_0x13) // Default is `Version::_0x13` .opt_out_of_secret_key(true); // Default is `false` let hash = hasher .with_password("P@ssw0rd") .with_salt("somesalt") .hash() .unwrap(); assert_eq!( &hash, "$argon2id$v=19$m=4096,t=192,p=2$c29tZXNhbHQ$sw41ZsxebJmOJ6vSHe6BGQ", ); ``` -------------------------------- ### Configure Hasher with Custom Options Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-rs/README.md Demonstrates customizing various Argon2 hashing parameters like backend, CPU pool, hash length, iterations, lanes, and memory size. Use this when default settings are not suitable for your security or performance needs. ```rust extern crate argonautica; extern crate futures_cpupool; use argonautica::Hasher; use argonautica::config::{Backend, Variant, Version}; use futures_cpupool::CpuPool; fn main() { let mut hasher = Hasher::default(); hasher .configure_backend(Backend::C) // Default is `Backend::C` // 👆 argonautica was designed to support multiple backends (meaning multiple // implementations of the underlying Argon2 algorithm). Currently only the C backend // is supported, which uses the canonical Argon2 library written in C to actually // do the work. In the future hopefully a Rust backend will also be supported, but, // for the moment, you must use `Backend::C`, which is the default. Using // `Backend::Rust` will result in an error (again, for the moment). .configure_cpu_pool(CpuPool::new(2)) // 👆 There are two non-blocking methods on `Hasher` that perform computation on // a separate thread and return a `Future` instead of a `Result` (`hash_non_blocking` // and `hash_raw_non_blocking`). These methods allow argonautica to play nicely with // futures-heavy code, but need a `CpuPool` in order to work. The blocking // methods `hash` and `hash_raw` do not use a 'CpuPool'; so if you are using only // these blocking methods you can ignore this configuration entirely. If, however, // you are using the non-blocking methods and would like to provide your own `CpuPool` // instead of using the default, which is a lazily created `CpuPool` with the number // of threads equal to the number of logical cores on your machine, you can // configure your `Hasher` with a custom `CpuPool` using this method. This // might be useful if, for example, you are writing code in an environment which // makes heavy use of futures, the code you are writing uses both a `Hasher` and // a `Verifier`, and you would like both of them to share the same underlying // `CpuPool`. .configure_hash_len(16) // Default is `32` // 👆 The hash length in bytes is configurable. The default is 32. This is probably // a good number to use. 16 is also probably fine. You probably shouldn't go below 16 .configure_iterations(192) // Default is `192` // 👆 Argon2 has a notion of "iterations" or "time cost". All else equal and generally // speaking, the greater the number of iterations, the longer it takes to perform the // hash and the more secure the resulting hash. More iterations basically means more // CPU load. This and "memory size" (see below) are the two primary parameters to // adjust in order to increase or decrease the security of your hash. The default is // 192 iterations, which was chosen because, along with the default memory size of // 4096, this leads to a hashing time of approximately 300 milliseconds on the // early-2014 Macbook Air that is the developer's machine. If you're going to use // argonautica in production, you should probably tweak this parameter (and the memory // size parameter) in order to increase the time it takes to hash to the maximum you // can reasonably allow for your use-case (e.g. to probably about 300-500 milliseconds // for the use-case of hashing user passwords for a website) .configure_lanes(2) // Default is number of logical cores on your machine // 👆 Argon2 can break up its work into one or more "lanes" during some parts of // the hashing algorithm. If you configure it with multiple lanes and you also // use multiple threads (see below) the hashing algorithm will performed its // work in parallel in some parts, potentially speeding up the time it takes to // produce a hash without diminishing the security of the result. By default, // the number of lanes is set to the number of logical cores on your machine .configure_memory_size(4096) // Default is `4096` // 👆 Argon2 has a notion of "memory size" or "memory cost" (in kibibytes). All else // equal and generally speaking, the greater the memory size, the longer it takes to // perform the hash and the more secure the resulting hash. More memory size basically ; } ``` -------------------------------- ### Configure Hasher backend Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Sets the underlying implementation backend for the Argon2 algorithm. ```python hasher.backend = Backend.C # Default is Backend.C ``` -------------------------------- ### Configure Argon2 Hashing and Verification Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Instantiate Hasher and Verifier with custom configurations for Argon2 parameters like backend, variant, and version. A secret key can be set to None if not required. ```python3 from argonautica import Hasher, Verifier from argonautica.config import Backend, Variant, Version hasher = Hasher(secret_key=None) ``` -------------------------------- ### Add Argonautica to Cargo.toml (Cross-Machine Build with Serde) Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-rs/README.md Include this in your Cargo.toml when building for a different machine, with the 'serde' feature. ```toml argonautica = { version = "0.2", features = ["serde"] } ``` -------------------------------- ### Add Argonautica to Cargo.toml (Local Build) Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-rs/README.md Include this in your Cargo.toml when building for your own machine, with optional 'simd' feature. ```toml argonautica = { version = "0.2", features = ["simd"] } ``` -------------------------------- ### Configure Custom RandomSalt Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Use a custom length for RandomSalt when initializing a Hasher. ```python from argonautica import Hasher from argonautica.data import RandomSalt hasher = Hasher( salt=RandomSalt(16), # 👆 Here we're using a RandomSalt of length of 16 bytes # instead of the default, which is a RandomSalt of length 32 bytes secret_key="somesecret" ) hash = hasher.hash(password='P@ssw0rd') print(hash) ``` -------------------------------- ### Add Argonautica to Cargo.toml (Cross-Machine Build) Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-rs/README.md Include this in your Cargo.toml when building for a different machine, without specific features. ```toml argonautica = "0.2" ``` -------------------------------- ### Python: Custom Argon2 Configuration Source: https://context7.com/bcmyers/argonautica/llms.txt Configures Argon2 hashing and verification parameters like memory, iterations, and lanes. This allows fine-tuning security and performance. ```python from argonautica import Hasher, Verifier from argonautica.config import Backend, Variant, Version # Configure hasher with custom parameters hasher = Hasher(secret_key='production_secret') hasher.backend = Backend.C # Use C backend hasher.hash_len = 32 # Output hash length in bytes hasher.iterations = 192 # Time cost (more = slower/secure) hasher.memory_size = 4096 # Memory cost in kibibytes hasher.lanes = 2 # Parallelism lanes hasher.threads = 2 # Number of threads hasher.variant = Variant.Argon2id # Algorithm variant hasher.version = Version._0x13 # Argon2 version hasher.additional_data = None # Optional additional data hash = hasher.hash(password='P@ssw0rd', salt='somesalt') assert hash == '$argon2id$v=19$m=4096,t=192,p=2$c29tZXNhbHQ$8nD3gRm+NeOcIiIrlnzDAdnK4iD+K0mVqFXowGs13M4' # Configure verifier verifier = Verifier(secret_key='production_secret') verifier.backend = Backend.C verifier.threads = 2 is_valid = verifier.verify(hash=hash, password='P@ssw0rd') assert is_valid ``` -------------------------------- ### Rust: Load Secret Key from Environment Source: https://context7.com/bcmyers/argonautica/llms.txt Loads a base64 encoded secret key from environment variables. Ensure the SECRET_KEY environment variable is set before running. ```rust extern crate argonautica; extern crate dotenv; use std::env; use argonautica::input::SecretKey; use argonautica::{Hasher, Verifier}; fn load_secret_key() -> Result, Box> { dotenv::dotenv().ok(); let base64_encoded_key = env::var("SECRET_KEY")?; Ok(SecretKey::from_base64_encoded(&base64_encoded_key)?) } fn main() -> Result<(), Box> { let secret_key = load_secret_key()?; let mut hasher = Hasher::default(); let hash = hasher .with_password("P@ssw0rd") .with_secret_key(&secret_key) .hash()?; let mut verifier = Verifier::default(); let is_valid = verifier .with_hash(&hash) .with_password("P@ssw0rd") .with_secret_key(&secret_key) .verify()?; assert!(is_valid); Ok(()) } ``` -------------------------------- ### Configure Hasher security parameters in Rust Source: https://context7.com/bcmyers/argonautica/llms.txt Customize hashing performance and security settings such as memory size, iterations, and algorithm variants. Default settings are tuned for approximately 300ms execution time. ```rust extern crate argonautica; use argonautica::Hasher; use argonautica::config::{Backend, Variant, Version}; fn main() { let mut hasher = Hasher::default(); hasher .configure_backend(Backend::C) // Use C backend (default) .configure_hash_len(32) // Hash output length in bytes .configure_iterations(192) // Time cost (CPU iterations) .configure_memory_size(4096) // Memory cost in kibibytes .configure_lanes(4) // Parallelism factor .configure_threads(4) // Number of threads .configure_variant(Variant::Argon2id) // Algorithm variant .configure_version(Version::_0x13) // Argon2 version .configure_password_clearing(true) // Zero password after hash .configure_secret_key_clearing(true) // Zero secret key after hash .opt_out_of_secret_key(false); // Require secret key let hash = hasher .with_password("P@ssw0rd") .with_secret_key("production_secret_key") .hash() .unwrap(); assert!(hash.starts_with("$argon2id$v=19$m=4096,t=192,p=4$")); } ``` -------------------------------- ### Python: Argon2 Convenience Class Source: https://context7.com/bcmyers/argonautica/llms.txt Uses the Argon2 class for a combined hashing and verification interface. Simplifies common password operations. ```python from argonautica import Argon2 argon2 = Argon2(secret_key='somesecret') # Hash a password hash = argon2.hash(password='P@ssw0rd') print(hash) # Verify the password is_valid = argon2.verify(hash=hash, password='P@ssw0rd') assert is_valid # Verify with wrong password is_invalid = argon2.verify(hash=hash, password='wrong_password') assert not is_invalid ``` -------------------------------- ### C API: argonautica_hash Source: https://context7.com/bcmyers/argonautica/llms.txt Creates password hashes with full control over all Argon2 parameters. ```APIDOC ## C: argonautica_hash ### Description Generates a password hash using the specified Argon2 parameters. ### Parameters - **encoded** (char*) - Required - Output buffer for the encoded hash. - **additional_data** (uint8_t*) - Optional - Additional data for hashing. - **password** (uint8_t*) - Required - The password to hash. - **salt** (uint8_t*) - Required - The salt to use. - **secret_key** (uint8_t*) - Required - The secret key for hashing. - **backend** (int) - Required - The backend to use (e.g., ARGONAUTICA_C). - **hash_len** (int) - Required - Length of the hash. - **iterations** (int) - Required - Number of iterations. - **lanes** (int) - Required - Number of lanes. - **memory_size** (int) - Required - Memory size in KiB. - **threads** (int) - Required - Number of threads. - **variant** (int) - Required - Argon2 variant. - **version** (int) - Required - Argon2 version. ### Response - **argonautica_error_t** (int) - Returns ARGONAUTICA_OK on success or an error code. ``` -------------------------------- ### Verify a Password Hash Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Use the Verifier class to check if a given password matches a stored Argon2 hash. Requires the same secret key used during hashing. ```python3 from argonautica import Verifier verifier = Verifier(secret_key='somesecret') is_valid = verifier.verify( hash='$argon2id$v=19$m=4096,t=192,p=2$ULwasg5z5byOAork0UEhoTBVxIvAafKuceNz9NdCVXU$YxhaPnqRDys', password='P@ssw0rd', ) assert(is_valid) ``` -------------------------------- ### C API: argonautica_verify Source: https://context7.com/bcmyers/argonautica/llms.txt Validates a password against an existing hash. ```APIDOC ## C: argonautica_verify ### Description Validates a password against a stored hash string. ### Parameters - **is_valid** (int*) - Required - Pointer to store the result (1 for valid, 0 for invalid). - **stored_hash** (const char*) - Required - The hash to verify against. - **password** (uint8_t*) - Required - The password to verify. - **secret_key** (uint8_t*) - Required - The secret key used during hashing. - **backend** (int) - Required - The backend to use. - **threads** (int) - Required - Number of threads. ### Response - **argonautica_error_t** (int) - Returns ARGONAUTICA_OK on success or an error code. ``` -------------------------------- ### Configure Hasher additional data Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Sets the additional data for the hasher, which acts as a secondary secret key. ```python hasher.additional_data = None # Default is None ``` -------------------------------- ### Configure Hasher iterations Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Sets the number of iterations, or time cost, for the hashing process. ```python hasher.iterations = 192 # Default is 192 ``` -------------------------------- ### Configure Hasher memory size Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Sets the memory cost in kibibytes for the hashing process. ```python hasher.memory_size = 4096 # Default is 4096 ``` -------------------------------- ### Verify a Password Against a Hash in Rust Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-rs/README.md Use the Verifier to check if a given password matches a stored Argon2 hash. Ensure the same secret key used for hashing is provided for verification. The verify method returns a boolean indicating success. ```rust extern crate argonautica; use argonautica::Verifier; fn main() { let mut verifier = Verifier::default(); let is_valid = verifier .with_hash("$argon2id$v=19$m=4096,t=192,p=4$o2y5PU86Vt+sr93N7YUGgC7AMpTKpTQCk4tNGUPZMY4$yzP/ukZRPIbZg6PvgnUUobUMbApfF9RH6NagL9L4Xr4") .with_password("P@ssw0rd") .with_secret_key("secret key that you should really store in a .env file instead of in code, but this is just an example") .verify() .unwrap(); assert!(is_valid); } ``` -------------------------------- ### Hash a Password with Argon2 Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Use the Hasher class to hash a password. A random salt is used by default, ensuring unique hashes for the same password. ```python3 from argonautica import Hasher hasher = Hasher(secret_key='somesecret') hash = hasher.hash(password='P@ssw0rd') print(hash) # 👆 prints a random hash as the defeault `Hasher` uses a random salt by default ``` -------------------------------- ### Decode Hash Components in Python Source: https://context7.com/bcmyers/argonautica/llms.txt Use the decode function and HashRaw class to inspect string-encoded hash parameters, salt, and raw bytes. ```python from argonautica.utils import decode, HashRaw from argonautica.config import Variant, Version hash = '$argon2id$v=19$m=4096,t=128,p=2$c29tZXNhbHQ$WwD2/wGGTuw7u4BW8sLM0Q' # Decode hash into components hash_raw = decode(hash) # Access hash parameters print(f"Iterations: {hash_raw.iterations}") # 128 print(f"Lanes: {hash_raw.lanes}") # 2 print(f"Memory size: {hash_raw.memory_size}") # 4096 print(f"Variant: {hash_raw.variant}") # Variant.Argon2id print(f"Version: {hash_raw.version}") # Version._0x13 # Access raw bytes print(f"Salt bytes: {hash_raw.raw_salt_bytes}") # b'somesalt' print(f"Hash bytes: {hash_raw.raw_hash_bytes}") # b'[\x00\xf6\xff...' # Re-encode back to string encoded = hash_raw.encode() assert hash == encoded ``` -------------------------------- ### Python: Hashing Without Secret Key Source: https://context7.com/bcmyers/argonautica/llms.txt Hashing a password without a secret key. This is not recommended for production environments due to reduced security. ```python # Hashing without secret key (not recommended) hasher = Hasher(secret_key=None) hash = hasher.hash(password='P@ssw0rd') ``` -------------------------------- ### Python: Random Salt Generation Source: https://context7.com/bcmyers/argonautica/llms.txt Generates cryptographically random salt values of configurable length for hashing. Each hash operation uses a new random salt by default. ```python from argonautica import Hasher from argonautica.data import RandomSalt # Use random salt with custom length (default is 32 bytes) hasher = Hasher( salt=RandomSalt(16), # 16-byte random salt secret_key="somesecret" ) # Each hash will use a different random salt hash1 = hasher.hash(password='P@ssw0rd') hash2 = hasher.hash(password='P@ssw0rd') print(f"Hash 1: {hash1}") print(f"Hash 2: {hash2}") # Both hashes will be different due to random salts ``` -------------------------------- ### Configure Hasher variant Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Sets the Argon2 variant to be used for hashing. ```python hasher.variant = Variant.Argon2id # Default is Variant.Argon2id ``` -------------------------------- ### Perform non-blocking hashing with Futures in Rust Source: https://context7.com/bcmyers/argonautica/llms.txt Utilize non-blocking methods to perform hashing and verification without stalling the current thread. This requires the futures crate. ```rust extern crate argonautica; extern crate futures; use argonautica::{Hasher, Verifier}; use futures::Future; fn main() { let mut hasher = Hasher::default(); let mut verifier = Verifier::default(); let future = hasher .with_password("P@ssw0rd") .with_secret_key("my_secret_key") .hash_non_blocking() .and_then(|hash| { println!("Hash: {}", &hash); verifier .with_hash(&hash) .with_password("P@ssw0rd") .with_secret_key("my_secret_key") .verify_non_blocking() }) .and_then(|is_valid| { assert!(is_valid); println!("Verification successful!"); Ok(()) }); future.wait().unwrap(); } ``` -------------------------------- ### Python: Verifying Password Source: https://context7.com/bcmyers/argonautica/llms.txt Validates a given password against a stored hash using the Verifier class. The same secret key used for hashing must be provided. ```python from argonautica import Verifier verifier = Verifier(secret_key='somesecret') is_valid = verifier.verify( hash='$argon2id$v=19$m=4096,t=192,p=2$ULwasg5z5byOAork0UEhoTBVxIvAafKuceNz9NdCVXU$YxhaPnqRDys', password='P@ssw0rd', ) if is_valid: print("Password verified successfully!") else: print("Invalid password!") ``` -------------------------------- ### Configure Hasher threads Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Sets the number of threads for parallel computation, limited by the number of lanes. ```python hasher.threads = 2 # Default is multiprocessing.cpu_count() ``` -------------------------------- ### Verify Passwords in C Source: https://context7.com/bcmyers/argonautica/llms.txt Use argonautica_verify to validate a password against a stored hash. The is_valid pointer is updated to reflect the match result. ```c #include #include #include "argonautica.h" int main() { const char* stored_hash = "$argon2id$v=19$m=4096,t=192,p=2$c29tZXNhbHQ$8nD3gRm+NeOcIiIrlnzDAdnK4iD+K0mVqFXowGs13M4"; uint8_t password[] = "P@ssw0rd"; uint8_t secret_key[] = "my_secret_key"; int is_valid = 0; argonautica_error_t err = argonautica_verify( &is_valid, // Output: 1 if valid, 0 if invalid NULL, 0, // additional_data, additional_data_len stored_hash, // encoded hash to verify against password, strlen((char*)password), secret_key, strlen((char*)secret_key), ARGONAUTICA_C, // backend 0, // password_clearing 0, // secret_key_clearing 2 // threads ); if (err == ARGONAUTICA_OK) { if (is_valid) { printf("Password is correct!\n"); } else { printf("Password is incorrect!\n"); } } else { printf("Verification error: %s\n", argonautica_error_msg(err)); } return 0; } ``` -------------------------------- ### Configure Hasher hash length Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Sets the length of the resulting hash in bytes. ```python hasher.hash_len = 32 # Default is 32 ``` -------------------------------- ### Create Password Hashes in C Source: https://context7.com/bcmyers/argonautica/llms.txt The argonautica_hash function generates password hashes with configurable Argon2 parameters. Ensure the output buffer is sized correctly using argonautica_encoded_len. ```c #include #include #include "argonautica.h" int main() { // Allocate buffer for encoded hash (use argonautica_encoded_len to calculate size) int encoded_len = argonautica_encoded_len( 32, // hash_len 192, // iterations 2, // lanes 4096, // memory_size 8, // salt_len ARGONAUTICA_ARGON2ID // variant ); char encoded[encoded_len]; // Password and secret key uint8_t password[] = "P@ssw0rd"; uint8_t secret_key[] = "my_secret_key"; uint8_t salt[] = "somesalt"; // Hash the password argonautica_error_t err = argonautica_hash( encoded, // Output buffer NULL, 0, // additional_data, additional_data_len password, strlen((char*)password), salt, 8, // Use deterministic salt secret_key, strlen((char*)secret_key), ARGONAUTICA_C, // backend 32, // hash_len 192, // iterations 2, // lanes 4096, // memory_size 0, // password_clearing 0, // secret_key_clearing 2, // threads ARGONAUTICA_ARGON2ID, // variant ARGONAUTICA_0x13 // version ); if (err == ARGONAUTICA_OK) { printf("Hash: %s\n", encoded); } else { printf("Error: %s\n", argonautica_error_msg(err)); } return 0; } ``` -------------------------------- ### Python: Decode Hash Components Source: https://context7.com/bcmyers/argonautica/llms.txt The decode function and HashRaw class allow inspection of string-encoded hash components including salt, parameters, and raw bytes. ```APIDOC ## Python: decode(hash) ### Description Decodes an Argon2 hash string into its constituent components and parameters. ### Parameters - **hash** (string) - Required - The encoded Argon2 hash string. ### Response - **HashRaw** (object) - An object containing hash parameters (iterations, lanes, memory_size, variant, version) and raw bytes (salt, hash). ``` -------------------------------- ### Hash passwords with Hasher in Rust Source: https://context7.com/bcmyers/argonautica/llms.txt Use the Hasher struct to generate password hashes. It supports optional secret keys and custom salts, though custom salts are not recommended for production. ```rust extern crate argonautica; use argonautica::Hasher; fn main() { // Create a default hasher and hash a password with a secret key let mut hasher = Hasher::default(); let hash = hasher .with_password("P@ssw0rd") .with_secret_key("my_secret_key_from_env_file") .hash() .unwrap(); println!("{}", &hash); // Output: $argon2id$v=19$m=4096,t=192,p=4$$ // Hash with custom salt (not recommended for production) let hash_deterministic = hasher .with_password("P@ssw0rd") .with_salt("somesalt") .with_secret_key("my_secret_key_from_env_file") .hash() .unwrap(); println!("{}", &hash_deterministic); } ``` -------------------------------- ### Configure Hasher lanes Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Sets the number of lanes used to parallelize parts of the hashing algorithm. ```python hasher.lanes = 2 # Default is multiprocessing.cpu_count() ``` -------------------------------- ### Handle Argonautica Errors in C Source: https://context7.com/bcmyers/argonautica/llms.txt Demonstrates how to use a switch statement with argonautica_error_t enum values to handle different error conditions. Includes a fallback to argonautica_error_msg for unhandled errors. ```c #include #include "argonautica.h" void handle_error(argonautica_error_t err) { switch (err) { case ARGONAUTICA_OK: printf("Success\n"); break; case ARGONAUTICA_ERROR_PASSWORD_TOO_SHORT: printf("Error: Password must be at least 1 byte\n"); break; case ARGONAUTICA_ERROR_SALT_TOO_SHORT: printf("Error: Salt must be at least 8 bytes\n"); break; case ARGONAUTICA_ERROR_HASH_DECODE: printf("Error: Invalid hash format\n"); break; case ARGONAUTICA_ERROR_MEMORY_ALLOCATION: printf("Error: Failed to allocate memory\n"); break; default: // Use built-in error message function printf("Error: %s\n", argonautica_error_msg(err)); } } int main() { argonautica_error_t err = ARGONAUTICA_ERROR_PASSWORD_TOO_SHORT; handle_error(err); // Output: Error: Password must be at least 1 byte return 0; } ``` -------------------------------- ### Hasher API Source: https://context7.com/bcmyers/argonautica/llms.txt The Hasher struct is the primary interface for generating password hashes using a builder pattern. ```APIDOC ## Hasher ### Description Configures and generates password hashes using the Argon2 algorithm. ### Methods - **with_password(password: &str)**: Sets the password to be hashed. - **with_secret_key(key: &str)**: Sets an optional secret key for additional security. - **with_salt(salt: &str)**: Sets a custom salt (not recommended for production). - **hash()**: Performs the blocking hash operation and returns the resulting string. - **hash_non_blocking()**: Performs the hash operation asynchronously returning a Future. ``` -------------------------------- ### Verify passwords with Verifier in Rust Source: https://context7.com/bcmyers/argonautica/llms.txt Use the Verifier struct to validate a password against a stored hash. The same secret key used during hashing must be provided. ```rust extern crate argonautica; use argonautica::Verifier; fn main() { let stored_hash = "$argon2id$v=19$m=4096,t=192,p=4$\ o2y5PU86Vt+sr93N7YUGgC7AMpTKpTQCk4tNGUPZMY4$\ yzP/ukZRPIbZg6PvgnUUobUMbApfF9RH6NagL9L4Xr4"; let mut verifier = Verifier::default(); let is_valid = verifier .with_hash(stored_hash) .with_password("P@ssw0rd") .with_secret_key("my_secret_key_from_env_file") .verify() .unwrap(); if is_valid { println!("Password is correct!"); } else { println!("Invalid password!"); } } ``` -------------------------------- ### Hash a Password with Argon2 in Rust Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-rs/README.md Use the Hasher to hash a password with a secret key. The default Hasher uses a random salt, resulting in a unique hash each time. Store secret keys securely, e.g., in a .env file. ```rust extern crate argonautica; use argonautica::Hasher; fn main() { let mut hasher = Hasher::default(); let hash = hasher .with_password("P@ssw0rd") .with_secret_key("secret key that you should really store in a .env file instead of in code, but this is just an example") .hash() .unwrap(); println!("{}", &hash); } ``` -------------------------------- ### Decode Raw Hash Components Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-py/README.md Extract parameters and raw bytes from a string-encoded Argon2 hash using the decode utility. ```python from argonautica.utils import decode, HashRaw hash = '$argon2id$v=19$m=4096,t=128,p=2$c29tZXNhbHQ$WwD2/wGGTuw7u4BW8sLM0Q' # Create a `HashRaw` using the `decode` function hash_raw = decode(hash) # Pull out the raw parameters iterations = hash_raw.iterations # 128 lanes = hash_raw.lanes # 2 memory_size = hash_raw.memory_size # 4096 variant = hash_raw.variant # Variant.Argon2id version = hash_raw.version # Version._0x13 # Pull out the raw bytes raw_hash_bytes = hash_raw.raw_hash_bytes # b'[\x00\xf6\xff\x01\x86N\xec;\xbb\x80V\xf2\xc2\xcc\xd1' raw_salt_bytes = hash_raw.raw_salt_bytes # b'somesalt' # Turn a `HashRaw` back into a string-encoded hash using the `encode` method hash2 = hash_raw.encode() assert(hash == hash2) ``` -------------------------------- ### Include Argonautica Crate in Rust Code Source: https://github.com/bcmyers/argonautica/blob/master/argonautica-rs/README.md Add this line to your Rust code, typically in lib.rs or main.rs, to use the Argonautica crate. ```rust extern crate argonautica; ``` -------------------------------- ### Verifier API Source: https://context7.com/bcmyers/argonautica/llms.txt The Verifier struct is used to validate a password against a previously generated Argonautica hash. ```APIDOC ## Verifier ### Description Validates a provided password against a stored hash string. ### Methods - **with_hash(hash: &str)**: Sets the hash string to verify against. - **with_password(password: &str)**: Sets the password to verify. - **with_secret_key(key: &str)**: Sets the secret key used during the original hashing process. - **verify()**: Performs the blocking verification and returns a boolean. - **verify_non_blocking()**: Performs the verification asynchronously returning a Future. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.