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 6608533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:37:12+00:00 2026-05-25T19:37:12+00:00

I have some code I am using function genCode ($entropy=1) { $truCde = ;

  • 0

I have some code I am using

function genCode ($entropy=1) {
    $truCde = "";
    $indx = 0;
    $leng = 30*$entropy;
    while ($indx < $leng) {
        $code = "";
        $length = 100*$entropy;
        $index = 0;
        while ($index < $length) {
            $code .= rand();
            $index++;
        }
        $index = 0;
        while ($index < $length) {
            $code = sha1($code);
            $index++;
        }
        $truCde .= $code;
        $indx++;
    }
    $finalCode = sha1(rand()) . hash("sha256",$truCde . md5($entropy*rand()));
    $finalCode .= sha1(md5(strlen($finalCode)*$entropy));
    return hash (
        "sha256",
        sha1($finalCode) . sha1(md5($finalCode)) . sha1(sha1($finalCode))
    );
}

to generate a random code for e-mail verification. Is there code that takes less time to generate random codes. It takes about 1-2 seconds to run this code, but I am looking to shave .7 seconds off this because the rest of the script will take longer.

  • 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-05-25T19:37:12+00:00Added an answer on May 25, 2026 at 7:37 pm

    That’s massive overkill. Calling rand() repeatedly isn’t going to make the code “more random”, nor will using random combinations of SHA and MD5 hashes. None of that complexity improves the verification codes.

    An improvement that would make a difference would be to use mt_rand() in preference to rand(). The Mersenne Twister pseudo RNG is much stronger than most default rand() implementations. The PHP documentation hints that rand() may max out at 215 meaning you can only generate 32,768 unique verification codes.

    Other than that, a single hash call will do.

    sha1(mt_rand())
    

    (You don’t even really need to call a hash function as the unpredictability of your codes will come from the random number generator, not the hash function. But hash functions have the nice side effect of creating long hex strings which “look” better.)

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

Sidebar

Related Questions

I'm using Grails 1.3.7. I have some code that uses the built-in base64Encode function
Hi We have some php code using the file_get_contents() function, and I understand this
I have some code in .Net to draw some text content using GDI+. I
Suppose I have some code: let listB = [ 1; 2; 3 ] Using
I have a task and I want to generate some code using the CodeDom.
I have some existing code that retrieves data from a database using ADO.NET that
Does anyone have some sample code showing how to POST to a URL using
We have some really old code that calls WebServices using behaviours (webservice.htc), and we
I have to work on some code that's using generic lists to store a
I have written some code using jQuery to use Ajax to get data from

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.