EntropyString/Erlang

non-power of 2 length charset

yhafri opened this issue · 3 comments

I would like to create random string only from this 36 alphabet charset:

<<"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ">>

For non-power of 2 length charsets look at https://github.com/puid/Elixir. That library is an extension of EntropyString to any custom character sets. An optimized version for the characters you listed is actually already provided.

Using the Puid library does, of course, require the caveats of calling Elixir code from Erlang. I used mix to create a quick Elixir project that had one file, upper_alpha_num.ex:

defmodule(UpperAlphaNum, do: use(Puid, bits: 96, charset: :alphanum_upper))

I ran an Erlang shell including the BEAM files from the compilation of that simple Elixir project, as well as the BEAM files for Elixir, and got:

1> 'Elixir.UpperAlphaNum':info().
#{'struct' => 'Elixir.Puid.Info',
chars => <<"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789">>,
charset => alphanum_upper,entropy_bits => 98.23,
entropy_bits_per_char => 5.17,ere => 0.65,length => 19,
rand_bytes => fun crypto:strong_rand_bytes/1}

2> 'Elixir.UpperAlphaNum':generate().
<<"BHG00YLBEKLBAANM1AK">>

Thanks for your help but i've zero interest on Elixir and for sure i'll not ship BEAM code that way.

Are the characters really important? You could drop four of them and have a 32 length set. Using 96-bits of entropy (you didn't specify), the difference is:

36 characters:
  entropy_bits: 98.23,
  entropy_bits_per_char: 5.17,
  ere: 0.65,
  length: 19
32 characters:
  entropy_bits: 100.0,
  entropy_bits_per_char: 5.0,
  ere: 0.63,
  length: 20