Loading...
More Python Posts
from time import sleepdef delay(fn, ms, *args):sleep(ms / 1000)return fn(*args)delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second
from statistics import median, mean, modedef print_stats(array):print(array)print("median =", median(array))print("mean =", mean(array))print("mode =", mode(array))print()print_stats([1, 2, 3, 3, 4])print_stats([1, 2, 3, 3])
def parse_ike_proposal(proposal):"""Parse an IKE proposal string to extract encryption, hash, PRF, and DH group in human-readable format.Args:proposal (str): IKE proposal string, e.g., 'IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/MODP_1024'Returns:dict: Dictionary with encryption, hash, PRF, and DH group in human-readable format"""dh_mapping = {'MODP_1024': 'Group 2','MODP_2048': 'Group 14','MODP_3072': 'Group 15','MODP_4096': 'Group 16','MODP_6144': 'Group 17','MODP_8192': 'Group 18','ECP_256': 'Group 19','ECP_384': 'Group 20','ECP_521': 'Group 21','MODP_1024_160': 'Group 22','MODP_2048_224': 'Group 23','MODP_2048_256': 'Group 24'}enc_mapping = {'AES_CBC_128': 'AES-128','AES_CBC_256': 'AES-256'}hash_mapping = {'HMAC_SHA1_96': 'SHA1','HMAC_SHA2_256_128': 'SHA2-256','HMAC_SHA2_384_192': 'SHA2-384','HMAC_SHA2_512_256': 'SHA2-512'}prf_mapping = {'PRF_HMAC_SHA1': 'SHA1','PRF_HMAC_SHA2_256': 'SHA2-256','PRF_HMAC_SHA2_384': 'SHA2-384','PRF_HMAC_SHA2_512': 'SHA2-512'}# Split the proposal into componentscomponents = proposal.split('/')# Initialize result dictionaryresult = {'encryption': 'Unknown','hash': 'Unknown','prf': 'Unknown','dh_group': 'Unknown'}# Extract components based on expected lengthif len(components) >= 4:result['encryption'] = enc_mapping.get(components[0].replace('IKE:', ''), 'Unknown')result['hash'] = hash_mapping.get(components[1], 'Unknown')result['prf'] = prf_mapping.get(components[2], 'Unknown')result['dh_group'] = dh_mapping.get(components[3], 'Unknown')elif len(components) == 3: # Handle cases without DH groupresult['encryption'] = enc_mapping.get(components[0].replace('IKE:', ''), 'Unknown')result['hash'] = hash_mapping.get(components[1], 'Unknown')result['prf'] = prf_mapping.get(components[2], 'Unknown')return resultdef process_proposals(proposal_list):"""Process a list of IKE proposals and convert each to its human-readable components.Args:proposal_list (str): Comma-separated string of IKE proposalsReturns:dict: Dictionary mapping each proposal to its parsed components"""proposals = proposal_list.split(', ')result = {}for proposal in proposals:parsed = parse_ike_proposal(proposal.strip())result[proposal] = parsedreturn result# Example usageif __name__ == "__main__":proposals = 'IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/MODP_1024, IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/MODP_2048, IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/MODP_3072, IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/MODP_4096, IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/MODP_6144, IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/MODP_8192, IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/ECP_256, IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/ECP_384, IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/ECP_521, IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/MODP_1024_160, IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/MODP_2048_224, IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/MODP_2048_256, IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_1024, IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_2048, IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_3072, IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_4096, IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_6144, IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_8192, IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/ECP_256, IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/ECP_384, IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/ECP_521, IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_1024_160, IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_2048_224, IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_2048_256, IKE:AES_CBC_128/HMAC_SHA2_384_192/PRF_HMAC_SHA2_384/MODP_1024, IKE:AES_CBC_128/HMAC_SHA2_384_192/PRF_HMAC_SHA2_384/MODP_2048, IKE:AES_CBC_128/HMAC_SHA2_384_192/PRF_HMAC_SHA2_384/MODP_3072, IKE:AES_CBC_128/HMAC_SHA2_384_192/PRF_HMAC_SHA2_384/MODP_4096, IKE:AES_CBC_128/HMAC_SHA2_384_192/PRF_HMAC_SHA2_384/MODP_6144, IKE:AES_CBC_128/HMAC_SHA2_384_192/PRF_HMAC_SHA2_384/MODP_8192, IKE:AES_CBC_128/HMAC_SHA2_384_192/PRF_HMAC_SHA2_384/ECP_256, IKE:AES_CBC_128/HMAC_SHA2_384_192/PRF_HMAC_SHA2_384/ECP_384, IKE:AES_CBC_128/HMAC_SHA2_384_192/PRF_HMAC_SHA2_384/ECP_521, IKE:AES_CBC_128/HMAC_SHA2_384_192/PRF_HMAC_SHA2_384/MODP_1024_160, IKE:AES_CBC_128/HMAC_SHA2_384_192/PRF_HMAC_SHA2_384/MODP_2048_224, IKE:AES_CBC_128/HMAC_SHA2_384_192/PRF_HMAC_SHA2_384/MODP_2048_256, IKE:AES_CBC_128/HMAC_SHA2_512_256/PRF_HMAC_SHA2_512/MODP_1024, IKE:AES_CBC_128/HMAC_SHA2_512_256/PRF_HMAC_SHA2_512/MODP_2048, IKE:AES_CBC_128/HMAC_SHA2_512_256/PRF_HMAC_SHA2_512/MODP_3072, IKE:AES_CBC_128/HMAC_SHA2_512_256/PRF_HMAC_SHA2_512/MODP_4096, IKE:AES_CBC_128/HMAC_SHA2_512_256/PRF_HMAC_SHA2_512/MODP_6144, IKE:AES_CBC_128/HMAC_SHA2_512_256/PRF_HMAC_SHA2_512/MODP_8192, IKE:AES_CBC_128/HMAC_SHA2_512_256/PRF_HMAC_SHA2_512/ECP_256'result = process_proposals(proposals)for proposal, parsed in result.items():print(f"Encryption={parsed['encryption']}, Hash={parsed['hash']}, PRF={parsed['prf']}, DH Group={parsed['dh_group']}")
def when(predicate, when_true):return lambda x: when_true(x) if predicate(x) else xdouble_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2)print(double_even_numbers(2)) # 4print(double_even_numbers(1)) # 1
import random# Define the ranks, suits, and create a deckranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']deck = [(rank, suit) for rank in ranks for suit in suits]# Shuffle the deckrandom.shuffle(deck)# Display the shuffled deckfor card in deck:print(card[0], "of", card[1])