### Hash passwords with custom options Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.html Examples of hashing passwords using specific rounds, digest algorithms, or output formats. ```elixir iex> hash = Pbkdf2.hash_pwd_salt("password", rounds: 100_000) ...> Pbkdf2.verify_pass("password", hash) true iex> hash = Pbkdf2.hash_pwd_salt("password", digest: :sha256) ...> Pbkdf2.verify_pass("password", hash) true iex> hash = Pbkdf2.hash_pwd_salt("password", digest: :sha256, format: :django) ...> Pbkdf2.verify_pass("password", hash) true ``` -------------------------------- ### Configure Pbkdf2 rounds for testing Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.html Set the computational cost for password hashing in test environments. Do not use this low value in production. ```elixir # Note: Do not use this value in production config :pbkdf2_elixir, rounds: 1 ``` -------------------------------- ### Pbkdf2.Stats.report/1 Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.Stats.html Hashes a password with Pbkdf2 and prints a performance report to help tune hashing parameters. ```APIDOC ## Pbkdf2.Stats.report(opts \\ []) ### Description Hashes a password and salt using Pbkdf2 and prints out statistics to assist in choosing configuration values. ### Parameters - **opts** (Keyword list) - Optional - Configuration options: - **:password** (String) - The password to hash (default: "password") - **:salt** (String) - The salt to use (default: "somesaltSOMESALT") - **:rounds** (Integer) - Number of hashing rounds - **:output_fmt** (Atom) - Output format - **:digest** (Atom) - Digest algorithm - **:length** (Integer) - Derived key length ``` -------------------------------- ### no_user_verify(opts \\ []) Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.html Runs the password hash function but always returns false, intended to mitigate timing attacks when verifying usernames. ```APIDOC ## no_user_verify(opts \\ []) ### Description Runs the password hash function, but always returns false. This function is intended to make it more difficult for any potential attacker to find valid usernames by using timing attacks. ### Parameters - **opts** (Keyword List) - Optional - Configuration options consistent with hash_pwd_salt/2. ``` -------------------------------- ### Hash and verify passwords Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.html Basic usage for hashing a password with a random salt and verifying it against the stored hash. ```elixir iex> hash = Pbkdf2.hash_pwd_salt("password") ...> Pbkdf2.verify_pass("password", hash) true iex> hash = Pbkdf2.hash_pwd_salt("password") ...> Pbkdf2.verify_pass("incorrect", hash) false ``` -------------------------------- ### Add Pbkdf2 dependency to mix.exs Source: https://pbkdf2-elixir.hexdocs.pm/readme.html Include the library in the dependencies list of your Elixir project. ```elixir def deps do [ {:pbkdf2_elixir, "~> 2.0"} ] end ``` -------------------------------- ### hash_pwd_salt(password, opts \\ []) Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.html Hashes a password with a randomly generated salt. This function accepts various options to configure the hashing process, such as the number of rounds and the digest algorithm. ```APIDOC ## hash_pwd_salt(password, opts \\ []) ### Description Hashes a password with a randomly generated salt. This function is the primary method for creating a secure hash for storage. ### Parameters - **password** (String) - Required - The plaintext password to hash. - **opts** (Keyword List) - Optional - Configuration options including :rounds, :salt_len, :format, and :digest. ``` -------------------------------- ### Pbkdf2.Base.verify_pass/6 Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.Base.html Verifies a password by comparing it with a stored Pbkdf2 hash. ```APIDOC ## verify_pass(password, hash, salt, digest, rounds, output_fmt) ### Description Verify a password by comparing it with the stored Pbkdf2 hash. ### Parameters - **password** (binary()) - Required - The password to verify. - **hash** (binary()) - Required - The stored hash. - **salt** (binary()) - Required - The salt used for the original hash. - **digest** (atom()) - Required - The digest algorithm used. - **rounds** (binary()) - Required - The number of iterations used. - **output_fmt** (atom()) - Required - The output format used. ``` -------------------------------- ### Pbkdf2.Base.gen_salt/1 Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.Base.html Generates a random salt for password hashing. Supports custom salt length and output formats. ```APIDOC ## gen_salt(opts \\ []) ### Description Generates a random salt. ### Parameters - **opts** (keyword() | integer()) - Optional - Configuration options including :salt_len (default 16) and :format (:modular, :django, or :hex). ### Example ```elixir Pbkdf2.Base.gen_salt(salt_len: 32) ``` ``` -------------------------------- ### Configure rounds for testing Source: https://pbkdf2-elixir.hexdocs.pm/readme.html Reduce the number of hashing rounds in the test environment to improve test suite performance. ```elixir config :pbkdf2_elixir, :rounds, 1 ``` -------------------------------- ### Pbkdf2.Base64.encode(data) Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.Base64.html Encodes a string using the adapted Pbkdf2 base64 alphabet. ```APIDOC ## Pbkdf2.Base64.encode(data) ### Description Encodes data using the adapted Pbkdf2 alphabet. ### Parameters - **data** (String) - Required - The string to encode. ### Example ```elixir iex> Pbkdf2.Base64.encode("spamandeggs") "c3BhbWFuZGVnZ3M" ``` ``` -------------------------------- ### Pbkdf2.Base.hash_password/3 Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.Base.html Hashes a password using the Pbkdf2 algorithm with configurable rounds, format, digest, and length. ```APIDOC ## hash_password(password, salt, opts \\ []) ### Description Hash a password using Pbkdf2. ### Parameters - **password** (binary()) - Required - The password to hash. - **salt** (binary()) - Required - The salt to use. - **opts** (keyword()) - Optional - Configuration options: :rounds (default 160,000), :format (:modular, :django, :hex), :digest (default :sha512), and :length. ``` -------------------------------- ### Encode data using Pbkdf2.Base64 Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.Base64.html Encodes a string using the adapted Pbkdf2 base64 alphabet. ```elixir iex> Pbkdf2.Base64.encode("spamandeggs") "c3BhbWFuZGVnZ3M" ``` -------------------------------- ### verify_pass(password, stored_hash) Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.html Verifies a password by hashing the provided input and comparing it against a previously stored hash. ```APIDOC ## verify_pass(password, stored_hash) ### Description Verifies a password by hashing the password and comparing the hashed value with a stored hash. ### Parameters - **password** (String) - Required - The plaintext password to verify. - **stored_hash** (String) - Required - The previously hashed password string to compare against. ``` -------------------------------- ### Decode data using Pbkdf2.Base64 Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.Base64.html Decodes a string using the adapted Pbkdf2 base64 alphabet. ```elixir iex> Pbkdf2.Base64.decode("c3BhbWFuZGVnZ3M") "spamandeggs" ``` -------------------------------- ### Pbkdf2.Base64.decode(data) Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.Base64.html Decodes a string using the adapted Pbkdf2 base64 alphabet. ```APIDOC ## Pbkdf2.Base64.decode(data) ### Description Decodes data using the adapted Pbkdf2 alphabet. ### Parameters - **data** (String) - Required - The base64 encoded string to decode. ### Example ```elixir iex> Pbkdf2.Base64.decode("c3BhbWFuZGVnZ3M") "spamandeggs" ``` ``` -------------------------------- ### Generate a random salt Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.Base.html Generates a random salt using default or custom length and format options. ```elixir Pbkdf2.Base.gen_salt() ``` ```elixir Pbkdf2.Base.gen_salt(salt_len: 32) ``` ```elixir Pbkdf2.Base.gen_salt(format: :django) ``` -------------------------------- ### Define hash_password specification Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.Base.html Type specification for the hash_password function. ```elixir @spec hash_password(binary(), binary(), keyword()) :: binary() ``` -------------------------------- ### Define verify_pass specification Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.Base.html Type specification for the verify_pass function. ```elixir @spec verify_pass(binary(), binary(), binary(), atom(), binary(), atom()) :: boolean() ``` -------------------------------- ### Define gen_salt specification Source: https://pbkdf2-elixir.hexdocs.pm/Pbkdf2.Base.html Type specification for the gen_salt function. ```elixir @spec gen_salt(keyword() | integer()) :: binary() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.