Beispiel #1
0
    def generate_oauth_signature(self, method, params, url, key=None):
        """ Generate OAuth Signature """

        string_to_sign = self.get_signature_base_string(method, params, url)

        if key is None:
            key = self.get_sign_key(self.consumer_secret)

        if self.signature_method == 'HMAC-SHA1':
            hmac_mod = sha1
        elif self.signature_method == 'HMAC-SHA256':
            hmac_mod = sha256
        else:
            raise UserWarning("Unknown signature_method")

        # print "\nstring_to_sign: %s" % repr(string_to_sign)
        # print "\nkey: %s" % repr(key)
        sig = HMAC(
            bytes(key.encode('utf-8')),
            bytes(string_to_sign.encode('utf-8')),
            hmac_mod
        )
        sig_b64 = binascii.b2a_base64(sig.digest())[:-1]
        # print "\nsig_b64: %s" % sig_b64
        return sig_b64
Beispiel #2
0
 def current_hmac(self, cached=False):
     """Returns the current hmac of self.fulldata"""
     data = ''
     for i in self.headers:
         log.debug("Adding hmac data %s", repr(i.hmac_data()))
         if cached:
             data += i.data
         else:
             data += i.hmac_data()
     for i in self.records:
         log.debug("Adding hmac data %s", repr(i.hmac_data()))
         data += i.hmac_data()
     log.debug("Building hmac with key %s", repr(self.hshkey))
     hm = HMAC(self.hshkey, data, sha256_mod)
     #print hm.hexdigest()
     log.debug("HMAC %s-%s", repr(hm.hexdigest()), repr(hm.digest()))
     return hm.digest()
Beispiel #3
0
 def current_hmac(self, cached = False):
     """Returns the current hmac of self.fulldata"""
     data = ''
     for i in self.headers:
         log.debug("Adding hmac data %s", repr(i.hmac_data()))
         if cached:
             data += i.data
         else:
             data += i.hmac_data()
     for i in self.records:
         log.debug("Adding hmac data %s", repr(i.hmac_data()))
         data += i.hmac_data()
     log.debug("Building hmac with key %s", repr(self.hshkey))
     hm = HMAC(self.hshkey, data, sha256_mod)
     #print hm.hexdigest()
     log.debug("HMAC %s-%s", repr(hm.hexdigest()), repr(hm.digest()))
     return hm.digest()
Beispiel #4
0
 def current_hmac(self, cached = False):
     """Returns the current hmac of self.fulldata"""
     data = ''
     for i in self.headers:
         log.debug("Adding hmac data %r from %r" % (i.hmac_data(), i.__class__.__name__))
         if cached:
             data += i.data
         else:
             data += i.hmac_data()
             # assert i.data == i.hmac_data(), "Working on %r where %r!=%r" % (i, i.data, i.hmac_data())
     for i in self.records:
         # TODO: Add caching support
         log.debug("Adding hmac data %r from %r" % (i.hmac_data(), i.__class__.__name__))
         data += i.hmac_data()
     log.debug("Building hmac with key %s", repr(self.hshkey))
     hm = HMAC(self.hshkey, data, sha256_mod)
     # print hm.hexdigest()
     log.debug("HMAC %s-%s", repr(hm.hexdigest()), repr(hm.digest()))
     return hm.digest()
Beispiel #5
0
 def current_hmac(self, cached=False):
     """Returns the current hmac of self.fulldata"""
     data = b''
     for i in self.headers:
         log.debug("Adding hmac data %r from %r" %
                   (i.hmac_data(), i.__class__.__name__))
         if cached:
             data += i.data
         else:
             data += i.hmac_data()
             # assert i.data == i.hmac_data(), "Working on %r where %r!=%r" % (i, i.data, i.hmac_data())
     for i in self.records:
         # TODO: Add caching support
         log.debug("Adding hmac data %r from %r" %
                   (i.hmac_data(), i.__class__.__name__))
         data += i.hmac_data()
     log.debug("Building hmac with key %s", repr(self.hshkey))
     hm = HMAC(self.hshkey, data, sha256_mod)
     # print hm.hexdigest()
     log.debug("HMAC %s-%s", repr(hm.hexdigest()), repr(hm.digest()))
     return hm.digest()
Beispiel #6
0
    def __init__(self, secret, digits, x=30, mode=hashlib.sha256):
        self.secret = secret
        self.digits = digits
        self.x = x
        self.mode = mode
        hash_key_len = 0
        if mode == hashlib.sha256:
            hash_key_len = 32
        if mode == hashlib.sha512:
            hash_key_len = 64
        if mode == hashlib.sha1:
            hash_key_len = 20

        secret_len_r = hash_key_len * 2 - len(secret)
        if secret_len_r > 0:
            secret = '0' * secret_len_r + secret
        self.secret = binascii.a2b_hex(secret)
        logger.info(('self.secret : %s len: %d' % (self.secret, len(self.secret))))
        self.mac = Hmac(self.secret, digestmod=self.mode)
Beispiel #7
0
class Topt(object):
    def __init__(self, secret, digits, x=30, mode=hashlib.sha256):
        self.secret = secret
        self.digits = digits
        self.x = x
        self.mode = mode
        hash_key_len = 0
        if mode == hashlib.sha256:
            hash_key_len = 32
        if mode == hashlib.sha512:
            hash_key_len = 64
        if mode == hashlib.sha1:
            hash_key_len = 20

        secret_len_r = hash_key_len * 2 - len(secret)
        if secret_len_r > 0:
            secret = '0' * secret_len_r + secret
        self.secret = binascii.a2b_hex(secret)
        logger.info(('self.secret : %s len: %d' % (self.secret, len(self.secret))))
        self.mac = Hmac(self.secret, digestmod=self.mode)

    def digest(self, msg):
        mac = self.mac.copy()
        mac.update(msg)

        bytes_hash = mac.digest()
        logger.info(('hashed:', bytes_hash, 'len:', len(bytes_hash)))

        return bytes_hash

    def dynamic_truncate(self, hash_bytes):
        offset = hash_bytes[-1] & 0x0f
        dyn_truncated = 0
        for i in range(4):
            dyn_truncated += (hash_bytes[offset + i] & 0xff) << 8 * (4 - 1 - i)
        dyn_truncated &= ~(0x01 << (8 * 4 - 1))

        logger.info(('offset:', offset, 'dyn_truncated:', dyn_truncated))
        return dyn_truncated

    def truncate(self, digest):
        dyn_truncated = self.dynamic_truncate(digest)
        truncated = dyn_truncated % (10 ** self.digits)
        logger.info(('truncated: ', truncated))
        return truncated

    def generateTotp(self, T: int):
        steps = T // self.x
        steps_hex_bytes = steps.to_bytes(8, 'big', signed=False)
        logger.info(('steps_hex_bytes:', steps_hex_bytes))

        hashed = self.digest(steps_hex_bytes)
        opt_int = self.truncate(hashed)
        opt_str = str(opt_int)
        opt_str = '0' * (self.digits - len(opt_str)) + opt_str
        return opt_str

    def generateTotp_now(self):

        import datetime
        utcnow = datetime.datetime.utcnow()
        T = utc = utcnow.timestamp()
        logger.info('utc now: %s  %d' % (utcnow, T,))
        return self.generateTotp(int(T))