The documentation says "The base64 encoded result will have a length of up to 4 * $byte_count."

This is wrong. I think it is always exactly ceil($byte_count * 4 / 3).

Note that drupal_base64_encode() removes the padding equal signs.

Issue fork drupal-2745013

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tetranz created an issue. See original summary.

tetranz’s picture

Status: Active » Needs review
FileSize
568 bytes
tetranz’s picture

Status: Needs review » Active
FileSize
568 bytes

Redoing this since it's so old and the line numbers in the patch are way off.

tetranz’s picture

Status: Active » Needs review
tetranz’s picture

joshf’s picture

Assigned: tetranz » Unassigned
Status: Needs review » Reviewed & tested by the community

Confirmed; this can be easily checked:

for ($i = 0; $i < 100000; $i++) {
  $length = strlen(drupal_random_key($i));
  $expected_length = ceil($i * 4 / 3);
  if ($length != $expected_length) {
    die("Length mismatch calling drupal_random_key($i)"); // This is never executed.
  }
}
stefan.r’s picture

Version: 7.x-dev » 8.4.x-dev
Status: Reviewed & tested by the community » Active

Does this go for Crypt::randomBytesBase64 in Drupal 8.x as well? I think they use random_bytes(), random_compat, or the same hash-based generator.

tetranz’s picture

Version: 8.4.x-dev » 7.x-dev
Status: Active » Postponed
Related issues: +#2866495: Crypt::randomBytesBase64 documentation for length of returned string is wrong

You're quite right stefan.r, it does apply to Crypt::randomBytesBase64. I've created a related issue for that.

https://www.drupal.org/node/2866495

Setting this one back to 7.x.dev and postponed until the D8 one is committed.

poker10’s picture

The parent issue was commited (#3118581: The documentation for Crypt::randomBytesBase64() is wrong about the output length). From what I see, this documentation fix will needs to be tweaked a little bit to match commited D8 version.

apaderno made their first commit to this issue’s fork.

apaderno’s picture

Title: drupal_random_key() documentation for length of returned string is wrong. » drupal_random_key() documentation for length of returned string is wrong

apaderno’s picture

Status: Needs work » Needs review