<s> import base64 <EOL> import codecs <EOL> import crypt <EOL> from hashlib import md5 , sha1 <EOL> def apache_md5crypt ( password , salt , magic = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> password = password . encode ( '<STR_LIT:utf-8>' ) <EOL> salt = salt . encode ( '<STR_LIT:utf-8>' ) <EOL> magic = magic . encode ( '<STR_LIT:utf-8>' ) <EOL> m = md5 ( ) <EOL> m . update ( password + magic + salt ) <EOL> mixin = md5 ( password + salt + password ) . digest ( ) <EOL> for i in range ( <NUM_LIT:0> , len ( password ) ) : <EOL> m . update ( mixin [ i % <NUM_LIT:16> ] ) <EOL> i = len ( password ) <EOL> while i : <EOL> if i & <NUM_LIT:1> : <EOL> m . update ( '<STR_LIT:\x00>' ) <EOL> else : <EOL> m . update ( password [ <NUM_LIT:0> ] ) <EOL> i >>= <NUM_LIT:1> <EOL> final = m . digest ( ) <EOL> for i in range ( <NUM_LIT:1000> ) : <EOL> m2 = md5 ( ) <EOL> if i & <NUM_LIT:1> : <EOL> m2 . update ( password ) <EOL> else : <EOL> m2 . update ( final ) <EOL> if i % <NUM_LIT:3> : <EOL> m2 . update ( salt ) <EOL> if i % <NUM_LIT:7> : <EOL> m2 . update ( password ) <EOL> if i & <NUM_LIT:1> : <EOL> m2 . update ( final ) <EOL> else : <EOL> m2 . update ( password ) <EOL> final = m2 . digest ( ) <EOL> itoa64 = '<STR_LIT>' <EOL> rearranged = '<STR_LIT>' <EOL> seq = ( ( <NUM_LIT:0> , <NUM_LIT:6> , <NUM_LIT:12> ) , ( <NUM_LIT:1> , <NUM_LIT:7> , <NUM_LIT> ) , ( <NUM_LIT:2> , <NUM_LIT:8> , <NUM_LIT> ) , ( <NUM_LIT:3> , <NUM_LIT:9> , <NUM_LIT:15> ) , ( <NUM_LIT:4> , <NUM_LIT:10> , <NUM_LIT:5> ) ) <EOL> for a , b , c in seq : <EOL> v = ord ( final [ a ] ) << <NUM_LIT:16> | ord ( final [ b ] ) << <NUM_LIT:8> | ord ( final [ c ] ) <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> rearranged += itoa64 [ v & <NUM_LIT> ] <EOL> v >>= <NUM_LIT:6> <EOL> v = ord ( final [ <NUM_LIT:11> ] ) <EOL> for i in range ( <NUM_LIT:2> ) : <EOL> rearranged += itoa64 [ v & <NUM_LIT> ] <EOL> v >>= <NUM_LIT:6> <EOL> return magic + salt + '<STR_LIT:$>' + rearranged <EOL> def check_password ( test_password , password ) : <EOL> if password . startswith ( '<STR_LIT>' ) : <EOL> salt = password . strip ( '<STR_LIT:$>' ) . split ( '<STR_LIT:$>' ) [ <NUM_LIT:1> ] <EOL> check = apache_md5crypt ( test_password , salt ) <EOL> elif password . startswith ( '<STR_LIT>' ) : <EOL> _hash = sha1 ( test_password . encode ( '<STR_LIT:utf-8>' ) ) . digest ( ) <EOL> check = '<STR_LIT>' + base64 . b64encode ( _hash ) <EOL> else : <EOL> check = crypt . crypt ( test_password . encode ( '<STR_LIT:utf-8>' ) , password ) <EOL> return check == password <EOL> def read_htpasswd ( path ) : <EOL> f = codecs . open ( path , '<STR_LIT:r>' , '<STR_LIT:utf-8>' ) <EOL> try : <EOL> users = { } <EOL> for line in f . readlines ( ) : <EOL> line = line . strip ( ) <EOL> if not line or line . startswith ( '<STR_LIT:#>' ) : <EOL> continue <EOL> try : <EOL> username , password = line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> continue <EOL> users [ username ] = password <EOL> return users <EOL> finally : <EOL> f . close ( ) </s>
