1313use Robin \Ntlm \Credential \Password ;
1414use Robin \Ntlm \Crypt \CipherMode ;
1515use Robin \Ntlm \Crypt \Des \DesEncrypterInterface ;
16- use Robin \Ntlm \Crypt \Random \RandomByteGeneratorInterface ;
17- use SplFixedArray ;
1816
1917/**
2018 * Uses the "LM hash" computation strategy to hash a {@link Password} credential
@@ -53,14 +51,6 @@ class LmHasher implements HasherInterface
5351 */
5452 const PASSWORD_SLICE_LENGTH = 7 ;
5553
56- /**
57- * The length of the randomly generated binary string used for generating
58- * each piece of the resulting hash.
59- *
60- * @type int
61- */
62- const RANDOM_BINARY_STRING_LENGTH = 8 ;
63-
6454 /**
6555 * The constant known ASCII text to encrypt with the generated keys.
6656 *
@@ -83,14 +73,6 @@ class LmHasher implements HasherInterface
8373 */
8474 private $ des_encrypter ;
8575
86- /**
87- * The generator used to generate cryptographically secure random bytes to
88- * provide an initialization vector for encryption.
89- *
90- * @type RandomByteGeneratorInterface
91- */
92- private $ random_byte_generator ;
93-
9476
9577 /**
9678 * Methods
@@ -101,16 +83,10 @@ class LmHasher implements HasherInterface
10183 *
10284 * @param DesEncrypterInterface $des_encrypter The DES encryption engine
10385 * used to generate the hash.
104- * @param RandomByteGeneratorInterface $random_byte_generator Used to
105- * generate cryptographically secure random bytes to provide an
106- * initialization vector for encryption.
10786 */
108- public function __construct (
109- DesEncrypterInterface $ des_encrypter ,
110- RandomByteGeneratorInterface $ random_byte_generator
111- ) {
87+ public function __construct (DesEncrypterInterface $ des_encrypter )
88+ {
11289 $ this ->des_encrypter = $ des_encrypter ;
113- $ this ->random_byte_generator = $ random_byte_generator ;
11490 }
11591
11692 /**
@@ -134,53 +110,16 @@ public function hash(Password $password)
134110 $ binary_hash = array_reduce (
135111 $ halves ,
136112 function ($ result , $ half ) {
137- $ expanded = static ::expand56BitKeyTo64BitKey ($ half );
138-
139113 return $ result . $ this ->des_encrypter ->encrypt (
140- $ expanded ,
114+ $ half ,
141115 static ::ENCRYPT_DATA_CONSTANT ,
142116 CipherMode::ECB ,
143- $ this -> random_byte_generator -> generate ( static :: RANDOM_BINARY_STRING_LENGTH )
117+ '' // DES-ECB expects a 0-byte-length initialization vector
144118 );
145119 },
146120 ''
147121 );
148122
149123 return Hash::fromBinaryString ($ binary_hash , HashType::LM );
150124 }
151-
152- /**
153- * Expands a 56-bit key to a full 64-bit key for DES encryption.
154- *
155- * @link http://php.net/manual/en/ref.hash.php#84587 Implementation basis.
156- * @link https://github.com/jclulow/node-smbhash/blob/edc48e2b93067/lib/common.js Inspired by Joshua Clulow's work.
157- * @param string $string_key The 56-bit key to expand
158- * @return string
159- */
160- public static function expand56BitKeyTo64BitKey ($ string_key )
161- {
162- $ byte_array_56 = new SplFixedArray (7 );
163- $ byte_array_64 = new SplFixedArray (8 );
164- $ key_64bit = '' ;
165-
166- // Get the byte value of each ASCII character in the string
167- for ($ i = 0 ; $ i < $ byte_array_56 ->getSize (); $ i ++) {
168- $ byte_array_56 [$ i ] = isset ($ string_key [$ i ]) ? ord ($ string_key [$ i ]) : 0 ;
169- }
170-
171- $ byte_array_64 [0 ] = $ byte_array_56 [0 ] & 254 ;
172- $ byte_array_64 [1 ] = ($ byte_array_56 [0 ] << 7 ) | ($ byte_array_56 [1 ] >> 1 );
173- $ byte_array_64 [2 ] = ($ byte_array_56 [1 ] << 6 ) | ($ byte_array_56 [2 ] >> 2 );
174- $ byte_array_64 [3 ] = ($ byte_array_56 [2 ] << 5 ) | ($ byte_array_56 [3 ] >> 3 );
175- $ byte_array_64 [4 ] = ($ byte_array_56 [3 ] << 4 ) | ($ byte_array_56 [4 ] >> 4 );
176- $ byte_array_64 [5 ] = ($ byte_array_56 [4 ] << 3 ) | ($ byte_array_56 [5 ] >> 5 );
177- $ byte_array_64 [6 ] = ($ byte_array_56 [5 ] << 2 ) | ($ byte_array_56 [6 ] >> 6 );
178- $ byte_array_64 [7 ] = $ byte_array_56 [6 ] << 1 ;
179-
180- foreach ($ byte_array_64 as $ byte_val ) {
181- $ key_64bit .= chr ($ byte_val );
182- }
183-
184- return $ key_64bit ;
185- }
186125}
0 commit comments