Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9231667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:07:43+00:00 2026-06-18T06:07:43+00:00

I need a valid way to get both a random and unique string, with

  • 0

I need a valid way to get both a random and unique string, with either a zero (or negligible) chance of a duplicate.

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

This is what I have so far:

substr(sha1(mt_rand().uniqid()),0,22);  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:07:44+00:00Added an answer on June 18, 2026 at 6:07 am

    Recent changes to PHP

    Since I know this is actually talking about bcrypt and password salting now I can really just point people reading this to functions they should be using instead of manually rolling their own salt system.

    Use password_hash($input, PASSWORD_DEFAULT); to generate a hash suitable to insert into a database. This will fetch the salt for you.

    Insertion:

    $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
    

    In versions before PHP 5.5, use https://github.com/ircmaxell/password_compat as a drop-in1


    When randomly generating a salt, the odds of a collision are

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

    Which for a 22-character string are impossibly low (well, not impossibly, but negligibly)

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

    See? tiny.


    Mathematical Fallacy

    If you need a truly random string (note: these strings are just a line of numbers mapped to letters), then you’re a little out of luck.
    What you’re looking for is a CSPRNG (Cryptographically Secure Pseudo-Random Number Generator). No need for uniqueness.

    As @Guarav pointed out in his answer, you can use a timestamp as your seed and then hash it. This is called a UUID (Unique Universal Identifier, if it’s a 128bit timestamp) is predictable, and can be bad for a number of reasons:

    1. The accuracy to which you take this timestamp becomes the deciding factor of how predictable this salt is.
    2. If you take the time in seconds as an integer and hash it, then you end up with very defined and easily guessed salts

    Nevertheless, with enough accuracy, you can still use a timestamp as a unique salt. Not random (unless you use it as a random seed and base convert it to base10, which is still a bad idea). Consider this if you can count time in something under nanoseconds and fancy using it as a unique ID. PHP cannot feasibly process fast enough to give two colliding sub-nanosecond IDs1 (but that doesn’t mean you shouldn’t verify!)


    1: It works with composer!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: I need a good way to get data from a thread to
All I need is to check, using python, if a string is a valid
For my program I need to generate valid infix expressions with customizable complexity. The
I need to transform a valid XML document to the OFX v1.0.2 format .
I need to select an entity that is valid at the midth of the
I need to verify if the args are valid file names, is this the
I need to compare the password entered by the user if it is valid
I'm writing an XSLT template that need to output a valid xml file for
I'm trying to test an application and I need to make an valid IP
Facebook's documentation states To read an Album you need... Any valid access_token if it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.