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


Python HOTP.generate方法代码示例

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


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

示例1: test_generate

# 需要导入模块: from cryptography.hazmat.primitives.twofactor.hotp import HOTP [as 别名]
# 或者: from cryptography.hazmat.primitives.twofactor.hotp.HOTP import generate [as 别名]
    def test_generate(self, backend, params):
        secret = params["secret"]
        counter = int(params["counter"])
        hotp_value = params["hotp"]

        hotp = HOTP(secret, 6, SHA1(), backend)

        assert hotp.generate(counter) == hotp_value
开发者ID:B-Rich,项目名称:cryptography,代码行数:10,代码来源:test_hotp.py

示例2: send_aic

# 需要导入模块: from cryptography.hazmat.primitives.twofactor.hotp import HOTP [as 别名]
# 或者: from cryptography.hazmat.primitives.twofactor.hotp.HOTP import generate [as 别名]
def send_aic(sessionid, role, bindkey, pubkey):
	"""
	Send an AIC to the ags, returns success value

	sessionid -- Anonymized Session ID for user
	role -- Role as retrieved from database
	bindkey -- As generated during login
	pubkey -- Users stored public key
	context -- Users login context
	"""
	# AIC construct
	aic = {"sessionid":sessionid, "role":role, "bindkey":bindkey, "pubkey":pubkey}
	message = json.dumps(aic)
	logger.info("Genrated AIC: %s", message)
	# Secure the payload
	payload = transport_security.construct_message(message, srcprivkey=app_privatekey, destpubkey=ags_publickey)
	# Calculate the current HOTP token from file
	with open(ags_secretfile, 'r') as f:
		agsdict = json.loads(f.read())
		counter = agsdict['Counter']
		tfapass = agsdict['2FAKey']
	hotp = HOTP(bytes(tfapass, 'utf-8'), 6, hashes.SHA1(), backend=default_backend())
	tfaval = hotp.generate(counter).decode('utf-8')
	# Do the reques to the AGS
	#XXX: Change to https for deployment
	url = "http://{}:{}/ags/authorized?token={}".format(ags_address, ags_port, tfaval)
	logger.info("Adding AIC to AGS: %s", url)
	try:
		resp = requests.put(url, json=payload)
	except requests.Timeout:
		logger.warning("AGS connection timeout")
		return None
	except requests.ConnectionError:
		logger.warning("AGS connection error")
		return None
	logger.info("AGS AIC PUT status code: %s", resp.status_code)
	# A 200 OK response means the request was successful
	if resp.status_code == 200:
		# Update the HOTP counter value
		counter += 1
		agsdict['Counter'] = counter
		with open(ags_secretfile, 'w') as f:
			f.write(json.dumps(agsdict))
		logger.info("AGS HOTP counter was incremented to %s", counter)
		# Return the validity as returned by the AGS
		return resp.json()['validity']
	else:
		logger.warning("AGS rejected AIC")
		return None
开发者ID:DroidX86,项目名称:CloudSec,代码行数:51,代码来源:auth.py

示例3: retrieve_keys

# 需要导入模块: from cryptography.hazmat.primitives.twofactor.hotp import HOTP [as 别名]
# 或者: from cryptography.hazmat.primitives.twofactor.hotp.HOTP import generate [as 别名]
def retrieve_keys(db, attribute):
	"""
	Retrieve keys from the KS; This involves querying the KS with an authentication token
	Then decrypting and returning the encrypted payload

	db -- Databse whose keys to retrieve
	attribute -- The attribute whose encryption key to retrieve
	"""
	# Load the HOTP secret and counter from the secret file of the Key Server
	with open(ks_secretfile, 'r') as f:
		ksdict = json.loads(f.read())
		counter = ksdict['Counter']
		tfapass = ksdict['2FAKey']
	# Calculate the HOTP token value
	hotp = HOTP(bytes(tfapass, 'utf-8'), 6, hashes.SHA1(), backend=default_backend())
	tfaval = hotp.generate(counter).decode('utf-8')
	# Do the request to the key server
	#XXX: Chnage to https for deployment
	url = "http://{}:{}/key/{}/{}?token={}".format(ks_address, ks_port, db, attribute, tfaval)
	logger.info("Trying to access key from KS: %s", url)
	# Do the request
	try:
		resp = requests.get(url)
	except requests.exceptions.Timeout:
		logger.warning("KS request timeout")
		return None
	except requests.exceptions.ConnectionError:
		logger.warning("KS connection refused")
		return None
	logger.info("KS response code: %s", resp.status_code)
	if resp.status_code == 200:	# Got 200 means KS incremented the counter
		counter += 1	# Increment our own counter
		# Save the updated secret
		ksdict['Counter'] = counter
		with open(ks_secretfile, 'w') as f:
			f.write(json.dumps(ksdict))
		logger.info("KS HOTP counter was incremented to %s", counter)
		# Get the transport secure message
		secured_message = resp.json()
		key = transport_security.deconstruct_message(secured_message, destprivkey=app_privatekey, srcpubkey=ks_publickey)
		# Check if decryption was successful
		if not key:
			logger.warning("Secured message from KS was not decrypted properly")
			return None
		return key
	else:
		# KS did not return the key
		logger.warning("KS replied negatively. Something is wrong.")
		return None
开发者ID:DroidX86,项目名称:CloudSec,代码行数:51,代码来源:ags.py

示例4: TOTP

# 需要导入模块: from cryptography.hazmat.primitives.twofactor.hotp import HOTP [as 别名]
# 或者: from cryptography.hazmat.primitives.twofactor.hotp.HOTP import generate [as 别名]
class TOTP(object):
    def __init__(self, key, length, algorithm, time_step, backend):
        if not isinstance(backend, HMACBackend):
            raise UnsupportedInterface("Backend object does not implement HMACBackend")

        self._time_step = time_step
        self._hotp = HOTP(key, length, algorithm, backend)

    def generate(self, time):
        counter = int(time / self._time_step)
        return self._hotp.generate(counter)

    def verify(self, totp, time):
        if not constant_time.bytes_eq(self.generate(time), totp):
            raise InvalidToken("Supplied TOTP value does not match")
开发者ID:jgiannuzzi,项目名称:cryptography,代码行数:17,代码来源:totp.py

示例5: TOTP

# 需要导入模块: from cryptography.hazmat.primitives.twofactor.hotp import HOTP [as 别名]
# 或者: from cryptography.hazmat.primitives.twofactor.hotp.HOTP import generate [as 别名]
class TOTP(object):
    def __init__(self, key, length, algorithm, time_step, backend, enforce_key_length=True):
        if not isinstance(backend, HMACBackend):
            raise UnsupportedAlgorithm(
                "Backend object does not implement HMACBackend.", _Reasons.BACKEND_MISSING_INTERFACE
            )

        self._time_step = time_step
        self._hotp = HOTP(key, length, algorithm, backend, enforce_key_length)

    def generate(self, time):
        counter = int(time / self._time_step)
        return self._hotp.generate(counter)

    def verify(self, totp, time):
        if not constant_time.bytes_eq(self.generate(time), totp):
            raise InvalidToken("Supplied TOTP value does not match.")

    def get_provisioning_uri(self, account_name, issuer):
        return _generate_uri(self._hotp, "totp", account_name, issuer, [("period", int(self._time_step))])
开发者ID:CHENSHUFANG,项目名称:cryptography,代码行数:22,代码来源:totp.py

示例6: test_buffer_protocol

# 需要导入模块: from cryptography.hazmat.primitives.twofactor.hotp import HOTP [as 别名]
# 或者: from cryptography.hazmat.primitives.twofactor.hotp.HOTP import generate [as 别名]
 def test_buffer_protocol(self, backend):
     key = bytearray(b"a long key with lots of entropy goes here")
     hotp = HOTP(key, 6, SHA1(), backend)
     assert hotp.generate(10) == b"559978"
开发者ID:pyca,项目名称:cryptography,代码行数:6,代码来源:test_hotp.py


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