PageRenderTime 48ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pear/Text/Password.php

https://bitbucket.org/blackriver/openx
PHP | 536 lines | 276 code | 60 blank | 200 comment | 36 complexity | c2f80dfe6cfb56d8774374e373bdea04 MD5 | raw file
  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 6 2006-12-15 17:27:27Z $
  21. * @link http://pear.php.net/package/Text_Password
  22. */
  23. /**
  24. * Number of possible characters in the password
  25. */
  26. $_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 : 'A,B,C,D,E,F,G'
  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. global $_Text_Password_NumberOfPossibleCharacters;
  393. $retVal = '';
  394. /**
  395. * List of vowels and vowel sounds
  396. */
  397. $v = array('a', 'e', 'i', 'o', 'u', 'ae', 'ou', 'io',
  398. 'ea', 'ou', 'ia', 'ai'
  399. );
  400. /**
  401. * List of consonants and consonant sounds
  402. */
  403. $c = array('b', 'c', 'd', 'g', 'h', 'j', 'k', 'l', 'm',
  404. 'n', 'p', 'r', 's', 't', 'u', 'v', 'w',
  405. 'tr', 'cr', 'fr', 'dr', 'wr', 'pr', 'th',
  406. 'ch', 'ph', 'st', 'sl', 'cl'
  407. );
  408. $v_count = 12;
  409. $c_count = 29;
  410. $_Text_Password_NumberOfPossibleCharacters = $v_count + $c_count;
  411. for ($i = 0; $i < $length; $i++) {
  412. $retVal .= $c[mt_rand(0, $c_count-1)] . $v[mt_rand(0, $v_count-1)];
  413. }
  414. return substr($retVal, 0, $length);
  415. }
  416. /**
  417. * Create unpronounceable password
  418. *
  419. * This method creates a random unpronounceable password
  420. *
  421. * @access private
  422. * @param integer Length of the password
  423. * @param string Character which could be use in the
  424. * unpronounceable password ex : 'ABCDEFG'
  425. * or numeric, alphabetical or alphanumeric.
  426. * @return string Returns the password
  427. */
  428. function _createUnpronounceable($length, $chars)
  429. {
  430. global $_Text_Password_NumberOfPossibleCharacters;
  431. $password = '';
  432. /**
  433. * List of character which could be use in the password
  434. */
  435. switch($chars) {
  436. case 'alphanumeric':
  437. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  438. $_Text_Password_NumberOfPossibleCharacters = 62;
  439. break;
  440. case 'alphabetical':
  441. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  442. $_Text_Password_NumberOfPossibleCharacters = 52;
  443. break;
  444. case 'numeric':
  445. $chars = '0123456789';
  446. $_Text_Password_NumberOfPossibleCharacters = 10;
  447. break;
  448. case '':
  449. $chars = '_#@%&ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  450. $_Text_Password_NumberOfPossibleCharacters = 67;
  451. break;
  452. default:
  453. /**
  454. * Some characters shouldn't be used
  455. */
  456. $chars = trim($chars);
  457. $chars = str_replace(array('+', '|', '$', '^', '/', '\\', ','), '', $chars);
  458. $_Text_Password_NumberOfPossibleCharacters = strlen($chars);
  459. }
  460. /**
  461. * Generate password
  462. */
  463. for ($i = 0; $i < $length; $i++) {
  464. $num = mt_rand(0, $_Text_Password_NumberOfPossibleCharacters - 1);
  465. $password .= $chars{$num};
  466. }
  467. /**
  468. * Return password
  469. */
  470. return $password;
  471. }
  472. }
  473. ?>