=OXc@sdZddlmZddlZddlZddlZddlZddlZddlZddl m Z m Z m Z m Z ddlmZmZmZmZmZddlmZedZere ndZere nd Zed Zd d Zd ZdZdZdZ dZ!dZ"dZ#dZ$dZ%ia&da'ej(a)dZ*e dZ+de,fdYZ-e-Z.dZ/dZ0dZ1de,fdYZ2dZ3dS( s0 Useful functions used by the rest of paramiko. i(t generatorsN(tDEBUGt zero_bytet xfffffffftmax_byte(tPY2tlongtbyte_chrtbyte_ordtb(t SSHConfigc Cstd}d}| rJt|dkrJt|ddkrJd}nt|drt}|rot}n|dt|d|}nxItdt|dD]/}|d>tjd|||d!d}qW|r|tddt|>8}n|S(sPturns a normalized byte string into a long-int (adapted from Crypto.Util.number)iiiii s>Ii(RtlenRRRtrangetstructtunpack(tstalways_positivetouttnegativetfillerti((s1/usr/lib/python2.7/site-packages/paramiko/util.pyt inflate_long%s /  -!iicCsIt}t|}x@|dkrW|dkrWtjd|t@|}|dL}qWxut|D]F}|dkr|dtkrPn|dkre|dtkrePqeqeWd}|dkrt}nt }||d}|rE|dkrt |ddkrt|}n|dkrEt |ddkrEt |}qEn|S(sPturns a long-int into a normalized byte string (adapted from Crypto.Util.number)iis>Ii ii(i( tbytesRR tpackRt enumeratet deflate_zerot deflate_ffRRR(tntadd_sign_paddingRR((s1/usr/lib/python2.7/site-packages/paramiko/util.pyt deflate_long<s*    " "tcCsd}g}xBt||dkrP|jt|||d!|d7}qW|t|kr}|jt||ng|D]}||^qS(Nii(R tappendtformat_binary_line(tdatatprefixtxR((s1/usr/lib/python2.7/site-packages/paramiko/util.pyt format_binaryZscCsrdjg|D]}dt|^q }djg|D]"}d|t|dd^q9}d||fS(Nt s%02XRs.%c..i?i_s%-50s %s(tjoinR(R!tctlefttright((s1/usr/lib/python2.7/site-packages/paramiko/util.pyR es,8cCsptd}x]|D]U}t|}d|ko<dknrT|t|7}q|td|7}qW|S(NRi is%%%02X(R RR(RRR'R((s1/usr/lib/python2.7/site-packages/paramiko/util.pyt safe_stringks   cCsy|jSWnvtk rt|t}t|d}|dkrMdSt|d}x"|d@s|dK}|d8}q`W|SXdS(Niiii(t bit_lengthtAttributeErrorRtFalseRR (Rtnormthbytetbitlen((s1/usr/lib/python2.7/site-packages/paramiko/util.pyR+vs    cCs%djtjtjjdS(NRs (R&t tracebacktformat_exceptiontsystexc_infotsplit(((s1/usr/lib/python2.7/site-packages/paramiko/util.pyt tb_stringsscCst}t}t|dkr1|d }nx|dkr|}t|dkrk|j|n|jt||j||j}t|t|}||| 7}||8}q4W|S(s Given a password, passphrase, or other human-source key, scramble it through a secure hash into some keyworthy bytes. This specific algorithm is used for encrypting/decrypting private key files. :param function hash_alg: A function which creates a new hash object, such as ``hashlib.sha256``. :param salt: data to salt the hash with. :type salt: byte string :param str key: human-entered password or passphrase. :param int nbytes: number of bytes to generate. :return: Key data `str` ii(RR tupdateR tdigesttmin(thash_algtsalttkeytnbytestkeydataR8thash_objtsize((s1/usr/lib/python2.7/site-packages/paramiko/util.pytgenerate_key_bytess      cCsddlm}||S(s Read a file of known SSH host keys, in the format used by openssh, and return a compound dict of ``hostname -> keytype ->`` `PKey `. The hostname may be an IP address or DNS name. The keytype will be either ``"ssh-rsa"`` or ``"ssh-dss"``. This type of file unfortunately doesn't exist on Windows, but on posix, it will usually be stored in ``os.path.expanduser("~/.ssh/known_hosts")``. Since 1.5.3, this is just a wrapper around `.HostKeys`. :param str filename: name of the file to read host keys from :return: nested dict of `.PKey` objects, indexed by hostname and then keytype i(tHostKeys(tparamiko.hostkeysRB(tfilenameRB((s1/usr/lib/python2.7/site-packages/paramiko/util.pytload_host_keysscCst}|j||S(sM Provided only as a backward-compatible wrapper around `.SSHConfig`. (R tparse(tfile_objtconfig((s1/usr/lib/python2.7/site-packages/paramiko/util.pytparse_ssh_configs  cCs |j|S(sM Provided only as a backward-compatible wrapper around `.SSHConfig`. (tlookup(thostnameRH((s1/usr/lib/python2.7/site-packages/paramiko/util.pytlookup_ssh_host_configsc Csdd|}}}dd|}}}xY|dkr||}||||}}||||}}||||}}q+W|dkr||7}n|S(Nii(( R#tmtu1tu2tu3tv1tv2tv3tq((s1/usr/lib/python2.7/site-packages/paramiko/util.pyt mod_inverses   cCskttj}y t|SWnFtk rftjztd7at}t|sH      "(