PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/class/Security.class.php

https://bitbucket.org/raphael_arthur/server_inovatech
PHP | 173 lines | 77 code | 18 blank | 78 comment | 14 complexity | 05dfcf6e2a6be643ecf5c584b5578e10 MD5 | raw file
  1. <?php
  2. /*
  3. * This STATIC class, implements encryption and decryption of data.
  4. * Also create a password cypher string
  5. */
  6. class Security{
  7. public function decrypt($data, $key){
  8. $data = base64_decode($data);
  9. $salt = substr($data, 0, 16);
  10. $ct = substr($data, 16);
  11. $rounds = 3; //Because the server maybe slow...
  12. $data00 = $key . $salt;
  13. $hash = array();
  14. $hash[0] = hash('sha256', $data00, true);
  15. $result = $hash[0];
  16. for($i = 1; $i < $rounds; $i++){
  17. $hash[$i] = hash('sha256', $hash[$i - 1] . $data00, true);
  18. $result .= $hash[$i];
  19. }
  20. $dKey = substr($result, 0, 32);
  21. $iv = substr($result, 32, 16);
  22. return openssl_decrypt($ct, 'AES-256-CBC', $dKey, true, $iv);
  23. }
  24. public function encrypt($data){
  25. $salt = openssl_random_pseudo_bytes(16);
  26. $saltedData = '';
  27. $dx = '';
  28. // Salt the key(32) and iv(16) = 48
  29. while (strlen($saltedData) < 48) {
  30. $dx = hash('sha256', true);
  31. $saltedData .= $dx;
  32. }
  33. $eKey = substr($saltedData, 0, 32);
  34. $iv = substr($saltedData, 32,16);
  35. $encrypted_data = openssl_encrypt($data, 'AES-256-CBC', $eKey, true, $iv);
  36. return base64_encode($salt . $encrypted_data);
  37. }
  38. /*
  39. * generate a randon 32 bits data and encrypt it
  40. * @param void
  41. * @return (String) random encrypted string
  42. */
  43. public function generateKey(){
  44. //$key = openssl_random_pseudo_bytes(32);
  45. //Can't use this in PHP versions below 5.4
  46. //$key = password_hash($key, PASSWORD_DEFAULT);
  47. return substr(str_shuffle(".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012346789@?#"), 0, 64);
  48. }
  49. /*
  50. * Validades the user token, this function will be called upon each client request
  51. * @param (String) $userToken
  52. * @return (String) a "true" or "false" string that the user was validadeted
  53. * if the session was not validadeted destroy the session and deny the connection_aborted
  54. *
  55. */
  56. public function authSession($userToken, $isSigned, $HTTP, $REMOTE){
  57. session_start();
  58. //session_regenerate_id(true);
  59. //print $userToken . '<br />';
  60. //print $_SESSION['userToken'];
  61. //print $_SESSION['isSigned'] . '<br />';
  62. //print $_SESSION['HTTP_USER_AGENT'] . '<br />';
  63. //print $_SESSION['REMOTE_ADDR'] . '<br />';
  64. //print_r($_SESSION);
  65. if(isset($isSigned) and isset($userToken) and isset($HTTP) and isset($REMOTE)){
  66. //print 'var setadas';
  67. //verify the identity of the user
  68. if($isSigned == 'true'){
  69. //print 'here';
  70. return true;
  71. }
  72. } else {
  73. //print 'deu treta';
  74. session_destroy();
  75. unset($_SESSION);
  76. return false;
  77. }
  78. session_write_close();
  79. }
  80. /*
  81. * generate a randon 32 bits data and encrypt it
  82. * @param void
  83. * @return (Array) [requestStatus:boolean, newToken:string]
  84. */
  85. function validadeRequest($userToken, $isSigned, $HTTP, $REMOTE){
  86. session_start();
  87. if($this->authSession($userToken, $isSigned, $HTTP, $REMOTE)){
  88. //print 'entrei if';
  89. $_SESSION["userToken"] = $this->generateKey();
  90. return array('requestStatus' => true, 'userToken' => $_SESSION["userToken"]);
  91. } else {
  92. //print 'nao entrei if';
  93. return array('requestStatus' => false, 'userToken' => "");
  94. }
  95. }
  96. /*
  97. * Get the user agent,
  98. * Obs.: I can often change this later, that's why the method call aproach
  99. * @param (void)
  100. * @return (String) userAgent
  101. *
  102. */
  103. public function getUserAgent(){
  104. return $_SERVER['HTTP_USER_AGENT'];
  105. }
  106. /*
  107. * Get the user IP,
  108. * Obs.: I can often change this later, that's why the method call aproach
  109. * @param (void)
  110. * @return (String) userAgent
  111. *
  112. */
  113. public function getUserIP(){
  114. return $_SERVER['REMOTE_ADDR'];
  115. }
  116. /*------------------------------------- PHP 5.3 VERSION ONLY ----------------------------
  117. -------------------------------------- DO NOT USE THIS IN PRODUCTION -------------
  118. --------------------------------------------------------------------------------- */
  119. /*
  120. * Verify two hashed strings
  121. * Obs.: I can often change this later, that's why the method call aproach
  122. * @param (void)
  123. * @return (String) userAgent
  124. *
  125. */
  126. public function passwordVerify($dbPassword, $passwordInput){
  127. if (CRYPT_SHA512 != 1) {
  128. print "Fatal Error! Server Does not Support SHA-512 encyption";
  129. return false;
  130. } else {
  131. if (crypt($passwordInput, $dbPassword) == $dbPassword){
  132. return true;
  133. } else {
  134. return false;
  135. }
  136. }
  137. }
  138. /*
  139. * Encrypt a string using SHA-512
  140. * Obs.: I can often change this later, that's why the method call aproach
  141. * @param (string) string
  142. * @return (String) hashed_password
  143. *
  144. */
  145. // public function encOneWayString($string){
  146. // $salt = substr(str_shuffle("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012346789"), 0, 8);
  147. // if (CRYPT_SHA512 != 1) {
  148. // print "Fatal Error! Server Does not Support SHA-512 encyption";
  149. // return false;
  150. // } else {
  151. // return crypt($string, '$6$'.$salt);
  152. // }
  153. // }
  154. }
  155. ?>