These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * The Hash medium strength mixer class |
||
4 | * |
||
5 | * This class implements a mixer based upon the recommendations in RFC 4086 |
||
6 | * section 5.2 |
||
7 | * |
||
8 | * PHP version 5.3 |
||
9 | * |
||
10 | * @see http://tools.ietf.org/html/rfc4086#section-5.2 |
||
11 | * @category PHPCryptLib |
||
12 | * @package Random |
||
13 | * @subpackage Mixer |
||
14 | * @author Anthony Ferrara <[email protected]> |
||
15 | * @copyright 2011 The Authors |
||
16 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||
17 | * @version Build @@version@@ |
||
18 | */ |
||
19 | |||
20 | namespace RandomLib\Mixer; |
||
21 | |||
22 | use \SecurityLib\Strength; |
||
23 | use \SecurityLib\Util; |
||
24 | |||
25 | /** |
||
26 | * The Hash medium strength mixer class |
||
27 | * |
||
28 | * This class implements a mixer based upon the recommendations in RFC 4086 |
||
29 | * section 5.2 |
||
30 | * |
||
31 | * @see http://tools.ietf.org/html/rfc4086#section-5.2 |
||
32 | * @category PHPCryptLib |
||
33 | * @package Random |
||
34 | * @subpackage Mixer |
||
35 | * @author Anthony Ferrara <[email protected]> |
||
36 | */ |
||
37 | class Hash extends \RandomLib\AbstractMixer { |
||
38 | |||
39 | /** |
||
40 | * @var string The hash instance to use |
||
41 | */ |
||
42 | protected $hash = null; |
||
43 | |||
44 | /** |
||
45 | * Build the hash mixer |
||
46 | * |
||
47 | * @param string $hash The hash instance to use (defaults to sha512) |
||
48 | * |
||
49 | * @return void |
||
50 | */ |
||
51 | public function __construct($hash = 'sha512') { |
||
52 | $this->hash = $hash; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Return an instance of Strength indicating the strength of the source |
||
57 | * |
||
58 | * @return Strength An instance of one of the strength classes |
||
59 | */ |
||
60 | public static function getStrength() { |
||
61 | return new Strength(Strength::MEDIUM); |
||
0 ignored issues
–
show
|
|||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Test to see if the mixer is available |
||
66 | * |
||
67 | * @return boolean If the mixer is available on the system |
||
68 | */ |
||
69 | public static function test() { |
||
70 | return true; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Get the block size (the size of the individual blocks used for the mixing) |
||
75 | * |
||
76 | * @return int The block size |
||
77 | */ |
||
78 | protected function getPartSize() { |
||
79 | return Util::safeStrlen(hash($this->hash, '', true)); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Mix 2 parts together using one method |
||
84 | * |
||
85 | * @param string $part1 The first part to mix |
||
86 | * @param string $part2 The second part to mix |
||
87 | * |
||
88 | * @return string The mixed data |
||
89 | */ |
||
90 | protected function mixParts1($part1, $part2) { |
||
91 | return hash_hmac($this->hash, $part1, $part2, true); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Mix 2 parts together using another different method |
||
96 | * |
||
97 | * @param string $part1 The first part to mix |
||
98 | * @param string $part2 The second part to mix |
||
99 | * |
||
100 | * @return string The mixed data |
||
101 | */ |
||
102 | protected function mixParts2($part1, $part2) { |
||
103 | return hash_hmac($this->hash, $part2, $part1, true); |
||
104 | } |
||
105 | |||
106 | } |
||
107 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.