@@ -107,13 +107,13 @@ void sha256_base64(const char *clear, int len, char *out)
107107 SHA256_CTX context ;
108108 apr_byte_t digest [SHA256_DIGEST_LENGTH ];
109109
110- apr__SHA256_Init ( & context );
111- apr__SHA256_Update ( & context , (const unsigned char * )clear , len );
112- apr__SHA256_Final ( digest , & context );
110+ apr__SHA256_Init (& context );
111+ apr__SHA256_Update (& context , (const unsigned char * )clear , len );
112+ apr__SHA256_Final (digest , & context );
113113
114- apr_cpystrn ( out , APR_SHA256PW_ID , APR_SHA256PW_IDLEN + 1 );
114+ apr_cpystrn (out , APR_SHA256PW_ID , APR_SHA256PW_IDLEN + 1 );
115115
116- l = apr_base64_encode_binary ( out + APR_SHA256PW_IDLEN , digest , sizeof (digest ) );
116+ l = apr_base64_encode_binary (out + APR_SHA256PW_IDLEN , digest , sizeof (digest ));
117117 out [l + APR_SHA256PW_IDLEN ] = '\0' ;
118118}
119119
@@ -218,12 +218,12 @@ char* mk_hash(int alg, const char *passwd, const char *mysalt)
218218 case ALG_PHPMD5 :
219219 md5str [0 ] = '\0' ;
220220
221- apr_md5_init ( & context );
222- apr_md5_update ( & context , passwd , strlen (passwd ) );
223- apr_md5_final ( digest , & context );
224- for ( i = 0 , r = md5str ; i < APR_MD5_DIGESTSIZE ; i ++ , r += 2 )
221+ apr_md5_init (& context );
222+ apr_md5_update (& context , passwd , strlen (passwd ));
223+ apr_md5_final (digest , & context );
224+ for ( i = 0 , r = md5str ; i < APR_MD5_DIGESTSIZE ; i ++ , r += 2 )
225225 {
226- sprintf ( r , "%02x" , digest [i ] );
226+ sprintf (r , "%02x" , digest [i ]);
227227 }
228228 * r = '\0' ;
229229
@@ -234,12 +234,12 @@ char* mk_hash(int alg, const char *passwd, const char *mysalt)
234234 case ALG_SHA256HEX :
235235 sha256str [0 ] = '\0' ;
236236
237- apr__SHA256_Init ( & context256 );
238- apr__SHA256_Update ( & context256 , passwd , strlen (passwd ) );
239- apr__SHA256_Final ( digest256 , & context256 );
240- for ( i = 0 , r = sha256str ; i < SHA256_DIGEST_LENGTH ; i ++ , r += 2 )
237+ apr__SHA256_Init (& context256 );
238+ apr__SHA256_Update (& context256 , passwd , strlen (passwd ));
239+ apr__SHA256_Final (digest256 , & context256 );
240+ for ( i = 0 , r = sha256str ; i < SHA256_DIGEST_LENGTH ; i ++ , r += 2 )
241241 {
242- sprintf ( r , "%02x" , digest256 [i ] );
242+ sprintf (r , "%02x" , digest256 [i ]);
243243 }
244244 * r = '\0' ;
245245
@@ -254,3 +254,86 @@ char* mk_hash(int alg, const char *passwd, const char *mysalt)
254254
255255 return result ;
256256}
257+
258+ int validate_hash (const char * password , const char * hash )
259+ {
260+ apr_status_t status ;
261+ char * tmphash , * result ;
262+
263+ if (!strncmp (hash , APR_SHA256PW_ID , APR_SHA256PW_IDLEN ))
264+ {
265+ tmphash = mk_hash (ALG_APSHA256 , password , NULL );
266+
267+ if (apr_strnatcmp (hash , tmphash ) == 0 )
268+ {
269+ free (tmphash );
270+ return TRUE;
271+ }
272+ else
273+ {
274+ free (tmphash );
275+ return FALSE;
276+ }
277+ }
278+
279+ if (strlen (hash ) == 32 && (hash [0 ] != '$' ))
280+ {
281+ tmphash = mk_hash (ALG_PHPMD5 , password , NULL );
282+
283+ if (apr_strnatcmp (hash , tmphash ) == 0 )
284+ {
285+ free (tmphash );
286+ return TRUE;
287+ }
288+ else
289+ {
290+ free (tmphash );
291+ return FALSE;
292+ }
293+ }
294+
295+ if (strlen (hash ) == 64 && (hash [0 ] != '$' ))
296+ {
297+ tmphash = mk_hash (ALG_SHA256HEX , password , NULL );
298+
299+ if (apr_strnatcmp (hash , tmphash ) == 0 )
300+ {
301+ free (tmphash );
302+ return TRUE;
303+ }
304+ else
305+ {
306+ free (tmphash );
307+ return FALSE;
308+ }
309+ }
310+
311+ status = apr_password_validate (password , hash );
312+
313+ if (status == APR_SUCCESS )
314+ {
315+ return TRUE;
316+ }
317+ #ifndef WIN32
318+ else
319+ {
320+ // maybe a different encrypted password (glibc2 crypt)?
321+ result = crypt (password , hash );
322+ if (result != NULL )
323+ {
324+ if (strcmp (hash , result ) == 0 )
325+ {
326+ return TRUE;
327+ }
328+ else
329+ {
330+ return FALSE;
331+ }
332+ }
333+ else
334+ {
335+ return FALSE;
336+ }
337+ }
338+ #endif
339+ }
0 commit comments