PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Classes/TYPO3/FLOW3/Utility/Algorithms.php

https://github.com/christianjul/FLOW3-Composer
PHP | 72 lines | 23 code | 9 blank | 40 comment | 0 complexity | 91f17a796963728f5146bee54f07c219 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Utility;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. require_once(FLOW3_PATH_FLOW3 . 'Resources/PHP/iSecurity/Security_Randomizer.php');
  13. use TYPO3\FLOW3\Annotations as FLOW3;
  14. /**
  15. * A utility class for various algorithms.
  16. *
  17. * @FLOW3\Scope("singleton")
  18. */
  19. class Algorithms {
  20. /**
  21. * Generates a universally unique identifier (UUID) according to RFC 4122.
  22. * The algorithm used here, might not be completely random.
  23. *
  24. * @return string The universally unique id
  25. * @todo check for randomness, optionally generate type 1 and type 5 UUIDs, use php5-uuid extension if available
  26. */
  27. static public function generateUUID() {
  28. return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  29. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
  30. mt_rand( 0, 0x0fff ) | 0x4000,
  31. mt_rand( 0, 0x3fff ) | 0x8000,
  32. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
  33. }
  34. /**
  35. * Returns a string of random bytes.
  36. *
  37. * @param integer $count Number of bytes to generate
  38. * @return string Random bytes
  39. */
  40. static public function generateRandomBytes($count) {
  41. return \Security_Randomizer::getRandomBytes($count);
  42. }
  43. /**
  44. * Returns a random token in hex format.
  45. *
  46. * @param integer $count Token length
  47. * @return string A random token
  48. */
  49. static public function generateRandomToken($count) {
  50. return \Security_Randomizer::getRandomToken($count);
  51. }
  52. /**
  53. * Returns a random string with alpha-numeric characters.
  54. *
  55. * @param integer $count Number of characters to generate
  56. * @param string $characters Allowed characters, defaults to alpha-numeric (a-zA-Z0-9)
  57. * @return string A random string
  58. */
  59. static public function generateRandomString($count, $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {
  60. return \Security_Randomizer::getRandomString($count, $characters);
  61. }
  62. }
  63. ?>