PageRenderTime 854ms CodeModel.GetById 43ms RepoModel.GetById 0ms app.codeStats 0ms

/typo3/sysext/saltedpasswords/classes/salts/class.tx_saltedpasswords_abstract_salts.php

https://bitbucket.org/linxpinx/mercurial
PHP | 127 lines | 39 code | 9 blank | 79 comment | 7 complexity | 5d0047ca3229184c35e6c1bb9964a5e8 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Unlicense, LGPL-2.1, Apache-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 2009-2010 Marcus Krause <marcus#exp2009@t3sec.info>
  6. * All rights reserved
  7. *
  8. * This script is part of the TYPO3 project. The TYPO3 project is
  9. * free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * The GNU General Public License can be found at
  15. * http://www.gnu.org/copyleft/gpl.html.
  16. * A copy is found in the textfile GPL.txt and important notices to the license
  17. * from the author is found in LICENSE.txt distributed with these scripts.
  18. *
  19. *
  20. * This script is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * This copyright notice MUST APPEAR in all copies of the script!
  26. ***************************************************************/
  27. /**
  28. * Contains abstract class "tx_saltedpasswords_abstract_salts"
  29. * to be used in classes that provide salted hashing.
  30. *
  31. * $Id: class.tx_saltedpasswords_abstract_salts.php 7905 2010-06-13 14:42:33Z ohader $
  32. */
  33. /**
  34. * Abtract class with methods needed to be extended
  35. * in a salted hashing class.
  36. *
  37. * @author Marcus Krause <marcus#exp2009@t3sec.info>
  38. *
  39. * @abstract
  40. * @since 2009-09-06
  41. * @package TYPO3
  42. * @subpackage tx_saltedpasswords
  43. */
  44. abstract class tx_saltedpasswords_abstract_salts {
  45. /**
  46. * Method applies settings (prefix, optional hash count, optional suffix)
  47. * to a salt.
  48. *
  49. * @param string $salt: a salt to apply setting to
  50. * @return string salt with setting
  51. */
  52. abstract protected function applySettingsToSalt($salt);
  53. /**
  54. * Generates a random base salt settings for the hash.
  55. *
  56. * @return string a string containing settings and a random salt
  57. */
  58. abstract protected function getGeneratedSalt();
  59. /**
  60. * Returns a string for mapping an int to the corresponding base 64 character.
  61. *
  62. * @return string string for mapping an int to the corresponding base 64 character
  63. */
  64. abstract protected function getItoa64();
  65. /**
  66. * Returns setting string to indicate type of hashing method.
  67. *
  68. * @return string setting string of hashing method
  69. */
  70. abstract protected function getSetting();
  71. /**
  72. * Encodes bytes into printable base 64 using the *nix standard from crypt().
  73. *
  74. * @param string $input: the string containing bytes to encode.
  75. * @param integer $count: the number of characters (bytes) to encode.
  76. * @return string encoded string
  77. */
  78. public function base64Encode($input, $count) {
  79. $output = '';
  80. $i = 0;
  81. $itoa64 = $this->getItoa64();
  82. do {
  83. $value = ord($input[$i++]);
  84. $output .= $itoa64[$value & 0x3f];
  85. if ($i < $count) {
  86. $value |= ord($input[$i]) << 8;
  87. }
  88. $output .= $itoa64[($value >> 6) & 0x3f];
  89. if ($i++ >= $count) {
  90. break;
  91. }
  92. if ($i < $count) {
  93. $value |= ord($input[$i]) << 16;
  94. }
  95. $output .= $itoa64[($value >> 12) & 0x3f];
  96. if ($i++ >= $count) {
  97. break;
  98. }
  99. $output .= $itoa64[($value >> 18) & 0x3f];
  100. } while ($i < $count);
  101. return $output;
  102. }
  103. /**
  104. * Method determines required length of base64 characters for a given
  105. * length of a byte string.
  106. *
  107. * @param integer $byteLength: length of bytes to calculate in base64 chars
  108. * @return integer required length of base64 characters
  109. */
  110. protected function getLengthBase64FromBytes($byteLength) {
  111. // calculates bytes in bits in base64
  112. return intval(ceil(($byteLength * 8) / 6));
  113. }
  114. }
  115. if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/saltedpasswords/classes/salts/class.tx_saltedpasswords_abstract_salts.php']) {
  116. include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/saltedpasswords/classes/salts/class.tx_saltedpasswords_abstract_salts.php']);
  117. }
  118. ?>