3

in my table, i insert new records of user(new users). I have a int set as field primary key. What i want is to make this id secure. Because this id public to everyone. Any user or people can see this id.

If id i generate is like this:

0000000001
...
0000000023
...
0000000157
...

it is easy for another person to guess it because ids are in a order which is incremented by 1. Instead, i want to store a "public" id in a separate field which is a aplhanumeric representation of this auto_increment primary id.

So in database, table will be like this:

id------------------public_id
---------------------------------
0000000001 -------- W3UB3VNAU3222
0000000002 -------- 7BNXYO28CN2KK

I thought about using hash. But hash is a fixed length of 40 or above. But i want the public id to be fixed 10 characters in length. Should be unique and if possible, I prefer generating it from id

plz tell how to create this ? any builtin functions that helps me to do this?


I read about hash() function with crc32:http://www.php.net/manual/en/function.hash.php#104987 It will give 8 char length hash. Will it conflict in future ?

Test:

echo hash('crc32', '0000000001'); // gives 6c13f76e
Vpp Man
  • 2,384
  • 8
  • 43
  • 74

5 Answers5

0

The following code example generates an 10 characters long random string that contains 1 digit:

<?php
  function random_string( )
  {
    $character_set_array = array( );
    $character_set_array[ ] = array( 'count' => 10, 'characters' => 'abcdefghijklmnopqrstuvwxyz' );
    $character_set_array[ ] = array( 'count' => 1, 'characters' => '0123456789' ); //you can change count
    $temp_array = array( );
    foreach ( $character_set_array as $character_set )
    {
      for ( $i = 0; $i < $character_set[ 'count' ]; $i++ )
      {
        $temp_array[ ] = $character_set[ 'characters' ][ rand( 0, strlen( $character_set[ 'characters' ] ) - 1 ) ];
      }
    }
    shuffle( $temp_array );
    return implode( '', $temp_array );
  }
?>
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • is it ok to use this: `echo hash('crc32', '0000000001'); // gives 6c13f76e` ? – Vpp Man Aug 03 '12 at 03:39
  • but hash can be decrypt easily there are even plenty of website who can decrypt hash like [http://www.hash-cracker.com/](http://www.hash-cracker.com/) and [http://www.md5decrypter.co.uk/](http://www.md5decrypter.co.uk/) – NullPoiиteя Aug 03 '12 at 03:50
0

try this

MySql auto-incrementing Alpha-numeric primary key?

CREATE TABLE myItems (
    id INT NOT NULL AUTO_INCREMENT,
    prefix CHAR(30) NOT NULL,
    PRIMARY KEY (id, prefix),

Or

composite (alphanumeric) primary key and auto increment

public function random_id_gen($length)
    {
        //the characters you want in your id
        $characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
        $max = strlen($characters) - 1;
        $string = '';

        for ($i = 0; $i < $length; $i++) {
            $string .= $characters[mt_rand(0, $max)];
        }

        return $string;
    }
Community
  • 1
  • 1
Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
0

You can try to shuffle characters and XOR from original id.

0

Generate a hash and take the first 10 letters.

Check if such an ID is already in your database. If it is not you can take it as your ID, otherwise generate and new hash and try again.

If you want to represent your true ID you could chose a seperator letter and map your ID to letters.

Example:

ID:          ...0000021
PUBLIC_ID :  ...Hd3dXBA

X is the separator to separate your mapped ID from the randomly generated part.

2 is mapped to B

1 is mapped to A

user1567896
  • 2,398
  • 2
  • 26
  • 43
0

There is no built-in functions that would do that for you, so I'm afraid you'll have to build one yourself. If you're not restricted by the 10 character requirement, have a look at UUID. This post: PHP function to generate v4 UUID will explain how to create a PHP function that will generate a UUID in PHP.

Otherwise try a function along those lines, although you will have to check if the id is unique every time you generate it.

function generateID()
{
    $capital_letters = range("A", "Z");
    $lowcase_letters = range("a", "z");
    $numbers         = range(0, 9);

    $all = array_merge($capital_letters, $lowcase_letters, $numbers);
    $count = count($all);    
    $id    = "";

    for($i = 0; $i < 10; $i++)
    {
        $key = rand(0, $count);
        $id .= $all[$key];
    }

    if(!uniqueId($id))
    {
        return generateID();
    }
    return $id;
}
Community
  • 1
  • 1
Kasia Gogolek
  • 3,374
  • 4
  • 33
  • 50
  • is it ok to use this: `echo hash('crc32', '0000000001'); // gives 6c13f76e` ? – Vpp Man Aug 03 '12 at 03:39
  • Hash is just encrypting the id, and in theory can be decrypted. If your priority is for the id to be random and unique, hash is the wrong tool In my opinion. – Kasia Gogolek Aug 03 '12 at 07:05