Esempio n. 1
0
def _make_signature(**kwargs):
    signarr = [kwargs['method'], kwargs['uri'], kwargs['date']]
    if kwargs.get('policy'):
        signarr.append(kwargs['policy'])
    if kwargs.get('content_md5'):
        signarr.append(kwargs['content_md5'])
    signstr = '&'.join(signarr)
    # hashed = hmac.new(b(kwargs['password']), b(signstr), sha1)
    # return 'UPYUN %s:%s' % (kwargs['username'], urlsafe_base64_encode(hashed.digest()))
    signature = base64.b64encode(
        hmac.new(b(kwargs['password']), b(signstr),
                 digestmod=sha1).digest()).decode()
    return 'UPYUN %s:%s' % (kwargs['username'], signature)
Esempio n. 2
0
def _put_file(username, password, service, k, path, expired_time=1800):
    fields = {}
    exp_time = int(time.time()) + expired_time
    fields['expiration'] = exp_time
    fields['save-key'] = k
    fields['service'] = service
    fields['date'] = cur_dt()
    fields.update(kwargs)
    policy = _make_policy(fields)
    signature = _make_signature(username=username,
                                password=(md5(b(password)).hexdigest()),
                                method='POST',
                                uri='/%s/' % service,
                                date=cur_dt(),
                                policy=policy)

    with open(path, 'rb') as input_stream:
        try:
            url = '%s/%s/' % (host, service)
            postdata = {
                'policy': policy,
                'authorization': signature,
                'file':
                (os.path.basename(os.path.basename(path)), input_stream),
            }
            resp = requests.post(url, files=postdata, headers=_headers)
            return resp.json()
        except Exception as e:
            return None
Esempio n. 3
0
    def digest(self):
        # NOTE: backing up state so we can restore it after _process is called,
        #       in case object is updated again (this is only attr altered by this method)
        orig = list(self._state)

        # final block: buf + 0x80,
        # then 0x00 padding until congruent w/ 56 mod 64 bytes
        # then last 8 bytes = msg length in bits
        buf = self._buf
        msglen = self._count*512 + len(buf)*8
        block = buf + b('\x80') + b('\x00') * ((119-len(buf)) % 64) + \
            struct.pack("<2I", msglen & MASK_32, (msglen>>32) & MASK_32)
        if len(block) == 128:
            self._process(block[:64])
            self._process(block[64:])
        else:
            assert len(block) == 64
            self._process(block)

        # render digest & restore un-finalized state
        out = struct.pack("<4I", *self._state)
        self._state = orig
        return out
Esempio n. 4
0
    def digest(self):
        # NOTE: backing up state so we can restore it after _process is called,
        #       in case object is updated again (this is only attr altered by this method)
        orig = list(self._state)

        # final block: buf + 0x80,
        # then 0x00 padding until congruent w/ 56 mod 64 bytes
        # then last 8 bytes = msg length in bits
        buf = self._buf
        msglen = self._count*512 + len(buf)*8
        block = buf + b('\x80') + b('\x00') * ((119-len(buf)) % 64) + \
            struct.pack("<2I", msglen & MASK_32, (msglen>>32) & MASK_32)
        if len(block) == 128:
            self._process(block[:64])
            self._process(block[64:])
        else:
            assert len(block) == 64
            self._process(block)

        # render digest & restore un-finalized state
        out = struct.pack("<4I", *self._state)
        self._state = orig
        return out
Esempio n. 5
0
 def __init__(self, content=None):
     self._count = 0
     self._state = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]
     self._buf = b('')
     if content:
         self.update(content)
Esempio n. 6
0
 def md4(content=None):
     "wrapper for hashlib.new('md4')"
     return hashlib.new('md4', content or b(''))
Esempio n. 7
0
def urlsafe_base64_encode(data):
    ret = base64.urlsafe_b64encode(b(data))
    return s(ret)
Esempio n. 8
0
def __token(data, sk):
    data = b(data)
    hashed = hmac.new(sk, data, sha1)
    return urlsafe_base64_encode(hashed.digest())
Esempio n. 9
0
 def __init__(self, content=None):
     self._count = 0
     self._state = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]
     self._buf = b('')
     if content:
         self.update(content)
Esempio n. 10
0
 def md4(content=None):
     "wrapper for hashlib.new('md4')"
     return hashlib.new('md4', content or b(''))