Create a random and unique string

I need the right way to get both random and unique string with zero (or negligible) duplication probability.

I need characters in a range [0-9A-z].

This is what I still have:

substr(sha1(mt_rand().uniqid()),0,22);  
+5
source share
5 answers

Recent Changes in PHP

Since I know that it is actually a bcrypt and a password, now I can just tell the people reading this the functions that they should use, instead of manually starting their own salt system.

Use password_hash($input, PASSWORD_DEFAULT);to create a hash suitable for insertion into the database. It will bring you salt.

Insert:

$hash = password_hash($_POST["password"], PASSWORD_DEFAULT, ["cost" => 16]);
DB::table("users")->insert(["username" => $user, "password" => $hash]);
// or whatever database method you use to insert data

Verification:

$hash = DB::table("users")->fetchByName($username)->select("password");
$input = $_POST["password"];

$verified = password_verify($input, $hash); // true if the password matches

PHP 5.5 https://github.com/ircmaxell/password_compat drop-in 1


1 / [number of possible letters/numbers] ** [length]

22 (, , )

1 / (22 ** 60) = 1 / (3.51043 x 10**80)

? .


( : - , ), . , , - CSPRNG ( ). .

@Guarav , , . UUID ( , 128- ) :

  • , , , .
  • , .

, , . ( , base10, - ). , - . PHP , 1 ( , !)


1: !

+8

- ...

// chars
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-+';

// convert to array
$arr = str_split($chars, 1);

// shuffle the array
shuffle($arr);

// array to chars with 22 chars
echo substr(implode('', $arr), 0, 22);

xd*thKM$B#13^)9!QkD@gU

ixXYL0GEHRf+SNn#gcJIq-

$0LruRlgpjv1XS8xZq)hwY

$G-MKXf@rI3hFwT4l9)j0u

, , . .

+4

. ( ) ?

TimeStamp.

, , .

+2

, , . .

, , .

:

  • sha1. , . .
  • sha1 (, 36 0-9 AZ, ). 36, , 256 +?
  • 22 substr, . , , 22 , .
  • , substr, raw sha1, 20 .
  • 22 ( - ).
+1

time().

- , , PHP time(), $reference_number = 'BFF-' . time();, BFF , -, , , , , , .

,

-1

All Articles