-3

I would like to render such a key or similar, but I don't want the "-" sign to appear at the beginning. My current code means that this character can also be generated at the beginning, and I would like it never to be generated at the beginning and at the end, but only in the middle after the X character from the end and the beginning, where X is the number of characters from the side

6LdBaA6aBxOAsj0AoTJ8rC-NbzpzAPgVaAz1bnAE

My Code

// Generowanie Unique Key
if(!function_exists('genetateUniqueKey')){
    function genetateUniqueKey($length = 40){
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321-'.time();
        $charactersLength = strlen($characters);
        $randomString = '';
        for($i = $length; $i > 0; $i--){
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
}

Thank you in advance for your help :)

Kondzio
  • 37
  • 6
  • 1
    Generate *x* characters where `$characters` excludes "-", then generate some more where `$characters` includes "-", then again *x* where it excludes "-"…!? – deceze Jun 09 '21 at 12:23
  • 1
    Why just not remove '-' altogether? – Nigel Ren Jun 09 '21 at 12:28
  • @deceze But how should I do it so that it works well and takes into account the length of the entire generated key and is "scalable" in such a chunk that when I give, for example, the length of 20, the length will be reduced by half, after which this character can be? – Kondzio Jun 09 '21 at 12:29
  • 1
    @Kondzio: How does the length of the string affect it? For any string of length X... Generate **1** character where "-" isn't an option; Generate **X - 2** characters where "-" is an option; Generate **1** character where "-" isn't an option; Concatenate the three results. Have you tried? What didn't work? – David Jun 09 '21 at 12:30
  • @NigelRen Because I want to generate a similar key to what reCaptha has, which is fed after "api.js?render=KEY" – Kondzio Jun 09 '21 at 12:32
  • @David I don't really understand what you mean, could you show it on the code? Because combining the code will not do anything because how is it supposed to know what part will be, what length will be as soon as the general is given? – Kondzio Jun 09 '21 at 12:36
  • As a simple start: write that `for` loop three times; the first and last time you use `rand(0, $charactersLength - 2)`, the middle time you use `rand(0, $charactersLength - 1)`. Then you just need to adjust the `$i = $length` condition a bit to make sure your resulting string has the desired length. – deceze Jun 09 '21 at 12:38
  • @Kondzio: You'll need to clarify what you're asking first. What is *"where X is the number of characters from the side"*? What specific logic are you trying to implement? Where specifically in your string do you want it to be possible that there might be a "-"? Or where do you want there to always be a "-"? It's not really clear what your exact goal is. – David Jun 09 '21 at 12:40
  • For generating random keys, only a secure random generator should be used, such as `random_bytes` or `random_int`. Many other solutions for random value generation, including those involving `time()`, `rand()`, and `mt_rand()`, are much more predictable and are unsuitable if the random string will serve as a password or another secret value. See: https://stackoverflow.com/questions/4570980/generating-a-random-code-in-php/64472183#64472183 . – Peter O. Jun 09 '21 at 12:40
  • @PeterO. I only know I need to generate a public key such as is generated for reCaptha by google because I am writing my own WAF which will be the most extensive WAF that exists and for that I need to put the public key after the js file name. Initially it will only be for decoration but then I plan to make this public key the key of the client application that uses WAF – Kondzio Jun 09 '21 at 12:50
  • 1
    Not to be discouraging, but, that's an ambition plan, if you're starting with this question… Either way, the `-` probably *means something* to reCaptcha. If it doesn't mean anything to you, why include it with these very specific rules…? – deceze Jun 09 '21 at 12:52
  • @deceze It doesn't really work what you said because it has a length of 41 and not the default https://hastebin.com/ivarigemek.php – Kondzio Jun 09 '21 at 12:58
  • Well, you haven't adjusted your length *appropriately*. Also… you want the "-" *exactly* in the middle? Then simply generate your 40 character string without "-", and replace the center character with a "-", done. – deceze Jun 09 '21 at 13:09
  • @deceze Because I did not know how to do it and I can not do as you say, because when the length will be, for example, 10, it will not be equal to the middle. I did as you wrote and I didn't know what you mean with this adjustment. Maybe send the corrected code for the best because we can't really understand each other xD – Kondzio Jun 09 '21 at 13:20

1 Answers1

-1

I am making some changes to your code

The string will be trimmed from both sides according to the value privided in $length

EDIT: Sorry, by mistake I have given wrong code. Try this one:

// Generowanie Unique Key
if (!function_exists('genetateUniqueKey')) {
    function genetateUniqueKey($length = 40)
    {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321';
        $randomString = str_shuffle($characters) . "-" . str_shuffle($characters);
        return substr($randomString, 55, $length);
    }
} 
  • 2
    Shuffling a string and repeatedly picking random characters are very different algorithms with very different properties in terms of entropy. – deceze Jun 09 '21 at 13:42
  • This code doesn't work because it displays over 100 characters, so you probably haven't checked if it shows the correct length – Kondzio Jun 09 '21 at 14:11
  • Sorry, by mistake I have given wrong code. I have made some changes. Please try that – Swapnil Prakash Jun 11 '21 at 04:11