示例#1
0
def compute_hash(fp, buf_size=8192, size=None, hash_algorithm=md5):
    hash_obj = hash_algorithm()
    spos = fp.tell()
    if size and size < buf_size:
        s = fp.read(size)
    else:
        s = fp.read(buf_size)
    while s:
        if not isinstance(s, bytes):
            s = s.encode('utf-8')
        hash_obj.update(s)
        if size:
            size -= len(s)
            if size <= 0:
                break
        if size and size < buf_size:
            s = fp.read(size)
        else:
            s = fp.read(buf_size)
    hex_digest = hash_obj.hexdigest()
    base64_digest = encodebytes(hash_obj.digest()).decode('utf-8')
    if base64_digest[-1] == '\n':
        base64_digest = base64_digest[0:-1]
    # data_size based on bytes read.
    data_size = fp.tell() - spos
    fp.seek(spos)
    return (hex_digest, base64_digest, data_size)
示例#2
0
def compute_hash(fp, buf_size=8192, size=None, hash_algorithm=md5):
    hash_obj = hash_algorithm()
    spos = fp.tell()
    if size and size < buf_size:
        s = fp.read(size)
    else:
        s = fp.read(buf_size)
    while s:
        if not isinstance(s, bytes):
            s = s.encode('utf-8')
        hash_obj.update(s)
        if size:
            size -= len(s)
            if size <= 0:
                break
        if size and size < buf_size:
            s = fp.read(size)
        else:
            s = fp.read(buf_size)
    hex_digest = hash_obj.hexdigest()
    base64_digest = encodebytes(hash_obj.digest()).decode('utf-8')
    if base64_digest[-1] == '\n':
        base64_digest = base64_digest[0:-1]
    # data_size based on bytes read.
    data_size = fp.tell() - spos
    fp.seek(spos)
    return (hex_digest, base64_digest, data_size)
示例#3
0
 def get_proxy_auth_header(self):
     auth = encodebytes(self.proxy_user + ":" + self.proxy_pass)
     return {"Proxy-Authorization": "Basic %s" % auth}
示例#4
0
 def sign_string(self, string_to_sign):
     new_hmac = self._get_hmac()
     new_hmac.update(string_to_sign.encode('utf-8'))
     return encodebytes(new_hmac.digest()).decode('utf-8').strip()
示例#5
0
 def sign_string(self, string_to_sign):
     new_hmac = self._get_hmac()
     new_hmac.update(string_to_sign.encode('utf-8'))
     return encodebytes(new_hmac.digest()).decode('utf-8').strip()
示例#6
0
 def get_proxy_auth_header(self):
     auth = encodebytes(self.proxy_user + ':' + self.proxy_pass)
     return {'Proxy-Authorization': 'Basic %s' % auth}