PageRenderTime 58ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/_inc/class.password.php

https://github.com/lslucas/105fm
PHP | 93 lines | 27 code | 28 blank | 38 comment | 5 complexity | d77ad7ce1b50bf4507f3fd131a84c57d MD5 | raw file
  1. <?php
  2. class Password {
  3. /*
  4. Copyright 2011 Lucas Serafim
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. /*
  16. * @author: Lucas Serafim, lslucas@gmail.com
  17. * @site: www.lucasserafim.com.br
  18. * This class verify php version and suport for mcrypt
  19. * if not suport mcrypt use md5, if php version is lower 5.0.0 not use binary format
  20. *
  21. ####usage
  22. *$pass = new Password;
  23. *var_dump($pass->hash('mypassword'));
  24. *ou var_dump($pass->hash('mypassword', 'md5', 'my salt'));
  25. */
  26. private $_input, $_key, $_type, $encrypted_data;
  27. public $used;
  28. //clean vars
  29. public function __destruct() {
  30. unset($encrypted_data, $_input, $_type, $_key);
  31. }
  32. /*
  33. *generate the password with parameters
  34. @params: password, function to use: mcrypt (default) or md5, key or your salt (I use server_name with default)
  35. @return string
  36. */
  37. public function hash($_input, $_type='mcrypt', $_key='your salt') {
  38. /*
  39. *if exists mcrypt and $_type is mcrypt
  40. */
  41. if( function_exists('mcrypt') && $_type=='mcrypt' ) {
  42. $td = mcrypt_module_open(MCRYPT_TWOFISH256, '', 'ofb', '');
  43. $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_BLOWFISH);
  44. mcrypt_generic_init($td, $_key, $iv);
  45. $encrypted_data = mcrypt_generic($td, $_input);
  46. mcrypt_generic_deinit($td);
  47. mcrypt_module_close($td);
  48. $this->used = 'mcrypt';
  49. //else use md5
  50. } else {
  51. if(version_compare(PHP_VERSION, '5.0.0', '>='))
  52. $bool = true;
  53. else $bool = false;
  54. $this->used = $bool ? 'md5 with PHP5+' : 'md5 with PHP5-';
  55. $encrypted_key = md5($_key, $bool).md5($_input, $bool);
  56. $encrypted_data = md5($encrypted_key, $bool);
  57. }
  58. // return generated password
  59. // enjoy
  60. return md5(utf8_encode($encrypted_data));
  61. }
  62. }