functions.php 799 B

1234567891011121314151617181920212223242526
  1. <?php
  2. @session_start();
  3. require_once(PATH_PHP.'database.php');
  4. // TODO - create php functions for the api
  5. function addUser($username,$password,$email){
  6. $salt = $mysqli->escape_string(salt());
  7. $email = $mysqli->escape_string($email);
  8. $username = $mysqli->escape_string($username);
  9. $hash = $mysqli->escape_string(saltedHash($password,$salt));
  10. return $mysqli->query("INSERT INTO `bugs`.`users` (email,name,pass,salt) VALUES '{$email}','{$username}','{$password}','{$salt}'");
  11. }
  12. function salt(){
  13. return uniqid(mt_rand(0,61), true);
  14. }
  15. function saltedHash($pass,$salt){
  16. $hash = $pass.$salt;
  17. for($i = 0;$i<50;$i++){
  18. $hash = hash('sha512',$pass.$hash.$salt);
  19. }
  20. return $hash;
  21. }
  22. function compareSaltedHash($pass,$salt,$hash){
  23. return $hash == saltedHash($pass,$salt);
  24. }
  25. ?>