Here is an API key gen script for a cryptocurrency trading platform I am building.
First it checks to see if a key exists in the db for the user ID. If it does exist, it displays the key. If it doesn't, it creates one.
Next it checks to make sure the key is unique, by polling the db for identical keys. If one is found to be identical, the page is refreshed via meta refresh
. If no key collision is found, it inserts the key in the database and refreshes the page via meta refresh
. When the page is reloaded, there is now a key in the database for the user ID, and the key is displayed.
Any tips or feedback is greatly appreciated. I am a fairly inexperienced programmer and this is my first serious project.
require_once("models/config.php");
$id = $loggedInUser->user_id;
$api_select = mysql_query("SELECT * FROM userCake_Users WHERE `User_Id`='$id'");
while($row = mysql_fetch_assoc($api_select)) {
if($row["api_key"] !== null) {
echo '<h3>Your Api Key is:</h3><br/>';
echo $row["api_key"];
}else{
$user = $row["Username"];
$pass = $row["Password"];
$length = 128;
$time = date("F j, Y, g:i a");
$salt1 = $time . hash('sha512', (sha1 .$time));
$salt2 = substr(md5(uniqid(rand(), true)), 0, 25);
$salt3 = substr(md5(uniqid(rand(), true)), 0, 25);
$salt4 = hash('sha256', (md5 .$time));
$user_hash = hash('sha512', ($salt2 . $user . $salt1));
$pass_hash = hash('sha512', ($salt1 . $pass . $salt2));
$keyhash_a = hash('sha512', ($user_hash . $salt3));
$keyhash_b = hash('sha512', ($pass_hash . $salt4));
$hash_a = str_split($keyhash_a);
$hash_b = str_split($keyhash_b);
foreach($hash_a as $key => $value) {
$hashed_a[] = $salt2 . hash('sha512', ($salt3 . $value)) . $salt1 . hash('sha256', ($salt4 . $key));
}
foreach($hash_a as $key => $value) {
$hashed_b[] = $salt2 . hash('sha512', ($salt3 . $value)) . $salt1 . hash('sha256', ($salt4 . $key));
}
$hash_merge = array_merge($hashed_b, $hashed_a);
$from_merge = implode($hash_merge);
$exploded_2 = str_split($from_merge);
$key_hash_last = implode($exploded_2);
$key0 = str_shuffle($key_hash_last);
$key1 = str_split($key0);
$key2 = array_unique($key1);
$key3 = implode($key2);
$key4 = str_shuffle($key3);
$key5 = str_shuffle($key4);
$api_key0 = str_shuffle($key3.$key4.$key5.$key2);
$api_key_prepped = mysql_real_escape_string($api_key0);
$api_check_no_collision = mysql_query("SELECT * FROM userCake_Users WHERE `api_key` = '$api_key_prepped'"); //check to see if an identical key exists
if(mysql_num_rows($api_check_no_collision) > 0) {
echo '<meta http-equiv="refresh" content="0; URL=index.php?page=api">';//an identical key exists in the database, refresh the page and generate a new key.
}else{
$api_insert = mysql_query("UPDATE `testing`.`userCake_Users` SET `api_key` = '$api_key_prepped' WHERE `userCake_Users`.`User_ID` ='$id';"); //the key is unique, submit it to the database.
echo '<meta http-equiv="refresh" content="0; URL=index.php?page=api">';
}
}
}
mysql_query
is deprecated and you should use newer implementations suggested here us2.php.net/mysql_query, unless you are using a much older PHP version. Second, is $id 'clean' enough to prevent SQL injection attacks in "...WHERE User_Id = '$id'"? Third, why will you want to force a page-refresh if there is a key collision, instead of just looping within the logic? In the extremely unlikely scenario of 10 key collisions, are you expecting the users to face 10 page refreshes as well? \$\endgroup\$$id
. yes, it comes fromuserCake1.4.2
standard function library. basically the system is built on top of userCake, with modified password hashing and extra functions. by default, all of userCake functions are run through stringent sanitization. Third, as i stated, i have < 6 months experience in php/mysql. i haven't figured out a way to loop back through yet without a page refresh. can you provide me an example would i just return or something, i'm not sure exactly how to accomplish it in php. \$\endgroup\$