Password.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Class to create passwords
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category Text
  15. * @package Text_Password
  16. * @author Martin Jansen <mj@php.net>
  17. * @author Olivier Vanhoucke <olivier@php.net>
  18. * @copyright 2004-2005 Martin Jansen, Olivier Vanhoucke
  19. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  20. * @version CVS: $Id: Password.php,v 1.18 2008/11/30 13:38:56 mj Exp $
  21. * @link http://pear.php.net/package/Text_Password
  22. */
  23. /**
  24. * Number of possible characters in the password
  25. */
  26. $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] = 0;
  27. /**
  28. * Main class for the Text_Password package
  29. *
  30. * @category Text
  31. * @package Text_Password
  32. * @author Martin Jansen <mj@php.net>
  33. * @author Olivier Vanhoucke <olivier@php.net>
  34. * @copyright 2004-2005 Martin Jansen, Olivier Vanhoucke
  35. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  36. * @version Release: @package_version@
  37. * @link http://pear.php.net/package/Text_Password
  38. */
  39. class Text_Password {
  40. /**
  41. * Create a single password.
  42. *
  43. * @access public
  44. * @param integer Length of the password.
  45. * @param string Type of password (pronounceable, unpronounceable)
  46. * @param string Character which could be use in the
  47. * unpronounceable password ex : 'ABCDEFG'
  48. * or numeric, alphabetical or alphanumeric.
  49. * @return string Returns the generated password.
  50. */
  51. function create($length = 10, $type = 'pronounceable', $chars = '')
  52. {
  53. switch ($type) {
  54. case 'unpronounceable' :
  55. return Text_Password::_createUnpronounceable($length, $chars);
  56. case 'pronounceable' :
  57. default :
  58. return Text_Password::_createPronounceable($length);
  59. }
  60. }
  61. /**
  62. * Create multiple, different passwords
  63. *
  64. * Method to create a list of different passwords which are
  65. * all different.
  66. *
  67. * @access public
  68. * @param integer Number of different password
  69. * @param integer Length of the password
  70. * @param string Type of password (pronounceable, unpronounceable)
  71. * @param string Character which could be use in the
  72. * unpronounceable password ex : 'A,B,C,D,E,F,G'
  73. * or numeric, alphabetical or alphanumeric.
  74. * @return array Array containing the passwords
  75. */
  76. function createMultiple($number, $length = 10, $type = 'pronounceable', $chars = '')
  77. {
  78. $passwords = array();
  79. while ($number > 0) {
  80. while (true) {
  81. $password = Text_Password::create($length, $type, $chars);
  82. if (!in_array($password, $passwords)) {
  83. $passwords[] = $password;
  84. break;
  85. }
  86. }
  87. $number--;
  88. }
  89. return $passwords;
  90. }
  91. /**
  92. * Create password from login
  93. *
  94. * Method to create password from login
  95. *
  96. * @access public
  97. * @param string Login
  98. * @param string Type
  99. * @param integer Key
  100. * @return string
  101. */
  102. function createFromLogin($login, $type, $key = 0)
  103. {
  104. switch ($type) {
  105. case 'reverse':
  106. return strrev($login);
  107. case 'shuffle':
  108. return Text_Password::_shuffle($login);
  109. case 'xor':
  110. return Text_Password::_xor($login, $key);
  111. case 'rot13':
  112. return str_rot13($login);
  113. case 'rotx':
  114. return Text_Password::_rotx($login, $key);
  115. case 'rotx++':
  116. return Text_Password::_rotxpp($login, $key);
  117. case 'rotx--':
  118. return Text_Password::_rotxmm($login, $key);
  119. case 'ascii_rotx':
  120. return Text_Password::_asciiRotx($login, $key);
  121. case 'ascii_rotx++':
  122. return Text_Password::_asciiRotxpp($login, $key);
  123. case 'ascii_rotx--':
  124. return Text_Password::_asciiRotxmm($login, $key);
  125. }
  126. }
  127. /**
  128. * Create multiple, different passwords from an array of login
  129. *
  130. * Method to create a list of different password from login
  131. *
  132. * @access public
  133. * @param array Login
  134. * @param string Type
  135. * @param integer Key
  136. * @return array Array containing the passwords
  137. */
  138. function createMultipleFromLogin($login, $type, $key = 0)
  139. {
  140. $passwords = array();
  141. $number = count($login);
  142. $save = $number;
  143. while ($number > 0) {
  144. while (true) {
  145. $password = Text_Password::createFromLogin($login[$save - $number], $type, $key);
  146. if (!in_array($password, $passwords)) {
  147. $passwords[] = $password;
  148. break;
  149. }
  150. }
  151. $number--;
  152. }
  153. return $passwords;
  154. }
  155. /**
  156. * Helper method to create password
  157. *
  158. * Method to create a password from a login
  159. *
  160. * @access private
  161. * @param string Login
  162. * @param integer Key
  163. * @return string
  164. */
  165. function _xor($login, $key)
  166. {
  167. $tmp = '';
  168. for ($i = 0; $i < strlen($login); $i++) {
  169. $next = ord($login{$i}) ^ $key;
  170. if ($next > 255) {
  171. $next -= 255;
  172. } elseif ($next < 0) {
  173. $next += 255;
  174. }
  175. $tmp .= chr($next);
  176. }
  177. return $tmp;
  178. }
  179. /**
  180. * Helper method to create password
  181. *
  182. * Method to create a password from a login
  183. * lowercase only
  184. *
  185. * @access private
  186. * @param string Login
  187. * @param integer Key
  188. * @return string
  189. */
  190. function _rotx($login, $key)
  191. {
  192. $tmp = '';
  193. $login = strtolower($login);
  194. for ($i = 0; $i < strlen($login); $i++) {
  195. if ((ord($login{$i}) >= 97) && (ord($login{$i}) <= 122)) { // 65, 90 for uppercase
  196. $next = ord($login{$i}) + $key;
  197. if ($next > 122) {
  198. $next -= 26;
  199. } elseif ($next < 97) {
  200. $next += 26;
  201. }
  202. $tmp .= chr($next);
  203. } else {
  204. $tmp .= $login{$i};
  205. }
  206. }
  207. return $tmp;
  208. }
  209. /**
  210. * Helper method to create password
  211. *
  212. * Method to create a password from a login
  213. * lowercase only
  214. *
  215. * @access private
  216. * @param string Login
  217. * @param integer Key
  218. * @return string
  219. */
  220. function _rotxpp($login, $key)
  221. {
  222. $tmp = '';
  223. $login = strtolower($login);
  224. for ($i = 0; $i < strlen($login); $i++, $key++) {
  225. if ((ord($login{$i}) >= 97) && (ord($login{$i}) <= 122)) { // 65, 90 for uppercase
  226. $next = ord($login{$i}) + $key;
  227. if ($next > 122) {
  228. $next -= 26;
  229. } elseif ($next < 97) {
  230. $next += 26;
  231. }
  232. $tmp .= chr($next);
  233. } else {
  234. $tmp .= $login{$i};
  235. }
  236. }
  237. return $tmp;
  238. }
  239. /**
  240. * Helper method to create password
  241. *
  242. * Method to create a password from a login
  243. * lowercase only
  244. *
  245. * @access private
  246. * @param string Login
  247. * @param integer Key
  248. * @return string
  249. */
  250. function _rotxmm($login, $key)
  251. {
  252. $tmp = '';
  253. $login = strtolower($login);
  254. for ($i = 0; $i < strlen($login); $i++, $key--) {
  255. if ((ord($login{$i}) >= 97) && (ord($login{$i}) <= 122)) { // 65, 90 for uppercase
  256. $next = ord($login{$i}) + $key;
  257. if ($next > 122) {
  258. $next -= 26;
  259. } elseif ($next < 97) {
  260. $next += 26;
  261. }
  262. $tmp .= chr($next);
  263. } else {
  264. $tmp .= $login{$i};
  265. }
  266. }
  267. return $tmp;
  268. }
  269. /**
  270. * Helper method to create password
  271. *
  272. * Method to create a password from a login
  273. *
  274. * @access private
  275. * @param string Login
  276. * @param integer Key
  277. * @return string
  278. */
  279. function _asciiRotx($login, $key)
  280. {
  281. $tmp = '';
  282. for ($i = 0; $i < strlen($login); $i++) {
  283. $next = ord($login{$i}) + $key;
  284. if ($next > 255) {
  285. $next -= 255;
  286. } elseif ($next < 0) {
  287. $next += 255;
  288. }
  289. switch ($next) { // delete white space
  290. case 0x09:
  291. case 0x20:
  292. case 0x0A:
  293. case 0x0D:
  294. $next++;
  295. }
  296. $tmp .= chr($next);
  297. }
  298. return $tmp;
  299. }
  300. /**
  301. * Helper method to create password
  302. *
  303. * Method to create a password from a login
  304. *
  305. * @access private
  306. * @param string Login
  307. * @param integer Key
  308. * @return string
  309. */
  310. function _asciiRotxpp($login, $key)
  311. {
  312. $tmp = '';
  313. for ($i = 0; $i < strlen($login); $i++, $key++) {
  314. $next = ord($login{$i}) + $key;
  315. if ($next > 255) {
  316. $next -= 255;
  317. } elseif ($next < 0) {
  318. $next += 255;
  319. }
  320. switch ($next) { // delete white space
  321. case 0x09:
  322. case 0x20:
  323. case 0x0A:
  324. case 0x0D:
  325. $next++;
  326. }
  327. $tmp .= chr($next);
  328. }
  329. return $tmp;
  330. }
  331. /**
  332. * Helper method to create password
  333. *
  334. * Method to create a password from a login
  335. *
  336. * @access private
  337. * @param string Login
  338. * @param integer Key
  339. * @return string
  340. */
  341. function _asciiRotxmm($login, $key)
  342. {
  343. $tmp = '';
  344. for ($i = 0; $i < strlen($login); $i++, $key--) {
  345. $next = ord($login{$i}) + $key;
  346. if ($next > 255) {
  347. $next -= 255;
  348. } elseif ($next < 0) {
  349. $next += 255;
  350. }
  351. switch ($next) { // delete white space
  352. case 0x09:
  353. case 0x20:
  354. case 0x0A:
  355. case 0x0D:
  356. $next++;
  357. }
  358. $tmp .= chr($next);
  359. }
  360. return $tmp;
  361. }
  362. /**
  363. * Helper method to create password
  364. *
  365. * Method to create a password from a login
  366. *
  367. * @access private
  368. * @param string Login
  369. * @return string
  370. */
  371. function _shuffle($login)
  372. {
  373. $tmp = array();
  374. for ($i = 0; $i < strlen($login); $i++) {
  375. $tmp[] = $login{$i};
  376. }
  377. shuffle($tmp);
  378. return implode($tmp, '');
  379. }
  380. /**
  381. * Create pronounceable password
  382. *
  383. * This method creates a string that consists of
  384. * vowels and consonats.
  385. *
  386. * @access private
  387. * @param integer Length of the password
  388. * @return string Returns the password
  389. */
  390. function _createPronounceable($length)
  391. {
  392. $retVal = '';
  393. /**
  394. * List of vowels and vowel sounds
  395. */
  396. $v = array('a', 'e', 'i', 'o', 'u', 'ae', 'ou', 'io',
  397. 'ea', 'ou', 'ia', 'ai'
  398. );
  399. /**
  400. * List of consonants and consonant sounds
  401. */
  402. $c = array('b', 'c', 'd', 'g', 'h', 'j', 'k', 'l', 'm',
  403. 'n', 'p', 'r', 's', 't', 'u', 'v', 'w',
  404. 'tr', 'cr', 'fr', 'dr', 'wr', 'pr', 'th',
  405. 'ch', 'ph', 'st', 'sl', 'cl'
  406. );
  407. $v_count = 12;
  408. $c_count = 29;
  409. $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] = $v_count + $c_count;
  410. for ($i = 0; $i < $length; $i++) {
  411. $retVal .= $c[mt_rand(0, $c_count-1)] . $v[mt_rand(0, $v_count-1)];
  412. }
  413. return substr($retVal, 0, $length);
  414. }
  415. /**
  416. * Create unpronounceable password
  417. *
  418. * This method creates a random unpronounceable password
  419. *
  420. * @access private
  421. * @param integer Length of the password
  422. * @param string Character which could be use in the
  423. * unpronounceable password ex : 'ABCDEFG'
  424. * or numeric, alphabetical or alphanumeric.
  425. * @return string Returns the password
  426. */
  427. function _createUnpronounceable($length, $chars)
  428. {
  429. $password = '';
  430. /**
  431. * List of character which could be use in the password
  432. */
  433. switch($chars) {
  434. case 'alphanumeric':
  435. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  436. $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] = 62;
  437. break;
  438. case 'alphabetical':
  439. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  440. $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] = 52;
  441. break;
  442. case 'numeric':
  443. $chars = '0123456789';
  444. $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] = 10;
  445. break;
  446. case '':
  447. $chars = '_#@%&ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  448. $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] = 67;
  449. break;
  450. default:
  451. /**
  452. * Some characters shouldn't be used
  453. */
  454. $chars = trim($chars);
  455. $chars = str_replace(array('+', '|', '$', '^', '/', '\\', ','), '', $chars);
  456. $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] = strlen($chars);
  457. }
  458. /**
  459. * Generate password
  460. */
  461. for ($i = 0; $i < $length; $i++) {
  462. $num = mt_rand(0, $GLOBALS['_Text_Password_NumberOfPossibleCharacters'] - 1);
  463. $password .= $chars{$num};
  464. }
  465. /**
  466. * Return password
  467. */
  468. return $password;
  469. }
  470. }
  471. ?>