I found I needed support for a random hash (SHA1 or MD5, I preferred SHA1) to use in filefield foldernames, I had to add this support in token instead. The patch is trivial and is ready to use, feel free to include it.

Konstantinos

Comments

greggles’s picture

coltrane’s picture

Subscribing

dave reid’s picture

Status: Reviewed & tested by the community » Needs review

Setting appropriate status.

grah’s picture

subscribing

coltrane’s picture

I can see several use cases that would need different random tokens. What all should token provide?

In issue #247204: (pseudo) random number token there is discussion about various tokens that could be strung together to define a certain length.

A recent case I needed was a token for use in pathauto to make a random URL.

Some example tokens:

Random numbers, one, three, or five digits (could go on).
rand-num-1
rand-num-3
rand-num-5

Random alphabetical letters, one, three, or five characters (could go on).
rand-alpha-1
rand-alpha-3
rand-alpha-5

Using the above tokens could lead to more collisions I think and shouldn't be counted on for security.

What about the case where a 32 character hash from a sha1 token is too long? Should token provide truncated hashes, like [sha1-16] for a substr(sha1(mt_rand()), 0, 15)?

Edit: catching my off-by-one bug

greggles’s picture

Maybe 1, 3, 10 would be good?

I don't understand the use-cases for the SHA1 so I don't know whether it should be 16 or 32 or...something else.

I think we shouldn't worry too much and should just start providing them.

BTW - eaton and I discussed this a bit and agree that a "tokenSTARTER.module" is the best way to provide this. Then we need some docs to describe to people how to take the tokenSTARTER.module and turn it into their own fun thing (like Zen does). These random number/string tokens are a perfect point to start that module. Of course that's a lot more work - I'd be happy to build it/document it myself and then just add these in as a patch to that module...

coltrane’s picture

Title: Add support for a random SHA1 number » Tokens for random numbers and letters and a SHA1
Version: 5.x-1.9 » 6.x-1.x-dev
Status: Needs review » Needs work

1, 3, and 10 sound fine. I see use for a SHA1 token, but maybe a shortened one off a hash is too specific, or could be handled fine without collisions by stringing together rand-alpha tokens. I plan to roll a patch against the 6.x dev branch.

B747’s picture

Hi,

Is this in the dev version yet please?

greggles’s picture

I've committed a 32 character sha1 in #437842: create a token starterkit module and documentation so people know how to use it.

@coltrane - if you can add your random tokens in there it would be great. Thanks!

coltrane’s picture

StatusFileSize
new1.47 KB
coltrane’s picture

Status: Needs work » Needs review

"Hey there status select! How are you? Sorry I forgot you :("

coltrane’s picture

StatusFileSize
new2.07 KB

Here's #10 with 1, 3, and 10 letters.

greggles’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

batigolix’s picture

To use the random number token in modules like pathauto you need to define the random token in a custom module. E.g. as follows:

/**
 * Implementation of hook_token_list().
 */
function custommodule_token_list($type = 'all') {
  $tokens = array();
  if ($type == 'global' || $type == 'all') {
    $tokens['global']['random-alpha-13'] = t('Randomly generated ten-digit letters.');
  }
  return $tokens;
}

/**
 * Implementation of hook_token_values().
 */
function custommodule_token_values($type, $object = NULL, $options = array()) {
  $values = array();
  switch ($type) {
    case 'global':
      // Create random letters.
      $letters = range('a', 'z');
      shuffle($letters);
      $values['random-alpha-13'] = implode('', array_slice($letters, 0, 13));
      break;
  }
  return $values;
}

check tokenSTARTER.module in the token module folder for more examples of random numbers