4444
4545namespace Md5Crypt ;
4646
47- class Md5Crypt
47+ class Md5Crypt
4848{
49- static public $ itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ' ;
50- // [a-zA-Z0-9./]
51-
52- static protected function to64 ($ v , $ n )
53- {
54- $ itoa64 = self ::$ itoa64 ;
55- $ ret = '' ;
56-
57- while (--$ n >= 0 ) {
58- $ ret .= $ itoa64 [$ v & 0x3f ];
59- $ v = $ v >> 6 ;
60- }
61-
62- return $ ret ;
63- }
64-
65- static public function apache ($ pw , $ salt = NULL )
66- {
67- $ Magic = '$apr1$ ' ;
68-
69- return self ::unix ($ pw , $ salt , $ Magic );
70- }
71-
72- static public function unix ($ pw , $ salt = NULL , $ Magic = '$1$ ' )
73- {
74- $ itoa64 = self ::$ itoa64 ;
75-
76- if ( $ salt !== NULL ) {
77- // Take care of the magic string if present
78- if (substr ($ salt , 0 , strlen ($ Magic )) == $ Magic ) {
79- $ salt = substr ($ salt , strlen ($ Magic ), strlen ($ salt ));
80- }
81- // Salt can have up to 8 characters
82- $ parts = explode ('$ ' , $ salt , 1 );
83- $ salt = substr ($ parts [0 ], 0 , 8 );
84- } else {
85- $ salt = '' ;
86- mt_srand ((double ) (microtime () * 10000000 ));
87-
88- while (strlen ($ salt ) < 8 ) {
89- $ salt .= $ itoa64 [mt_rand (0 , strlen ($ itoa64 )- 1 )];
90- }
91- }
92-
93- $ ctx = $ pw . $ Magic . $ salt ;
94-
95- $ final = pack ('H* ' , md5 ($ pw . $ salt . $ pw ));
96-
97- for ($ pl = strlen ($ pw ); $ pl > 0 ; $ pl -= 16 ) {
98- $ ctx .= substr ($ final , 0 , ($ pl > 16 ) ? 16 : $ pl );
99- }
100-
101- // Now the 'weird' xform
102- for ($ i = strlen ($ pw ); $ i ; $ i >>= 1 ) {
103- if ($ i & 1 ) { // This comes from the original version,
104- $ ctx .= pack (" C " , 0 ); // where a memset() is done to $final
105- } else { // before this loop
106- $ ctx .= $ pw [0 ];
107- }
108- }
109-
110- $ final = pack ('H* ' , md5 ($ ctx )); // The following is supposed to make
111- // things run slower
112-
113- for ($ i = 0 ; $ i < 1000 ; $ i ++ ) {
114- $ ctx1 = '' ;
115- if ($ i & 1 ) {
116- $ ctx1 .= $ pw ;
117- } else {
118- $ ctx1 .= substr ($ final , 0 , 16 );
119- }
120- if ($ i % 3 ) {
121- $ ctx1 .= $ salt ;
122- }
123- if ($ i % 7 ) {
124- $ ctx1 .= $ pw ;
125- }
126- if ($ i & 1 ) {
127- $ ctx1 .= substr ($ final , 0 , 16 );
128- } else {
129- $ ctx1 .= $ pw ;
130- }
131- $ final = pack ('H* ' , md5 ($ ctx1 ));
132- }
133-
134- // Final xform
135- $ passwd = '' ;
136- $ passwd .= self ::to64 ((intval (ord ($ final [0 ])) << 16 )
137- | (intval (ord ($ final [6 ])) << 8 )
138- | (intval (ord ($ final [12 ]))),4 );
139- $ passwd .= self ::to64 ((intval (ord ($ final [1 ])) << 16 )
140- | (intval (ord ($ final [7 ])) << 8 )
141- | (intval (ord ($ final [13 ]))), 4 );
142- $ passwd .= self ::to64 ((intval (ord ($ final [2 ])) << 16 )
143- | (intval (ord ($ final [8 ])) << 8 )
144- | (intval (ord ($ final [14 ]))), 4 );
145- $ passwd .= self ::to64 ((intval (ord ($ final [3 ])) << 16 )
146- | (intval (ord ($ final [9 ])) << 8 )
147- | (intval (ord ($ final [15 ]))), 4 );
148- $ passwd .= self ::to64 ((intval (ord ($ final [4 ]) << 16 )
149- | (intval (ord ($ final [10 ])) << 8 )
150- | (intval (ord ($ final [5 ])))), 4 );
151- $ passwd .= self ::to64 ((intval (ord ($ final [11 ]))), 2 );
152-
153- // Return the final string
154- return $ Magic . $ salt . '$ ' . $ passwd ;
155- }
49+ public static $ itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ' ;
50+ // [a-zA-Z0-9./]
51+
52+ protected static function to64 ($ v , $ n )
53+ {
54+ $ itoa64 = self ::$ itoa64 ;
55+ $ ret = '' ;
56+
57+ while (--$ n >= 0 ) {
58+ $ ret .= $ itoa64 [$ v & 0x3f ];
59+ $ v = $ v >> 6 ;
60+ }
61+
62+ return $ ret ;
63+ }
64+
65+ public static function apache ($ pw , $ salt = null )
66+ {
67+ $ Magic = '$apr1$ ' ;
68+
69+ return self ::unix ($ pw , $ salt , $ Magic );
70+ }
71+
72+ public static function unix ($ pw , $ salt = null , $ Magic = '$1$ ' )
73+ {
74+ $ itoa64 = self ::$ itoa64 ;
75+
76+ if ( null !== $ salt ) {
77+ // Take care of the magic string if present
78+ if (substr ($ salt , 0 , strlen ($ Magic )) == $ Magic ) {
79+ $ salt = substr ($ salt , strlen ($ Magic ), strlen ($ salt ));
80+ }
81+ // Salt can have up to 8 characters
82+ $ parts = explode ('$ ' , $ salt , 1 );
83+ $ salt = substr ($ parts [0 ], 0 , 8 );
84+ } else {
85+ $ salt = '' ;
86+ mt_srand ((float ) (microtime () * 10000000 ));
87+
88+ while (strlen ($ salt ) < 8 ) {
89+ $ salt .= $ itoa64 [mt_rand (0 , strlen ($ itoa64 ) - 1 )];
90+ }
91+ }
92+
93+ $ ctx = $ pw . $ Magic . $ salt ;
94+
95+ $ final = pack ('H* ' , md5 ($ pw . $ salt . $ pw ));
96+
97+ for ($ pl = strlen ($ pw ); $ pl > 0 ; $ pl -= 16 ) {
98+ $ ctx .= substr ($ final , 0 , ($ pl > 16 ) ? 16 : $ pl );
99+ }
100+
101+ // Now the 'weird' xform
102+ for ($ i = strlen ($ pw ); $ i ; $ i >>= 1 ) {
103+ if ($ i & 1 ) { // This comes from the original version,
104+ $ ctx .= pack (' C ' , 0 ); // where a memset() is done to $final
105+ } else { // before this loop
106+ $ ctx .= $ pw [0 ];
107+ }
108+ }
109+
110+ $ final = pack ('H* ' , md5 ($ ctx )); // The following is supposed to make
111+ // things run slower
112+
113+ for ($ i = 0 ; $ i < 1000 ; ++ $ i ) {
114+ $ ctx1 = '' ;
115+ if ($ i & 1 ) {
116+ $ ctx1 .= $ pw ;
117+ } else {
118+ $ ctx1 .= substr ($ final , 0 , 16 );
119+ }
120+ if ($ i % 3 ) {
121+ $ ctx1 .= $ salt ;
122+ }
123+ if ($ i % 7 ) {
124+ $ ctx1 .= $ pw ;
125+ }
126+ if ($ i & 1 ) {
127+ $ ctx1 .= substr ($ final , 0 , 16 );
128+ } else {
129+ $ ctx1 .= $ pw ;
130+ }
131+ $ final = pack ('H* ' , md5 ($ ctx1 ));
132+ }
133+
134+ // Final xform
135+ $ passwd = '' ;
136+ $ passwd .= self ::to64 ((intval (ord ($ final [0 ])) << 16 )
137+ | (intval (ord ($ final [6 ])) << 8 )
138+ | (intval (ord ($ final [12 ]))), 4 );
139+ $ passwd .= self ::to64 ((intval (ord ($ final [1 ])) << 16 )
140+ | (intval (ord ($ final [7 ])) << 8 )
141+ | (intval (ord ($ final [13 ]))), 4 );
142+ $ passwd .= self ::to64 ((intval (ord ($ final [2 ])) << 16 )
143+ | (intval (ord ($ final [8 ])) << 8 )
144+ | (intval (ord ($ final [14 ]))), 4 );
145+ $ passwd .= self ::to64 ((intval (ord ($ final [3 ])) << 16 )
146+ | (intval (ord ($ final [9 ])) << 8 )
147+ | (intval (ord ($ final [15 ]))), 4 );
148+ $ passwd .= self ::to64 ((intval (ord ($ final [4 ]) << 16 )
149+ | (intval (ord ($ final [10 ])) << 8 )
150+ | (intval (ord ($ final [5 ])))), 4 );
151+ $ passwd .= self ::to64 ((intval (ord ($ final [11 ]))), 2 );
152+
153+ // Return the final string
154+ return $ Magic . $ salt . '$ ' . $ passwd ;
155+ }
156156}
157157
158- // eof
158+ // eof
0 commit comments