Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. <?php
  2.  
  3. class Teste {
  4. const EU = "<b>Disse ele:</b>n";
  5.  
  6. public function eu(){
  7. return self::EU . $this->ele();
  8. }
  9. private function ele(){
  10. return 'Pertenço a 3º pessoa';
  11. }
  12. }
  13.  
  14. $teste = new Teste();
  15. print $teste->eu();
  16.  
  17. ?>
  18.  
  19. class Teste1 {
  20. const EU = "<b>Disse ele:</b>n";
  21.  
  22. public static function eu(){
  23. return self::EU . $this->ele();
  24. }
  25. private function ele(){
  26. return 'Pertenço à 3º pessoa';
  27. }
  28. }
  29.  
  30. print Teste1::eu();
  31.  
  32. <?php
  33.  
  34. class Teste1 {
  35. static $instance;
  36. const EU = "<b>Disse ele:</b>n";
  37.  
  38. public static function get(){
  39. if(empty(self::$instance)):
  40. self::$instance = new Teste1();
  41. endif;
  42. return self::$instance;
  43. }
  44.  
  45. public static function eu(){
  46. return self::EU . self::$instance->ele();
  47. }
  48. private function ele(){
  49. return 'Pertenço à 3º pessoa';
  50. }
  51. }
  52.  
  53. print Teste1::get()->eu();
  54.  
  55. ?>
  56.  
  57. <?php
  58.  
  59. class Hash {
  60.  
  61. static $hash;
  62. const COST = "$2y$10$";
  63.  
  64. public static function hash_create($password){
  65. return crypt($password, $this->salt(self::COST)); # <--- $this
  66. }
  67. public static function hash_verify($password, $db_hash){
  68. $hash = crypt($password, $db_hash);
  69. return $this->are_equal($hash, $db_hash); # <--- $this
  70. }
  71.  
  72. // métodos projectados apenas para uso interno
  73. private function random(){
  74. return md5(uniqid(), true);
  75. }
  76. private function fix_random($random){
  77. $encode = base64_encode($random);
  78. return str_replace("+", ".", $encode);
  79. }
  80. private function half_salt($size=null){
  81. $size = empty($size) ? 22 : $size;
  82. return substr($this->fix_random($this->random()), 0, $size);
  83. }
  84. private function salt($cost){
  85. return $cost.$this->half_salt();
  86. }
  87. private function are_equal($x, $y){
  88. if($x === $y):
  89. return true;
  90. else:
  91. return false;
  92. endif;
  93. }
  94.  
  95. }
  96.  
  97. ## 3º Método - PRETENDIDO (nada será executado) ##
  98.  
  99. print Hash::hash_create('password');
  100.  
  101. print "<br/>";
  102.  
  103. $db_hash = Hash::hash_create('password');
  104. var_dump(Hash::hash_verify('password', $db_hash)); # (null);
  105.  
  106.  
  107. ?>
  108.  
  109. <?php
  110.  
  111. class Hash {
  112.  
  113. static $hash;
  114. const COST = "$2y$10$";
  115.  
  116. # 1º notação
  117. public function create($password){
  118. return crypt($password, $this->salt(self::COST));
  119. }
  120. public function verify($password, $db_hash){
  121. $hash = crypt($password, $db_hash);
  122. return $this->are_equal($hash, $db_hash);
  123. }
  124.  
  125. // métodos projectados apenas para uso interno
  126. private function random(){
  127. return md5(uniqid(), true);
  128. }
  129. private function fix_random($random){
  130. $encode = base64_encode($random);
  131. return str_replace("+", ".", $encode);
  132. }
  133. private function half_salt($size=null){
  134. $size = empty($size) ? 22 : $size;
  135. return substr($this->fix_random($this->random()), 0, $size);
  136. }
  137. private function salt($cost){
  138. return $cost.$this->half_salt();
  139. }
  140. private function are_equal($x, $y){
  141. if($x === $y):
  142. return true;
  143. else:
  144. return false;
  145. endif;
  146. }
  147.  
  148. }
  149.  
  150. ## 1º Método - NORMAL ##
  151. $hash = new Hash(); # <---
  152. print $hash->create('password');
  153.  
  154. print "<br/>";
  155.  
  156. $db_hash = $hash->create('password');
  157. var_dump($hash->verify('password', $db_hash)); # (true);
  158.  
  159.  
  160. ?>
  161.  
  162. <?php
  163.  
  164. class Hash {
  165.  
  166. static $hash;
  167. const COST = "$2y$10$";
  168.  
  169. public static function instance(){
  170. if(empty(self::$hash)){
  171. self::$hash = $self = new Hash();
  172. }
  173. return self::$hash;
  174. }
  175.  
  176. # 2º notação
  177. public static function hash_create($password){
  178. return crypt($password, self::$hash->salt(self::COST));
  179. }
  180.  
  181. public static function hash_verify($password, $db_hash){
  182. $hash = crypt($password, $db_hash);
  183. return self::$hash->are_equal($hash, $db_hash);
  184. }
  185.  
  186. // métodos projectados apenas para uso interno
  187. private function random(){
  188. return md5(uniqid(), true);
  189. }
  190. private function fix_random($random){
  191. $encode = base64_encode($random);
  192. return str_replace("+", ".", $encode);
  193. }
  194. private function half_salt($size=null){
  195. $size = empty($size) ? 22 : $size;
  196. return substr($this->fix_random($this->random()), 0, $size);
  197. }
  198. private function salt($cost){
  199. return $cost.$this->half_salt();
  200. }
  201. private function are_equal($x, $y){
  202. if($x === $y):
  203. return true;
  204. else:
  205. return false;
  206. endif;
  207. }
  208.  
  209. }
  210.  
  211. ## 2º Método - HACK ##
  212.  
  213. print Hash::instance()->hash_create('password');
  214.  
  215. print "<br/>";
  216.  
  217. $db_hash = Hash::hash_create('password');
  218. var_dump(Hash::hash_verify('password', $db_hash)); # (true);
  219.  
  220.  
  221. ?>
  222.  
  223. class Teste {
  224. const EU = "<b>Disse ele:</b>n";
  225.  
  226. public static function eu() {
  227. return self::EU . static::ele();
  228. }
  229. private static function ele(){
  230. return 'Pertenço a 3º pessoa';
  231. }
  232. }
  233.  
  234. Teste::eu();
  235.  
  236. class Usuario
  237. {
  238. public funciton setNome($nome) { $this->nome = $nome; return $this; }
  239. public function getNome(){ return $this->nome; }
  240. }
  241.  
  242.  
  243. class UsuarioFacade{
  244. public static function __callStatic($method, $arguments)
  245. {
  246. return call_user_func_array(array(new Usuario, $method), $arguments);
  247. }
  248.  
  249. }
  250.  
  251. UsuarioFacade::setNome('wallace')->getNome(); // Imprime: wallace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement