Skip to content

Implement Alphanumeric::gen_string #1128

@vks

Description

@vks

Background

Some people wonder how to use Alphanumeric to generate a string (see #935). While we give an example in the documentation, it might be more obvious to provide a method for this, especially because the efficient implementation requires unsafe code.

Applications are the generation of unique IDs and nonces for APIs such as OAuth.

Feature request

Provide a method of Alphanumeric for generating strings.

Open questions:

  • Should we provide gen_string supporting only CryptoRngs and gen_string_insecure supporting any RNG?
  • Should we
    1. always generate a new string (fn gen_string(&self, &mut Rng, usize) -> String),
    2. modify an existing slice (fn gen_string(&self, &mut Rng, &mut str)) or
    3. push to an existing string (fn gen_string(&self, &mut Rng, &mut String, usize))?

i. is probably most efficient, because we can call String::from_utf8_unchecked, but it cannot reuse an existing buffer.
ii. is more flexible in that regard, but it requires an initialized buffer.
iii. is the most flexible, but offers little over converting to char and collecting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions