1
0

accounts.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. This file contains functions & definitions
  3. assiocated to players and players only.
  4. This includes hashing, player data, loading,
  5. saving etc.
  6. */
  7. /*
  8. ** Hashing
  9. ** by Octet
  10. */
  11. #define MAX_PASSWORD_LENGTH 64
  12. #define MAX_SALT_LENGTH 32
  13. #define MAX_ENCRYPTED_PASS_LENGTH 129
  14. enum e_PassData {
  15. passEncrypt[MAX_ENCRYPTED_PASS_LENGTH],
  16. passSalt[MAX_SALT_LENGTH]
  17. }
  18. //temp vars for storing password
  19. new tempPassword[MAX_PLAYERS][65];
  20. new tempHash[MAX_PLAYERS][65];
  21. new tempSalt[MAX_PLAYERS][33];
  22. /*
  23. Whirlpool: http://forum.sa-mp.com/showthread.php?t=570945
  24. */
  25. native WP_Hash(buffer[], len, const str[]);
  26. // Variables
  27. //Variables used when fetching the values from the table
  28. new NewPassword[MAX_PLAYERS][MAX_ENCRYPTED_PASS_LENGTH];
  29. new NewSalt[MAX_PLAYERS][MAX_SALT_LENGTH];
  30. // Prototypes
  31. forward HashPassword(password[], playerid);
  32. forward HashPasswordEx(password[], encrypted[], salt[]);
  33. forward CheckPassword(password[], hash[], salt[], playerid);
  34. // Functions
  35. //Updates the password in the DB, as well as hashing
  36. public HashPassword(password[], playerid) {
  37. printf("Hashing password for player %s (%d).", PlayerName(playerid), playerid);
  38. new string[MAX_ENCRYPTED_PASS_LENGTH];
  39. new salt[MAX_SALT_LENGTH];
  40. HashPasswordEx(password, string, salt);
  41. //save inside field NewPassword
  42. new query[300];
  43. mysql_format(sqlGameConnection, query, sizeof query, "UPDATE `players` SET `NewPassword` = '%e', `NewSalt` = '%e' WHERE `Name` = '%s'", string, salt, PlayerName(playerid));
  44. mysql_pquery(sqlGameConnection, query);
  45. return 1;
  46. }
  47. //Hashes the password referencing encrypted & salt & remember arrays are pointers so reference not required
  48. public HashPasswordEx(password[], encrypted[], salt[]) {
  49. new part1[33];
  50. new part2[33];
  51. strmid(part1, password, 0, floatround(strlen(password)/2, floatround_floor));
  52. strmid(part2, password, floatround(strlen(password)/2, floatround_floor), strlen(password));
  53. format(salt, MAX_SALT_LENGTH, "%s", generateRandomSalt());
  54. format(encrypted, MAX_ENCRYPTED_PASS_LENGTH, "%s%s%s", part1, salt, part2);
  55. WP_Hash(encrypted, MAX_ENCRYPTED_PASS_LENGTH, encrypted);
  56. return;
  57. }
  58. public CheckPassword(password[], hash[], salt[], playerid) {
  59. new string[MAX_ENCRYPTED_PASS_LENGTH];
  60. new part1[33];
  61. new part2[33];
  62. strmid(part1, password, 0, floatround(strlen(password)/2, floatround_floor));
  63. strmid(part2, password, floatround(strlen(password)/2, floatround_floor), strlen(password));
  64. format(string, sizeof(string), "%s%s%s", part1, salt, part2);
  65. WP_Hash(string, sizeof string, string);
  66. if(strcmp(hash, string, true) == 0) {
  67. printf("The account, %s (%d) has been authorized using password.", PlayerName(playerid), playerid);
  68. return true;
  69. }
  70. return false;
  71. }
  72. /*
  73. Salt generation by Smo (0ms)
  74. */
  75. stock generateRandomSalt() {
  76. new salt[32 + 1];
  77. for(new i; i < 32; i++) {
  78. salt[i] = random(79) + 47;
  79. if(salt[i] == 33 || salt[i] == 34 || salt[i] == 37 || salt[i] == 39 || salt[i] == 92) { //invalid SQL chars
  80. salt[i] = 33; //replace with !
  81. }
  82. }
  83. salt[32] = 0;
  84. return salt;
  85. }