当前位置: 首页>>代码示例>>Python>>正文


Python HmacOtp.generate方法代码示例

本文整理汇总了Python中privacyidea.lib.tokens.HMAC.HmacOtp.generate方法的典型用法代码示例。如果您正苦于以下问题:Python HmacOtp.generate方法的具体用法?Python HmacOtp.generate怎么用?Python HmacOtp.generate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在privacyidea.lib.tokens.HMAC.HmacOtp的用法示例。


在下文中一共展示了HmacOtp.generate方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_otp

# 需要导入模块: from privacyidea.lib.tokens.HMAC import HmacOtp [as 别名]
# 或者: from privacyidea.lib.tokens.HMAC.HmacOtp import generate [as 别名]
    def get_otp(self, current_time=None, do_truncation=True, time_seconds=None, challenge=None):
        """
        get the next OTP value

        :param current_time: the current time, for which the OTP value
        should be calculated for.
        :type current_time: datetime object
        :param time_seconds: the current time, for which the OTP value
        should be calculated for (date +%s)
        :type: time_seconds: int, unix system time seconds
        :return: next otp value, and PIN, if possible
        :rtype: tuple
        """
        otplen = int(self.token.otplen)
        secretHOtp = self.token.get_otpkey()

        hmac2Otp = HmacOtp(secretHOtp, self.get_otp_count(), otplen, self.get_hashlib(self.hashlib))

        if time_seconds is None:
            time_seconds = self._time2float(datetime.datetime.now())
        if current_time:
            time_seconds = self._time2float(current_time)

        # we don't need to round here as we have already float
        counter = int(((time_seconds - self.timeshift) / self.timestep))
        otpval = hmac2Otp.generate(counter=counter, inc_counter=False, do_truncation=do_truncation, challenge=challenge)

        pin = self.token.get_pin()
        combined = "%s%s" % (otpval, pin)
        if get_from_config("PrependPin") == "True":
            combined = "%s%s" % (pin, otpval)

        return 1, pin, otpval, combined
开发者ID:ask0n,项目名称:privacyidea,代码行数:35,代码来源:totptoken.py

示例2: get_multi_otp

# 需要导入模块: from privacyidea.lib.tokens.HMAC import HmacOtp [as 别名]
# 或者: from privacyidea.lib.tokens.HMAC.HmacOtp import generate [as 别名]
    def get_multi_otp(self, count=0, epoch_start=0, epoch_end=0,
                      curTime=None, timestamp=None):
        """
        return a dictionary of multiple future OTP values
        of the HOTP/HMAC token

        :param count: how many otp values should be returned
        :type count: int
        :param epoch_start: not implemented
        :param epoch_end: not implemented
        :param curTime: Simulate the servertime
        :type curTime: datetime
        :param timestamp: Simulate the servertime
        :type timestamp: epoch time
        :return: tuple of status: boolean, error: text and the OTP dictionary

        """
        otp_dict = {"type": "TOTP", "otp": {}}
        ret = False
        error = "No count specified"

        otplen = int(self.token.otplen)
        secretHOtp = self.token.get_otpkey()

        hmac2Otp = HmacOtp(secretHOtp, self.get_otp_count(),
                           otplen, self.get_hashlib(self.hashlib))

        if curTime:
            # datetime object provided for simulation
            tCounter = self._time2float(curTime)
        elif timestamp:
            # epoch time provided for simulation
            tCounter = int(timestamp)
        else:
            # use the current server time
            tCounter = self._time2float(datetime.datetime.now())

        # we don't need to round here as we have alread float
        counter = int(((tCounter - self.timeshift) / self.timestep))

        otp_dict["shift"] = self.timeshift
        otp_dict["timeStepping"] = self.timeshift

        if count > 0:
            error = "OK"
            for i in range(0, count):
                otpval = hmac2Otp.generate(counter=counter + i,
                                           inc_counter=False)
                timeCounter = ((counter + i) * self.timestep) + self.timeshift
                
                val_time = datetime.datetime.\
                    fromtimestamp(timeCounter).strftime("%Y-%m-%d %H:%M:%S")
                otp_dict["otp"][counter + i] = {'otpval': otpval,
                                                'time': val_time}
            ret = True
            
        return ret, error, otp_dict
开发者ID:R00tAK,项目名称:privacyidea,代码行数:59,代码来源:totptoken.py

示例3: OCRA

# 需要导入模块: from privacyidea.lib.tokens.HMAC import HmacOtp [as 别名]
# 或者: from privacyidea.lib.tokens.HMAC.HmacOtp import generate [as 别名]

#.........这里部分代码省略.........
        :param pin_hash: The hash of the pin
        :type pin_hash: basestring (hex)
        :param timesteps: timestemps
        :type timesteps: hex string
        :return: data_input
        :rytpe: binary
        """
        # In case the ocrasuite comes as a unicode (like from the webui) we
        # need to convert it!
        data_input = str(self.ocrasuite) + b'\0'
        # Check for counter
        if self.ocrasuite_obj.counter == "C":
            if counter:
                counter = int(counter)
                counter = struct.pack('>Q', int(counter))
                data_input += counter
            else:
                raise Exception("The ocrasuite {0!s} requires a counter".format(
                                self.ocrasuite))
        # Check for Question
        if self.ocrasuite_obj.challenge_type == "QN":
            # In case of QN
            question = '{0:x}'.format(int(question))
            question += '0' * (len(question) % 2)
            question = binascii.unhexlify(question)
            question += '\0' * (128-len(question))
            data_input += question
        elif self.ocrasuite_obj.challenge_type == "QA":
            question += '\0' * (128-len(question))
            data_input += question
        elif self.ocrasuite_obj.challenge_type == "QH":  # pragma: no cover
            question = binascii.unhexlify(question)
            question += '\0' * (128-len(question))
            data_input += question

        # in case of PIN
        if self.ocrasuite_obj.signature_type == "P":
            if pin_hash:
                data_input += binascii.unhexlify(pin_hash)
            elif pin:
                pin_hash = SHA_FUNC.get(self.ocrasuite_obj.signature_hash)(
                    pin).digest()
                data_input += pin_hash
            else:
                raise Exception("The ocrasuite {0!s} requires a PIN!".format(
                                self.ocrasuite))
        elif self.ocrasuite_obj.signature_type == "T":
            if not timesteps:
                raise Exception("The ocrasuite {0!s} requires timesteps".format(
                                self.ocrasuite))
            # In case of Time
            timesteps = int(timesteps, 16)
            timesteps = struct.pack('>Q', int(timesteps))
            data_input += timesteps
        elif self.ocrasuite_obj.signature_type == "S":  # pragma: no cover
            # In case of session
            # TODO: Session not yet implemented
            raise NotImplementedError("OCRA Session not implemented, yet.")
        return data_input

    def get_response(self, question, pin=None, pin_hash=None, counter=None,
                     timesteps=None):
        """
        Create an OTP response from the given input values.

        :param question:
        :param pin:
        :param pin_hash:
        :param counter:
        :return:
        """
        data_input = self.create_data_input(question,
                                            pin=pin,
                                            pin_hash=pin_hash,
                                            counter=counter,
                                            timesteps=timesteps)
        r = self.hmac_obj.generate(key=self.key,
                                   challenge=binascii.hexlify(data_input))
        return r

    def check_response(self, response, question=None, pin=None,
                       pin_hash=None, counter=None, timesteps=None):
        """
        Check the given *response* if it is the correct response to the
        challenge/question.

        :param response:
        :param question:
        :param pin:
        :param pin_hash:
        :param counter:
        :param timesteps:
        :return:
        """
        r = self.get_response(question, pin=pin, pin_hash=pin_hash,
                              counter=counter, timesteps=timesteps)
        if r == response:
            return 1
        else:
            return -1
开发者ID:STRML,项目名称:privacyidea,代码行数:104,代码来源:ocra.py


注:本文中的privacyidea.lib.tokens.HMAC.HmacOtp.generate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。