ExkPasswd.Batch (ExkPasswd v0.2.0)

View Source

Optimized batch password generation with buffered random bytes.

When generating many passwords at once, this module reduces cryptographic overhead by pre-generating a large buffer of random bytes and consuming them as needed, rather than making individual :crypto.strong_rand_bytes/1 calls for each random number.

Performance

For generating many passwords, batch generation reduces syscall overhead by using a stateful buffered random generator rather than individual :crypto.strong_rand_bytes/1 calls.

Examples

iex> passwords = ExkPasswd.Batch.generate_batch(10)
...> length(passwords)
10
iex> Enum.all?(passwords, &is_binary/1)
true

iex> config = ExkPasswd.Config.new!(num_words: 4)
...> passwords = ExkPasswd.Batch.generate_batch(5, config)
...> length(passwords)
5

Summary

Functions

Generate multiple passwords in batch with optimized random byte buffering.

Generate passwords in parallel using multiple processes.

Generate multiple unique passwords in batch.

Functions

generate_batch(count, config \\ Config.new!(), opts \\ [])

@spec generate_batch(pos_integer(), ExkPasswd.Config.t(), keyword()) :: [String.t()]

Generate multiple passwords in batch with optimized random byte buffering.

This function pre-allocates a buffer of random bytes and uses it to generate multiple passwords, reducing the number of expensive :crypto syscalls.

Parameters

  • count - Number of passwords to generate
  • config - Config struct to use (default: default preset)
  • opts - Optional keyword list:
    • :buffer_size - Size of random byte buffer (default: 10,000 bytes)

Returns

List of generated passwords

Examples

iex> passwords = ExkPasswd.Batch.generate_batch(10)
...> length(passwords)
10

iex> config = ExkPasswd.Config.new!(num_words: 4)
...> passwords = ExkPasswd.Batch.generate_batch(5, config)
...> length(Enum.uniq(passwords))
5

generate_parallel(count, config \\ Config.new!(), opts \\ [])

@spec generate_parallel(pos_integer(), ExkPasswd.Config.t(), keyword()) :: [
  String.t()
]

Generate passwords in parallel using multiple processes.

For very large batches (1000+), parallel generation can provide additional speedup on multi-core systems.

Parameters

  • count - Number of passwords to generate
  • config - Config struct to use
  • opts - Optional keyword list:
    • :workers - Number of parallel workers (default: System.schedulers_online())

Returns

List of generated passwords

Examples

iex> passwords = ExkPasswd.Batch.generate_parallel(100)
...> length(passwords)
100

generate_unique_batch(count, config \\ Config.new!(), opts \\ [])

@spec generate_unique_batch(pos_integer(), ExkPasswd.Config.t(), keyword()) :: [
  String.t()
]

Generate multiple unique passwords in batch.

Ensures all generated passwords are unique by regenerating duplicates. Useful when uniqueness is required (e.g., bulk user creation).

Parameters

  • count - Number of unique passwords to generate
  • config - Config struct to use
  • opts - Optional keyword list:
    • :max_attempts - Maximum total generation attempts (default: count * 100)

Returns

List of unique generated passwords, or raises if max_attempts exceeded

Examples

iex> alias ExkPasswd.Config
...> config = Config.new!(num_words: 4)
...> passwords = ExkPasswd.Batch.generate_unique_batch(10, config)
...> length(passwords)
10
iex> length(Enum.uniq(passwords))
10