-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy.php
More file actions
52 lines (43 loc) · 1.14 KB
/
strategy.php
File metadata and controls
52 lines (43 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
//Strategy Design Pattern
interface PasswordHashInterface {
function hash($data);
}
class MD5HashEngine implements PasswordHashInterface {
function hash($data){
return md5($data);
}
}
class SHA1HashEngine implements PasswordHashInterface {
function hash($data){
return sha1($data);
}
}
class NativeHashEngine implements PasswordHashInterface {
function hash($data){
return password_hash($data, PASSWORD_BCRYPT);
}
}
class PasswordHashing {
private $hashEngine;
function __construct(PasswordHashInterface $hashEngine = null){
$this->hashEngine = $hashEngine;
}
function setHashEngine(PasswordHashInterface $hashEngine){
$this->hashEngine = $hashEngine;
}
function getHash($data){
return $this->hashEngine->hash($data);
}
}
$password = "3iS9d0";
$md5 = new MD5HashEngine();
$sha1 = new SHA1HashEngine();
$native = new NativeHashEngine();
$ph = new PasswordHashing();
$ph->setHashEngine($md5);
echo $ph->getHash($password).PHP_EOL;
$ph->setHashEngine($sha1);
echo $ph->getHash($password).PHP_EOL;
$ph->setHashEngine($native);
echo $ph->getHash($password);