def validate_key(self, key, user_id): # Validate the activation key expected_mac = hmac.new(self.secret_key, f"{user_id}{key}".encode('utf-8'), hashlib.sha256).hexdigest() # Compare the expected MAC with the provided MAC return hmac.compare_digest(expected_mac, key)
is_valid = activation_key.validate_key(generated_key, user_id) is_expired = activation_key.is_expired(generated_key, expiration_date)
def main(): secret_key = "my_secret_key" expiration_days = 30
user_id = "user123" expiration_date = datetime.now() + timedelta(days=expiration_days)