5Xc@sdZddlmZmZddlmZddlZddlZddlm Z m Z m Z ddl m Z mZmZmZde fdYZd e fd YZdd lmZmZd e fd YZdS(s(tests for passlib.utils.(des|pbkdf2|md4)i(twith_statementtdivision(thexlifyN(tPY3tutJYTHON(tTestCaset TEST_MODEt skipUnlessthbt HashInfoTestc BseeZdZdZddgZd&d'd(d)d*d+d,d-d.g Zd"Zd#Zd$Zd%Z RS(/stest various crypto functionsspasslib.crypto.digestthashlibtianatmd5sSCRAM-MD5-PLUSsMD-5tsha1ssha-1s SCRAM-SHA-1tSHA1tsha256ssha-256tSHA_256ssha2-256tripemds SCRAM-RIPEMDtRIPEMDt ripemd160s ripemd-160sSCRAM-RIPEMD-160t RIPEmd160tsha4_256ssha4-256sSHA4-256s SHA-4-256ttest128stest-128tTEST128ttest2sTEST-2t test3_128s test3-128s TEST-3-128c Cs)ddlm}ddlm}m}tj}|j|j|j tj dd|j |t dd|j |dd|j t|d x|||jD]m}xdt|jD]S\}}||}x:|D]2} || |} |j | |d| |fqWqWqWd S( snorm_hash_name()i(tchain(tnorm_hash_namet_known_hash_namestignores.*unknown hashtMD4tmd4sname=%r, format=%r:N(t itertoolsRtpasslib.crypto.digestRRtwarningstcatch_warningst __enter__t addCleanupt__exit__tfilterwarningst assertEqualRt assertRaisest TypeErrortNonetnorm_hash_samplest enumeratetnorm_hash_formats( tselfRRRtctxtrowtidxtformattcorrecttvaluetresult((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyttest_norm_hash_name+s"     cCsddlm}|jt|d|jt|d|jt|d|j|dtjddfytjd t}Wntk rt }nX|r|d }|d }|j||d df|jt |d j d n|jt|d ytjdt}Wntk r9t }nX|d}|d }|syddl m }|j||n|j||ddf|jt |d j d|jt|d|j|d|ddS(slookup_hash() -- constructori(t lookup_hashtnewt__name__tsha4R ii@tshaiitabct(0164b8a914cd2a5e74c4f7ff082c4d97f1edf880R (R t a448017aaf21d8525fc10ae87aa6729dtxxx256N(R"R9R*t ValueErrorR)R R R:tTruetFalseRtdigesttpasslib.crypto._md4R tassertIs(R0R9thas_shatrecordtconstthas_md4R ((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyttest_lookup_hash_ctorEs@"             cCsddlm}|d}|j|jd|j|jd|j|jd|j|jd|j|d||d}|j|jd|j|jd|j|jd|j|jd d S( slookup_hash() -- metadatai(R9Rssha-256i@i sSHA2-256R iN(R"R9R)tnamet iana_namet block_sizet digest_sizeRG(R0R9tinfo((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyttest_lookup_hash_metadatavs  cCsbddlm}|d}|j||||j||j||jt|ddS(s lookup_hash() -- alternate typesi(R9Ri{N(R"R9RGRJR*R+(R0R9RQ((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyttest_lookup_hash_alt_typess  (smd5smd5sSCRAM-MD5-PLUSsMD-5(Rssha-1s SCRAM-SHA-1R(ssha256ssha-256Rssha2-256(RRs SCRAM-RIPEMDR(Rs ripemd-160sSCRAM-RIPEMD-160R(Rssha4-256sSHA4-256s SHA-4-256(Rstest-128R(RRsTEST-2(Rs test3-128s TEST-3-128( R;t __module__t__doc__tdescriptionPrefixR/R-R8RLRRRS(((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyR s     1 t Pbkdf1_Testc BseZdZdZdeddddedfddddd d ed fddddd edfddddd edfddddd edfdddddedfgZesejdddddedfndZ dZ RS(stest kdf helpersspasslib.crypto.digest.pbkdf1tpasswordt78578E5A5D63CB06iiRt dc19847e05c64d2faf10ebfb4a3d2a20tsaltiR tit84it8475c6a8531a5d27t 8475c6a8531a5d27e386cd496457812ct(4a8fd48e426ed081b535be5769892fa396293efbR t f7f2e91100a8f96190f2dd177cb26453c Csbddlm}xK|jD]@\}}}}}}||||||}|j||qWdS(stest reference vectorsi(tpbkdf1N(R"Rbt pbkdf1_testsR)( R0RbtsecretR[troundstkeylenRER5R7((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyt test_knowns"csddlmdddddfd}||jt|dd|jt|dd|jt|dd |jt|d d |jt|d d |jt|d d|jt|d ddd|jt|d d dS(stest border casesi(RbRdR[iR cs|||||S(N((RdR[ReRfthash(Rb(sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pythelpersRhtmissingReit1RfiN(R"RbR*R+RB(R0Ri((RbsD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyt test_borders(RXssaltiismd5R\N( R;RTRURVR R,RcRtappendRgRl(((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyRWs!!" (t pbkdf2_hmactPBKDF2_BACKENDSt Pbkdf2Testc$BszeZdZddjeZedddddfedddd dfed ddd d fed ddd d feddddd feddddd d feddddd d fedddddfedddd dfedddddfeddd dd!fed"d#d$ddfed%d&ed'd(dd)fed*d+dddd,fed-d.d/d0dd,fed1dddd d,fed2ddd d d,fed3dddd d,fed4dd dd5d,fed6d7dd8d d,fed9dd7d8d d,fed:d#d$ddd,fed;dddd d)fed<ddd d d)fed=dddd d)fed>dd dd?d)fedddddd@fedAdBddCdd@fedDdBddCdNd@fedEdBddFd5dGfedHdBddFd5dIfgZdJZ dKZ dLZ dMZ RS(Ostest pbkdf2() supports2passlib.crypto.digest.pbkdf2_hmac() s, t cdedb5281bb2f801565a1122b2563515RXsATHENA.MIT.EDUraeburniit 01dbee7f4a9e243e988b62c73cda935dit@01dbee7f4a9e243e988b62c73cda935da05378b93244ec8f48a99e61ad799d86i t@5c08eb61fdf71e4e4ec3cf6ba1f5512ba7e52ddbc5e5142f708a31e2e62b1e13it@d1daa78615f287e6a1c8b120d7062a493f98d203e6be49a6adf4fa574b6e64ees4VxxV4it@139c30c0966bc32ba55fdbf212530ac9c5ec59f1a452f5cc9ad940fea0598ed1tXi@spass phrase equals block sizet@9ccad6d468770cd51b10e6a68721be611a8b4d282601db3b36be9246915ec82aiAspass phrase exceeds block sizet(0c60c80f961f0e71f3a9b524af6012062fe037a6R[it(ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957t(4b007901b765489abead49d926f721d065a429c1it23d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038tpasswordPASSWORDpasswordt$saltSALTsaltSALTsaltSALTsaltSALTsaltit 56fa6aa75548099dcc37d7f03425e0c3t passwordtsaltt887CFF169EA8335235D8004242AA7D6187A41E3187DF0CE14E256D85ED97A97357AAA8FF0A3871AB9EEFF458392F462F495487387F685B7472FC6C29E293F0A0thellot9290F727ED06C38BA4549EF7DE25CF5642659211B7FC076F2D28FEFD71784BB8D8F6FB244A8CC5C06240631B97008565A120764C0EE9C2CB0073994D79080136i'tsha512t55ac046e56e3089fec1691c22544b605f94185216dde0465e68b9d57c20dacbc49ca9cccf179b645991664b39d77ef317c71b845b1e30bd509112041d3a19783tpasswdRt4ddcd8f60b98be21830cee5ef22701f9641a4418d04c0414aeff08876b34ab56a1d425a1225833549adb841b51c9b3176a272bdebba1d078478f62b397f33c8dtPasswordtNaCli8t@120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17bt@ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43t@c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134atP348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9i(t@9e83f279c040f2a11aa4a02b24c418f2d3cb39560c9627fa4f47e3bcc2897c3dR\it@ea5808411eb0c7e830deab55096cee582761e22a9bc034e3ece925225b07bf46t 89b69d0516f829893c696226650a8687t@867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252t@e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f0040713f18aefdb866d53ct@d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f87f6902e072f457b5t6e23f27638084b0f7ea1734e0d9841f55dd29ea60a834466f3396bac801fac1eeb63802f03a0b4acd7603e3699c8b74437be83ff01ad7f55dac1ef60f4d56480c35ee68fd52c6936iHRt e248fb6b13365146f8ac6307cc222812Rdi t(e248fb6b13365146f8ac6307cc2228127872da6dtPb1d5485772e6f76d5ebdc11b38d3eff0a5b2bd50dc11f937e86ecacd0cd40d1b9113e0734e3b76a3i>R tPea014cc01f78d3883cac364bb5d054e2be238fb0b6081795a9d84512126e3129062104d2183464c4R c Cs{xt|jD]i}|d \}}}}}t|dkrE|dnd}t|||||}|j||q WdS(stest reference vectorsiiRN(tpbkdf2_test_vectorstlenRnR)( R0R2R5RdR[ReRfRER7((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyRgs "cCsddlm}yddl}t}Wntk r?t}nX|jd|k|y#ddlm}|j dk}Wntk rt}nX|jd|k|ddl m }|r|j d |n|j d |dS( s$verify expected backends are presenti(RoNt fastpbkdf2(RnR s hashlib-ssl(Rsbuiltin-from-bytessbuiltin-unpack( R"RoRRCt ImportErrorRDR)R RnRTtpasslib.utils.compatRtassertIn(R0RoRthas_fastpbkdf2Rnthas_hashlib_sslR((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyt test_backendss"      cCs*dddddd}||jt|dd|jt|dd|jt|dd |d d|jt|d d|jt|d d|jt|d d dd|jt|d d |jt|dd|jt|dd|jt|dd|jt|dddS(stest border casesRXR[iRcSst|||||S(N(Rn(RdR[ReRfRE((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyRisReiitxRfiii iRdREtfooNII(R,R*RBR+t OverflowError(R0Ri((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyRls cCsZdddd dd}|jt|ddd|jt|ddd d S( stest keylen==NoneRXR[iRcSst|||||S(N(Rn(RdR[ReRfRE((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyRisREiRi N(R,R)R(R0Ri((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyttest_default_keylensN( R;RTRUtjoinRoRVR R,RRgRRlR(((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyRps                                       (RUt __future__RRtbinasciiRR R#RRRRtpasslib.tests.utilsRRRR R RWR"RnRoRp(((sD/usr/lib/python2.7/site-packages/passlib/tests/test_crypto_digest.pyts  ">