├── .github └── FUNDING.yml ├── README.md └── api_key_generator.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: kevinkabatra # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP-API-Key-Generator 2 | Example for an API Key generator written in PHP. The key that is generated will be 32 non-cryptographic random characters long, and can contain 0-9, a-z (lowercase), A-Z (uppercase). Adding the option for the characters to repeat, creates over 450 quadrillion combinations. 3 | 4 | To keep the code short, I generate a random number using rand(48, 122). This number will then be filtered for the ranges of 58 to 64, and 91 to 96. If the random number is present in the previous ranges, the number must be discarded and then recreated. This is done until a number is generated outside of the previous ranges, and this in turn must be completed 32 times. This is done so that the random number can convert into ASCII code (i.e. 0 ; = 0, whitespace added the prevent conversion) to generate the characters mentioned above. 5 | 6 | Another option would be to create a random number using rand(0, 61). Then using a switch statement append a string together based upon the result. This method results in code roughly 133 lines in length (excluding comments, but allowing whitespace), while the previous method is 27 lines in length (again excluding comments, but allowing whitespace). 7 | 8 | Running example at: http://kevinkabatra.ignorelist.com/examples/api%20key%20generator/example_api_key_generator.php 9 | -------------------------------------------------------------------------------- /api_key_generator.php: -------------------------------------------------------------------------------- 1 | 5 | * All rights reserved. 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * The code follows the Follow Field Naming Conventions from the 30 | * AOSP (Android Open Source Project) Code Style Guidelines for Contributors : 31 | * Non-public, non-static field names start with m. 32 | * Static field names start with s. 33 | * Other fields start with a lower case letter. 34 | * Public static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES 35 | * Hyperlink: (too long for one line) 36 | * http://source.android.com/source/code-style 37 | * .html#follow-field-naming-conventions 38 | */ 39 | 40 | //declare and set variables 41 | $output = null; 42 | 43 | /** 44 | * Generates a random integer between 48 and 122. 45 | *
46 | * @return int Non-cryptographically generated random number. 47 | */ 48 | function findRandom() { 49 | $mRandom = rand(48, 122); 50 | return $mRandom; 51 | } 52 | 53 | /** 54 | * Checks if $random equals ranges 48:57, 56:90, or 97:122. 55 | *
56 | * This function is being used to filter $random so that when used in: 57 | * '' . $random . ';' it will generate the ASCII characters for ranges 58 | * 0:8, a-z (lowercase), or A-Z (uppercase). 59 | *
60 | * @param int $mRandom Non-cryptographically generated random number. 61 | * @return int 0 if not within range, else $random is returned. 62 | */ 63 | function isRandomInRange($mRandom) { 64 | if(($mRandom >=58 && $mRandom <= 64) || 65 | (($mRandom >=91 && $mRandom <= 96))) { 66 | return 0; 67 | } else { 68 | return $mRandom; 69 | } 70 | } 71 | 72 | for($loop = 0; $loop <= 31; $loop++) { 73 | for($isRandomInRange = 0; $isRandomInRange === 0;){ 74 | $isRandomInRange = isRandomInRange(findRandom()); 75 | } 76 | $output .= html_entity_decode('' . $isRandomInRange . ';'); 77 | } 78 | 79 | echo $output; 80 | --------------------------------------------------------------------------------