Best Python code snippet using refurb_python
rsa_private_key.py
Source:rsa_private_key.py
1from samson.encoding.openssh.core.packed_bytes import PackedBytes2from samson.encoding.openssh.core.literal import Literal3from samson.encoding.openssh.general import check_decrypt4from samson.utilities.bytes import Bytes5from samson.padding.incremental_padding import IncrementalPadding6from types import FunctionType7class RSAPrivateKey(object):8 """9 OpenSSH encoding for an RSA private key.10 """11 def __init__(self, name: str, check_bytes: bytes=None, n: int=None, e: int=None, d: int=None, q_mod_p: int=None, p: int=None, q: int=None, host: bytes=None):12 """13 Parameters:14 name (str): Name for bookkeeping purposes.15 check_bytes (bytes): Four random bytes repeated for OpenSSH to check if the decryption worked.16 n (int): RSA modulus.17 e (int): RSA public exponent.18 q_mod_p (int): RSA q^{-1} mod p.19 p (int): RSA secret prime.20 q (int): RSA secret prime.21 host (bytes): Host the key was generated on.22 """23 self.name = name24 self.check_bytes = check_bytes or Bytes.random(4) * 225 self.n = n26 self.e = e27 self.d = d28 self.q_mod_p = q_mod_p29 self.p = p30 self.q = q31 self.host = host32 def __repr__(self):33 return f"<RSAPrivateKey: name={self.name}, n={self.n}, e={self.e}, d={self.d}, q_mod_p={self.q_mod_p}, p={self.p}, q={self.q}, host={self.host}>"34 def __str__(self):35 return self.__repr__()36 @staticmethod37 def pack(value: 'RSAPrivateKey', encryptor: FunctionType=None, padding_size: int=8) -> Bytes:38 """39 Packs a private key into an OpenSSH-compliant encoding.40 Parameters:41 value (RSAPrivateKey): Value to encode.42 encryptor (func): (Optional) Function to use as the encryptor.43 padding_size (int): The block size to pad to. Usually 8 unless you're encrypting.44 Returns:45 Bytes: Packed bytes.46 """47 check_bytes = Literal('check_bytes', length=8).pack(value.check_bytes)48 header = PackedBytes('rsa-header').pack(b'ssh-rsa')49 n = PackedBytes('n').pack(value.n)50 e = PackedBytes('e').pack(value.e)51 d = PackedBytes('d').pack(value.d)52 q_mod_p = PackedBytes('q_mod_p').pack(value.q_mod_p)53 p = PackedBytes('p').pack(value.p)54 q = PackedBytes('q').pack(value.q)55 host = PackedBytes('host').pack(value.host)56 padder = IncrementalPadding(padding_size)57 body = padder.pad(check_bytes + header + n + e + d + q_mod_p + p + q + host)58 if encryptor:59 body = encryptor(body)60 return PackedBytes('private_key').pack(body)61 @staticmethod62 def unpack(encoded_bytes: bytes, decryptor: FunctionType=None) -> ('RSAPrivateKey', bytes):63 """64 Unpacks bytes into an RSAPrivateKey object.65 Parameters:66 encoded_bytes (bytes): Bytes to be (partially?) decoded.67 decryptor (func): (Optional) Function to use as the decryptor.68 Returns:69 (RSAPrivateKey, bytes): The decoded object and unused bytes.70 """71 params, encoded_bytes = PackedBytes('private_key').unpack(encoded_bytes)72 check_bytes, params = check_decrypt(params, decryptor)73 _header, params = PackedBytes('rsa-header').unpack(params)74 n, params = PackedBytes('n').unpack(params)75 e, params = PackedBytes('e').unpack(params)76 d, params = PackedBytes('d').unpack(params)77 q_mod_p, params = PackedBytes('q_mod_p').unpack(params)78 p, params = PackedBytes('p').unpack(params)79 q, params = PackedBytes('q').unpack(params)80 host, params = PackedBytes('host').unpack(params)...
dsa_private_key.py
Source:dsa_private_key.py
1from samson.encoding.openssh.core.packed_bytes import PackedBytes2from samson.encoding.openssh.core.literal import Literal3from samson.encoding.openssh.general import check_decrypt4from samson.padding.incremental_padding import IncrementalPadding5from samson.utilities.bytes import Bytes6from types import FunctionType7class DSAPrivateKey(object):8 """9 OpenSSH encoding for an DSA private key.10 """11 def __init__(self, name: str, check_bytes: bytes=None, p: int=None, q: int=None, g: int=None, y: int=None, x: int=None, host: bytes=None):12 """13 Parameters:14 name (str): Name for bookkeeping purposes.15 check_bytes (bytes): Four random bytes repeated for OpenSSH to check if the decryption worked.16 p (int): Prime modulus.17 q (int): Prime modulus.18 g (int): Generator.19 y (int): Public key.20 x (int): Private key.21 host (bytes): Host the key was generated on.22 """23 self.name = name24 self.check_bytes = check_bytes or Bytes.random(4) * 225 self.p = p26 self.q = q27 self.g = g28 self.y = y29 self.x = x30 self.host = host31 def __repr__(self):32 return f"<DSAPrivateKey: name={self.name}, p={self.p}, q={self.q}, g={self.g}, y={self.y}, x={self.x}, host={self.host}>"33 def __str__(self):34 return self.__repr__()35 @staticmethod36 def pack(value: bytes, encryptor: FunctionType=None, padding_size: int=8) -> Bytes:37 """38 Packs a private key into an OpenSSH-compliant encoding.39 Parameters:40 value (bytes): Value to encode.41 encryptor (func): (Optional) Function to use as the encryptor.42 padding_size (int): The block size to pad to. Usually 8 unless you're encrypting.43 44 Returns:45 Bytes: Packed bytes.46 """47 check_bytes = Literal('check_bytes', length=8).pack(value.check_bytes)48 encoded = check_bytes + PackedBytes('dsa-header').pack(b'ssh-dss') + PackedBytes('p').pack(value.p) + PackedBytes('q').pack(value.q) + PackedBytes('g').pack(value.g) + PackedBytes('y').pack(value.y) + PackedBytes('x').pack(value.x) + PackedBytes('host').pack(value.host)49 padder = IncrementalPadding(padding_size)50 body = padder.pad(encoded)51 if encryptor:52 body = encryptor(body)53 return PackedBytes('private_key').pack(body)54 @staticmethod55 def unpack(encoded_bytes: bytes, decryptor: FunctionType=None, already_unpacked: bool=False) -> (object, bytes):56 """57 Unpacks bytes into an DSAPrivateKey object.58 Parameters:59 encoded_bytes (bytes): Bytes to be (partially?) decoded.60 already_unpacked (bool): Whether or not to do the initial length-decoding.61 Returns:62 (DSAPrivateKey, bytes): The decoded object and unused bytes.63 """64 encoded_bytes = Bytes.wrap(encoded_bytes)65 if already_unpacked:66 params, encoded_bytes = encoded_bytes, None67 else:68 params, encoded_bytes = PackedBytes('private_key').unpack(encoded_bytes)69 check_bytes, params = check_decrypt(params, decryptor)70 _header, params = PackedBytes('dsa-header').unpack(params)71 p, params = PackedBytes('p').unpack(params)72 q, params = PackedBytes('q').unpack(params)73 g, params = PackedBytes('g').unpack(params)74 y, params = PackedBytes('y').unpack(params)75 x, params = PackedBytes('x').unpack(params)76 host, params = PackedBytes('host').unpack(params)...
eddsa_private_key.py
Source:eddsa_private_key.py
1from samson.encoding.openssh.core.packed_bytes import PackedBytes2from samson.utilities.bytes import Bytes3from samson.encoding.openssh.general import check_decrypt4from samson.encoding.openssh.core.literal import Literal5from samson.padding.incremental_padding import IncrementalPadding6from types import FunctionType7class EdDSAPrivateKey(object):8 """9 OpenSSH encoding for an EdDSA private key.10 """11 def __init__(self, name: str, check_bytes: bytes=None, a: int=None, h: int=None, host: bytes=None):12 """13 Parameters:14 name (str): Name for bookkeeping purposes.15 check_bytes (bytes): Four random bytes repeated for OpenSSH to check if the decryption worked.16 a (int): Public int.17 h (int): Hashed private int.18 host (bytes): Host the key was generated on.19 """20 self.name = name21 self.check_bytes = check_bytes or Bytes.random(4) * 222 self.a = a23 self.h = h24 self.host = host25 def __repr__(self):26 return f"<EdDSAPrivateKey: name={self.name}, a={self.a}, h={self.h}, host={self.host}>"27 def __str__(self):28 return self.__repr__()29 @staticmethod30 def pack(value: 'EdDSAPrivateKey', encryptor: FunctionType=None, padding_size: int=8) -> Bytes:31 """32 Packs a private key into an OpenSSH-compliant encoding.33 Parameters:34 value (EdDSAPrivateKey): Value to encode.35 encryptor (func): (Optional) Function to use as the encryptor.36 padding_size (int): The block size to pad to. Usually 8 unless you're encrypting.37 Returns:38 Bytes: Packed bytes.39 """40 check_bytes = Literal('check_bytes', length=8).pack(value.check_bytes)41 encoded = check_bytes + PackedBytes('eddsa-header').pack(b'ssh-ed25519') + PackedBytes('a').pack(value.a) + PackedBytes('h').pack(value.h[::-1]) + PackedBytes('host').pack(value.host)42 padder = IncrementalPadding(padding_size)43 body = padder.pad(encoded)44 if encryptor:45 body = encryptor(body)46 body = PackedBytes('private_key').pack(body)47 return body48 @staticmethod49 def unpack(encoded_bytes: bytes, decryptor: FunctionType=None, already_unpacked: bool=False) -> ('EdDSAPrivateKey', bytes):50 """51 Unpacks bytes into an EdDSAPrivateKey object.52 Parameters:53 encoded_bytes (bytes): Bytes to be (partially?) decoded.54 already_unpacked (bool): Whether or not to do the initial length-decoding.55 Returns:56 (EdDSAPrivateKey, bytes): The decoded object and unused bytes.57 """58 encoded_bytes = Bytes.wrap(encoded_bytes)59 if already_unpacked:60 params, encoded_bytes = encoded_bytes, None61 else:62 params, encoded_bytes = PackedBytes('private_key').unpack(encoded_bytes)63 check_bytes, params = check_decrypt(params, decryptor)64 _header, params = PackedBytes('eddsa-header').unpack(params)65 a, params = PackedBytes('a').unpack(params)66 h, params = PackedBytes('h').unpack(params)67 host, params = PackedBytes('host').unpack(params)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!