idx int64 0 63k | question stringlengths 61 4.03k | target stringlengths 6 1.23k |
|---|---|---|
53,900 | def remove_file ( path , remove = os . remove , exists = os . path . exists ) : try : remove ( path ) except OSError : if exists ( path ) : raise | Remove file and raise OSError if still exists . |
53,901 | def unix_domain_socket_server ( sock_path ) : log . debug ( 'serving on %s' , sock_path ) remove_file ( sock_path ) server = socket . socket ( socket . AF_UNIX , socket . SOCK_STREAM ) server . bind ( sock_path ) server . listen ( 1 ) try : yield server finally : remove_file ( sock_path ) | Create UNIX - domain socket on specified path . |
53,902 | def handle_connection ( conn , handler , mutex ) : try : log . debug ( 'welcome agent' ) with contextlib . closing ( conn ) : while True : msg = util . read_frame ( conn ) with mutex : reply = handler . handle ( msg = msg ) util . send ( conn , reply ) except EOFError : log . debug ( 'goodbye agent' ) except Exception ... | Handle a single connection using the specified protocol handler in a loop . |
53,903 | def retry ( func , exception_type , quit_event ) : while True : if quit_event . is_set ( ) : raise StopIteration try : return func ( ) except exception_type : pass | Run the function retrying when the specified exception_type occurs . |
53,904 | def spawn ( func , kwargs ) : t = threading . Thread ( target = func , kwargs = kwargs ) t . start ( ) yield t . join ( ) | Spawn a thread and join it after the context is over . |
53,905 | def run_process ( command , environ ) : log . info ( 'running %r with %r' , command , environ ) env = dict ( os . environ ) env . update ( environ ) try : p = subprocess . Popen ( args = command , env = env ) except OSError as e : raise OSError ( 'cannot run %r: %s' % ( command , e ) ) log . debug ( 'subprocess %d is r... | Run the specified process and wait until it finishes . |
53,906 | def check_output ( args , env = None , sp = subprocess ) : log . debug ( 'calling %s with env %s' , args , env ) output = sp . check_output ( args = args , env = env ) log . debug ( 'output: %r' , output ) return output | Call an external binary and return its stdout . |
53,907 | def get_agent_sock_path ( env = None , sp = subprocess ) : args = [ util . which ( 'gpgconf' ) , '--list-dirs' ] output = check_output ( args = args , env = env , sp = sp ) lines = output . strip ( ) . split ( b'\n' ) dirs = dict ( line . split ( b':' , 1 ) for line in lines ) log . debug ( '%s: %s' , args , dirs ) ret... | Parse gpgconf output to find out GPG agent UNIX socket path . |
53,908 | def connect_to_agent ( env = None , sp = subprocess ) : sock_path = get_agent_sock_path ( sp = sp , env = env ) check_output ( args = [ 'gpg-connect-agent' , '/bye' ] , sp = sp ) sock = socket . socket ( socket . AF_UNIX , socket . SOCK_STREAM ) sock . connect ( sock_path ) return sock | Connect to GPG agent s UNIX socket . |
53,909 | def sendline ( sock , msg , confidential = False ) : log . debug ( '<- %r' , ( '<snip>' if confidential else msg ) ) sock . sendall ( msg + b'\n' ) | Send a binary message followed by EOL . |
53,910 | def recvline ( sock ) : reply = io . BytesIO ( ) while True : c = sock . recv ( 1 ) if not c : return None if c == b'\n' : break reply . write ( c ) result = reply . getvalue ( ) log . debug ( '-> %r' , result ) return result | Receive a single line from the socket . |
53,911 | def parse_term ( s ) : size , s = s . split ( b':' , 1 ) size = int ( size ) return s [ : size ] , s [ size : ] | Parse single s - expr term from bytes . |
53,912 | def parse ( s ) : if s . startswith ( b'(' ) : s = s [ 1 : ] name , s = parse_term ( s ) values = [ name ] while not s . startswith ( b')' ) : value , s = parse ( s ) values . append ( value ) return values , s [ 1 : ] return parse_term ( s ) | Parse full s - expr from bytes . |
53,913 | def parse_sig ( sig ) : label , sig = sig assert label == b'sig-val' algo_name = sig [ 0 ] parser = { b'rsa' : _parse_rsa_sig , b'ecdsa' : _parse_ecdsa_sig , b'eddsa' : _parse_eddsa_sig , b'dsa' : _parse_dsa_sig } [ algo_name ] return parser ( args = sig [ 1 : ] ) | Parse signature integer values from s - expr . |
53,914 | def sign_digest ( sock , keygrip , digest , sp = subprocess , environ = None ) : hash_algo = 8 assert len ( digest ) == 32 assert communicate ( sock , 'RESET' ) . startswith ( b'OK' ) ttyname = check_output ( args = [ 'tty' ] , sp = sp ) . strip ( ) options = [ 'ttyname={}' . format ( ttyname ) ] display = ( environ or... | Sign a digest using specified key using GPG agent . |
53,915 | def get_gnupg_components ( sp = subprocess ) : args = [ util . which ( 'gpgconf' ) , '--list-components' ] output = check_output ( args = args , sp = sp ) components = dict ( re . findall ( '(.*):.*:(.*)' , output . decode ( 'utf-8' ) ) ) log . debug ( 'gpgconf --list-components: %s' , components ) return components | Parse GnuPG components paths . |
53,916 | def gpg_command ( args , env = None ) : if env is None : env = os . environ cmd = get_gnupg_binary ( neopg_binary = env . get ( 'NEOPG_BINARY' ) ) return [ cmd ] + args | Prepare common GPG command line arguments . |
53,917 | def export_public_key ( user_id , env = None , sp = subprocess ) : args = gpg_command ( [ '--export' , user_id ] ) result = check_output ( args = args , env = env , sp = sp ) if not result : log . error ( 'could not find public key %r in local GPG keyring' , user_id ) raise KeyError ( user_id ) return result | Export GPG public key for specified user_id . |
53,918 | def export_public_keys ( env = None , sp = subprocess ) : args = gpg_command ( [ '--export' ] ) result = check_output ( args = args , env = env , sp = sp ) if not result : raise KeyError ( 'No GPG public keys found at env: {!r}' . format ( env ) ) return result | Export all GPG public keys . |
53,919 | def create_agent_signer ( user_id ) : sock = connect_to_agent ( env = os . environ ) keygrip = get_keygrip ( user_id ) def sign ( digest ) : return sign_digest ( sock = sock , keygrip = keygrip , digest = digest ) return sign | Sign digest with existing GPG keys using gpg - agent tool . |
53,920 | def msg_name ( code ) : ids = { v : k for k , v in COMMANDS . items ( ) } return ids [ code ] | Convert integer message code into a string name . |
53,921 | def _legacy_pubs ( buf ) : leftover = buf . read ( ) if leftover : log . warning ( 'skipping leftover: %r' , leftover ) code = util . pack ( 'B' , msg_code ( 'SSH_AGENT_RSA_IDENTITIES_ANSWER' ) ) num = util . pack ( 'L' , 0 ) return util . frame ( code , num ) | SSH v1 public keys are not supported . |
53,922 | def handle ( self , msg ) : debug_msg = ': {!r}' . format ( msg ) if self . debug else '' log . debug ( 'request: %d bytes%s' , len ( msg ) , debug_msg ) buf = io . BytesIO ( msg ) code , = util . recv ( buf , '>B' ) if code not in self . methods : log . warning ( 'Unsupported command: %s (%d)' , msg_name ( code ) , co... | Handle SSH message from the SSH client and return the response . |
53,923 | def list_pubs ( self , buf ) : assert not buf . read ( ) keys = self . conn . parse_public_keys ( ) code = util . pack ( 'B' , msg_code ( 'SSH2_AGENT_IDENTITIES_ANSWER' ) ) num = util . pack ( 'L' , len ( keys ) ) log . debug ( 'available keys: %s' , [ k [ 'name' ] for k in keys ] ) for i , k in enumerate ( keys ) : lo... | SSH v2 public keys are serialized and returned . |
53,924 | def sign_message ( self , buf ) : key = formats . parse_pubkey ( util . read_frame ( buf ) ) log . debug ( 'looking for %s' , key [ 'fingerprint' ] ) blob = util . read_frame ( buf ) assert util . read_frame ( buf ) == b'' assert not buf . read ( ) for k in self . conn . parse_public_keys ( ) : if ( k [ 'fingerprint' ]... | SSH v2 public key authentication is performed . |
53,925 | def recv ( conn , size ) : try : fmt = size size = struct . calcsize ( fmt ) except TypeError : fmt = None try : _read = conn . recv except AttributeError : _read = conn . read res = io . BytesIO ( ) while size > 0 : buf = _read ( size ) if not buf : raise EOFError size = size - len ( buf ) res . write ( buf ) res = re... | Receive bytes from connection socket or stream . |
53,926 | def bytes2num ( s ) : res = 0 for i , c in enumerate ( reversed ( bytearray ( s ) ) ) : res += c << ( i * 8 ) return res | Convert MSB - first bytes to an unsigned integer . |
53,927 | def num2bytes ( value , size ) : res = [ ] for _ in range ( size ) : res . append ( value & 0xFF ) value = value >> 8 assert value == 0 return bytes ( bytearray ( list ( reversed ( res ) ) ) ) | Convert an unsigned integer to MSB - first bytes with specified size . |
53,928 | def frame ( * msgs ) : res = io . BytesIO ( ) for msg in msgs : res . write ( msg ) msg = res . getvalue ( ) return pack ( 'L' , len ( msg ) ) + msg | Serialize MSB - first length - prefixed frame . |
53,929 | def split_bits ( value , * bits ) : result = [ ] for b in reversed ( bits ) : mask = ( 1 << b ) - 1 result . append ( value & mask ) value = value >> b assert value == 0 result . reverse ( ) return result | Split integer value into list of ints according to bits list . |
53,930 | def readfmt ( stream , fmt ) : size = struct . calcsize ( fmt ) blob = stream . read ( size ) return struct . unpack ( fmt , blob ) | Read and unpack an object from stream using a struct format string . |
53,931 | def setup_logging ( verbosity , filename = None ) : levels = [ logging . WARNING , logging . INFO , logging . DEBUG ] level = levels [ min ( verbosity , len ( levels ) - 1 ) ] logging . root . setLevel ( level ) fmt = logging . Formatter ( '%(asctime)s %(levelname)-12s %(message)-100s ' '[%(filename)s:%(lineno)d]' ) hd... | Configure logging for this tool . |
53,932 | def which ( cmd ) : try : from shutil import which as _which except ImportError : from backports . shutil_which import which as _which full_path = _which ( cmd ) if full_path is None : raise OSError ( 'Cannot find {!r} in $PATH' . format ( cmd ) ) log . debug ( 'which %r => %r' , cmd , full_path ) return full_path | Return full path to specified command or raise OSError if missing . |
53,933 | def readfmt ( self , fmt ) : size = struct . calcsize ( fmt ) blob = self . read ( size ) obj , = struct . unpack ( fmt , blob ) return obj | Read a specified object using a struct format string . |
53,934 | def read ( self , size = None ) : blob = self . s . read ( size ) if size is not None and len ( blob ) < size : raise EOFError if self . _captured : self . _captured . write ( blob ) return blob | Read size bytes from stream . |
53,935 | def get ( self ) : if self . timer ( ) > self . deadline : self . value = None return self . value | Returns existing value or None if deadline has expired . |
53,936 | def set ( self , value ) : self . deadline = self . timer ( ) + self . duration self . value = value | Set new value and reset the deadline for expiration . |
53,937 | def sig_encode ( r , s ) : r = util . assuan_serialize ( util . num2bytes ( r , 32 ) ) s = util . assuan_serialize ( util . num2bytes ( s , 32 ) ) return b'(7:sig-val(5:ecdsa(1:r32:' + r + b')(1:s32:' + s + b')))' | Serialize ECDSA signature data into GPG S - expression . |
53,938 | def parse_ecdh ( line ) : prefix , line = line . split ( b' ' , 1 ) assert prefix == b'D' exp , leftover = keyring . parse ( keyring . unescape ( line ) ) log . debug ( 'ECDH s-exp: %r' , exp ) assert not leftover label , exp = exp assert label == b'enc-val' assert exp [ 0 ] == b'ecdh' items = exp [ 1 : ] log . debug (... | Parse ECDH request and return remote public key . |
53,939 | def handle_getinfo ( self , conn , args ) : result = None if args [ 0 ] == b'version' : result = self . version elif args [ 0 ] == b's2k_count' : result = '{}' . format ( 64 << 20 ) . encode ( 'ascii' ) else : log . warning ( 'Unknown GETINFO command: %s' , args ) if result : keyring . sendline ( conn , b'D ' + result ... | Handle some of the GETINFO messages . |
53,940 | def handle_scd ( self , conn , args ) : reply = { ( b'GETINFO' , b'version' ) : self . version , } . get ( args ) if reply is None : raise AgentError ( b'ERR 100696144 No such device <SCD>' ) keyring . sendline ( conn , b'D ' + reply ) | No support for smart - card device protocol . |
53,941 | def get_identity ( self , keygrip ) : keygrip_bytes = binascii . unhexlify ( keygrip ) pubkey_dict , user_ids = decode . load_by_keygrip ( pubkey_bytes = self . pubkey_bytes , keygrip = keygrip_bytes ) user_id = user_ids [ 0 ] [ 'value' ] . decode ( 'utf-8' ) curve_name = protocol . get_curve_name_by_oid ( pubkey_dict ... | Returns device . interface . Identity that matches specified keygrip . |
53,942 | def pksign ( self , conn ) : log . debug ( 'signing %r digest (algo #%s)' , self . digest , self . algo ) identity = self . get_identity ( keygrip = self . keygrip ) r , s = self . client . sign ( identity = identity , digest = binascii . unhexlify ( self . digest ) ) result = sig_encode ( r , s ) log . debug ( 'result... | Sign a message digest using a private EC key . |
53,943 | def pkdecrypt ( self , conn ) : for msg in [ b'S INQUIRE_MAXLEN 4096' , b'INQUIRE CIPHERTEXT' ] : keyring . sendline ( conn , msg ) line = keyring . recvline ( conn ) assert keyring . recvline ( conn ) == b'END' remote_pubkey = parse_ecdh ( line ) identity = self . get_identity ( keygrip = self . keygrip ) ec_point = s... | Handle decryption using ECDH . |
53,944 | def have_key ( self , * keygrips ) : for keygrip in keygrips : try : self . get_identity ( keygrip = keygrip ) break except KeyError as e : log . warning ( 'HAVEKEY(%s) failed: %s' , keygrip , e ) else : raise AgentError ( b'ERR 67108881 No secret key <GPG Agent>' ) | Check if any keygrip corresponds to a TREZOR - based key . |
53,945 | def set_hash ( self , algo , digest ) : self . algo = algo self . digest = digest | Set algorithm ID and hexadecimal digest for next operation . |
53,946 | def handle ( self , conn ) : keyring . sendline ( conn , b'OK' ) for line in keyring . iterlines ( conn ) : parts = line . split ( b' ' ) command = parts [ 0 ] args = tuple ( parts [ 1 : ] ) if command == b'BYE' : return elif command == b'KILLAGENT' : keyring . sendline ( conn , b'OK' ) raise AgentStop ( ) if command n... | Handle connection from GPG binary using the ASSUAN protocol . |
53,947 | def connect ( self ) : log . critical ( 'NEVER USE THIS CODE FOR REAL-LIFE USE-CASES!!!' ) log . critical ( 'ONLY FOR DEBUGGING AND TESTING!!!' ) self . secexp = 1 self . sk = ecdsa . SigningKey . from_secret_exponent ( secexp = self . secexp , curve = ecdsa . curves . NIST256p , hashfunc = hashlib . sha256 ) self . vk... | Return dummy connection . |
53,948 | def create_identity ( user_id , curve_name ) : result = interface . Identity ( identity_str = 'gpg://' , curve_name = curve_name ) result . identity_dict [ 'host' ] = user_id return result | Create GPG identity for hardware device . |
53,949 | def pubkey ( self , identity , ecdh = False ) : with self . device : pubkey = self . device . pubkey ( ecdh = ecdh , identity = identity ) return formats . decompress_pubkey ( pubkey = pubkey , curve_name = identity . curve_name ) | Return public key as VerifyingKey object . |
53,950 | def sign ( self , identity , digest ) : log . info ( 'please confirm GPG signature on %s for "%s"...' , self . device , identity . to_string ( ) ) if identity . curve_name == formats . CURVE_NIST256 : digest = digest [ : 32 ] log . debug ( 'signing digest: %s' , util . hexlify ( digest ) ) with self . device : sig = se... | Sign the digest and return a serialized signature . |
53,951 | def ecdh ( self , identity , pubkey ) : log . info ( 'please confirm GPG decryption on %s for "%s"...' , self . device , identity . to_string ( ) ) with self . device : return self . device . ecdh ( pubkey = pubkey , identity = identity ) | Derive shared secret using ECDH from remote public key . |
53,952 | def connect ( self ) : transport = self . _defs . find_device ( ) if not transport : raise interface . NotFoundError ( '{} not connected' . format ( self ) ) log . debug ( 'using transport: %s' , transport ) for _ in range ( 5 ) : connection = self . _defs . Client ( transport = transport , ui = self . ui , state = sel... | Enumerate and connect to the first available interface . |
53,953 | def string_to_identity ( identity_str ) : m = _identity_regexp . match ( identity_str ) result = m . groupdict ( ) log . debug ( 'parsed identity: %s' , result ) return { k : v for k , v in result . items ( ) if v } | Parse string into Identity dictionary . |
53,954 | def identity_to_string ( identity_dict ) : result = [ ] if identity_dict . get ( 'proto' ) : result . append ( identity_dict [ 'proto' ] + '://' ) if identity_dict . get ( 'user' ) : result . append ( identity_dict [ 'user' ] + '@' ) result . append ( identity_dict [ 'host' ] ) if identity_dict . get ( 'port' ) : resul... | Dump Identity dictionary into its string representation . |
53,955 | def items ( self ) : return [ ( k , unidecode . unidecode ( v ) ) for k , v in self . identity_dict . items ( ) ] | Return a copy of identity_dict items . |
53,956 | def to_bytes ( self ) : s = identity_to_string ( self . identity_dict ) return unidecode . unidecode ( s ) . encode ( 'ascii' ) | Transliterate Unicode into ASCII . |
53,957 | def get_curve_name ( self , ecdh = False ) : if ecdh : return formats . get_ecdh_curve_name ( self . curve_name ) else : return self . curve_name | Return correct curve name for device operations . |
53,958 | def serve ( handler , sock_path , timeout = UNIX_SOCKET_TIMEOUT ) : ssh_version = subprocess . check_output ( [ 'ssh' , '-V' ] , stderr = subprocess . STDOUT ) log . debug ( 'local SSH version: %r' , ssh_version ) environ = { 'SSH_AUTH_SOCK' : sock_path , 'SSH_AGENT_PID' : str ( os . getpid ( ) ) } device_mutex = threa... | Start the ssh - agent server on a UNIX - domain socket . |
53,959 | def run_server ( conn , command , sock_path , debug , timeout ) : ret = 0 try : handler = protocol . Handler ( conn = conn , debug = debug ) with serve ( handler = handler , sock_path = sock_path , timeout = timeout ) as env : if command : ret = server . run_process ( command = command , environ = env ) else : signal .... | Common code for run_agent and run_git below . |
53,960 | def handle_connection_error ( func ) : @ functools . wraps ( func ) def wrapper ( * args , ** kwargs ) : try : return func ( * args , ** kwargs ) except device . interface . NotFoundError as e : log . error ( 'Connection error (try unplugging and replugging your device): %s' , e ) return 1 return wrapper | Fail with non - zero exit code . |
53,961 | def parse_config ( contents ) : for identity_str , curve_name in re . findall ( r'\<(.*?)\|(.*?)\>' , contents ) : yield device . interface . Identity ( identity_str = identity_str , curve_name = curve_name ) | Parse config file into a list of Identity objects . |
53,962 | def main ( device_type ) : args = create_agent_parser ( device_type = device_type ) . parse_args ( ) util . setup_logging ( verbosity = args . verbose , filename = args . log_file ) public_keys = None filename = None if args . identity . startswith ( '/' ) : filename = args . identity contents = open ( filename , 'rb' ... | Run ssh - agent using given hardware client factory . |
53,963 | def parse_public_keys ( self ) : public_keys = [ formats . import_public_key ( pk ) for pk in self . public_keys ( ) ] for pk , identity in zip ( public_keys , self . identities ) : pk [ 'identity' ] = identity return public_keys | Parse SSH public keys into dictionaries . |
53,964 | def public_keys_as_files ( self ) : if not self . public_keys_tempfiles : for pk in self . public_keys ( ) : f = tempfile . NamedTemporaryFile ( prefix = 'trezor-ssh-pubkey-' , mode = 'w' ) f . write ( pk ) f . flush ( ) self . public_keys_tempfiles . append ( f ) return self . public_keys_tempfiles | Store public keys as temporary SSH identity files . |
53,965 | def sign ( self , blob , identity ) : conn = self . conn_factory ( ) return conn . sign_ssh_challenge ( blob = blob , identity = identity ) | Sign a given blob using the specified identity on the device . |
53,966 | def packet ( tag , blob ) : assert len ( blob ) < 2 ** 32 if len ( blob ) < 2 ** 8 : length_type = 0 elif len ( blob ) < 2 ** 16 : length_type = 1 else : length_type = 2 fmt = [ '>B' , '>H' , '>L' ] [ length_type ] leading_byte = 0x80 | ( tag << 2 ) | ( length_type ) return struct . pack ( '>B' , leading_byte ) + util ... | Create small GPG packet . |
53,967 | def subpacket ( subpacket_type , fmt , * values ) : blob = struct . pack ( fmt , * values ) if values else fmt return struct . pack ( '>B' , subpacket_type ) + blob | Create GPG subpacket . |
53,968 | def subpacket_prefix_len ( item ) : n = len ( item ) if n >= 8384 : prefix = b'\xFF' + struct . pack ( '>L' , n ) elif n >= 192 : n = n - 192 prefix = struct . pack ( 'BB' , ( n // 256 ) + 192 , n % 256 ) else : prefix = struct . pack ( 'B' , n ) return prefix + item | Prefix subpacket length according to RFC 4880 section - 5 . 2 . 3 . 1 . |
53,969 | def subpackets ( * items ) : prefixed = [ subpacket_prefix_len ( item ) for item in items ] return util . prefix_len ( '>H' , b'' . join ( prefixed ) ) | Serialize several GPG subpackets . |
53,970 | def mpi ( value ) : bits = value . bit_length ( ) data_size = ( bits + 7 ) // 8 data_bytes = bytearray ( data_size ) for i in range ( data_size ) : data_bytes [ i ] = value & 0xFF value = value >> 8 data_bytes . reverse ( ) return struct . pack ( '>H' , bits ) + bytes ( data_bytes ) | Serialize multipresicion integer using GPG format . |
53,971 | def keygrip_nist256 ( vk ) : curve = vk . curve . curve gen = vk . curve . generator g = ( 4 << 512 ) | ( gen . x ( ) << 256 ) | gen . y ( ) point = vk . pubkey . point q = ( 4 << 512 ) | ( point . x ( ) << 256 ) | point . y ( ) return _compute_keygrip ( [ [ 'p' , util . num2bytes ( curve . p ( ) , size = 32 ) ] , [ 'a... | Compute keygrip for NIST256 curve public keys . |
53,972 | def keygrip_ed25519 ( vk ) : return _compute_keygrip ( [ [ 'p' , util . num2bytes ( 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFED , size = 32 ) ] , [ 'a' , b'\x01' ] , [ 'b' , util . num2bytes ( 0x2DFC9311D490018C7338BF8688861767FF8FF5B2BEBE27548A14B235ECA6874A , size = 32 ) ] , [ 'g' , util . num2... | Compute keygrip for Ed25519 public keys . |
53,973 | def keygrip_curve25519 ( vk ) : return _compute_keygrip ( [ [ 'p' , util . num2bytes ( 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFED , size = 32 ) ] , [ 'a' , b'\x01\xDB\x41' ] , [ 'b' , b'\x01' ] , [ 'g' , util . num2bytes ( 0x04000000000000000000000000000000000000000000000000000000000000000920ae1... | Compute keygrip for Curve25519 public keys . |
53,974 | def get_curve_name_by_oid ( oid ) : for curve_name , info in SUPPORTED_CURVES . items ( ) : if info [ 'oid' ] == oid : return curve_name raise KeyError ( 'Unknown OID: {!r}' . format ( oid ) ) | Return curve name matching specified OID or raise KeyError . |
53,975 | def make_signature ( signer_func , data_to_sign , public_algo , hashed_subpackets , unhashed_subpackets , sig_type = 0 ) : header = struct . pack ( '>BBBB' , 4 , sig_type , public_algo , 8 ) hashed = subpackets ( * hashed_subpackets ) unhashed = subpackets ( * unhashed_subpackets ) tail = b'\x04\xff' + struct . pack ( ... | Create new GPG signature . |
53,976 | def data ( self ) : header = struct . pack ( '>BLB' , 4 , self . created , self . algo_id ) oid = util . prefix_len ( '>B' , self . curve_info [ 'oid' ] ) blob = self . curve_info [ 'serialize' ] ( self . verifying_key ) return header + oid + blob + self . ecdh_packet | Data for packet creation . |
53,977 | def create_subkey ( primary_bytes , subkey , signer_func , secret_bytes = b'' ) : subkey_packet = protocol . packet ( tag = ( 7 if secret_bytes else 14 ) , blob = ( subkey . data ( ) + secret_bytes ) ) packets = list ( decode . parse_packets ( io . BytesIO ( primary_bytes ) ) ) primary , user_id , signature = packets [... | Export new subkey to GPG primary key . |
53,978 | def verify_gpg_version ( ) : existing_gpg = keyring . gpg_version ( ) . decode ( 'ascii' ) required_gpg = '>=2.1.11' msg = 'Existing GnuPG has version "{}" ({} required)' . format ( existing_gpg , required_gpg ) if not semver . match ( existing_gpg , required_gpg ) : log . error ( msg ) | Make sure that the installed GnuPG is not too old . |
53,979 | def check_output ( args ) : log . debug ( 'run: %s' , args ) out = subprocess . check_output ( args = args ) . decode ( 'utf-8' ) log . debug ( 'out: %r' , out ) return out | Runs command and returns the output as string . |
53,980 | def check_call ( args , stdin = None , env = None ) : log . debug ( 'run: %s%s' , args , ' {}' . format ( env ) if env else '' ) subprocess . check_call ( args = args , stdin = stdin , env = env ) | Runs command and verifies its success . |
53,981 | def write_file ( path , data ) : with open ( path , 'w' ) as f : log . debug ( 'setting %s contents:\n%s' , path , data ) f . write ( data ) return f | Writes data to specified path . |
53,982 | def run_agent ( device_type ) : p = argparse . ArgumentParser ( ) p . add_argument ( '--homedir' , default = os . environ . get ( 'GNUPGHOME' ) ) p . add_argument ( '-v' , '--verbose' , default = 0 , action = 'count' ) p . add_argument ( '--server' , default = False , action = 'store_true' , help = 'Use stdin/stdout fo... | Run a simple GPG - agent server . |
53,983 | def find_device ( ) : try : return get_transport ( os . environ . get ( "TREZOR_PATH" ) ) except Exception as e : log . debug ( "Failed to find a Trezor device: %s" , e ) | Selects a transport based on TREZOR_PATH environment variable . |
53,984 | def _convert_public_key ( ecdsa_curve_name , result ) : if ecdsa_curve_name == 'nist256p1' : if ( result [ 64 ] & 1 ) != 0 : result = bytearray ( [ 0x03 ] ) + result [ 1 : 33 ] else : result = bytearray ( [ 0x02 ] ) + result [ 1 : 33 ] else : result = result [ 1 : ] keyX = bytearray ( result [ 0 : 32 ] ) keyY = bytearr... | Convert Ledger reply into PublicKey object . |
53,985 | def connect ( self ) : try : return comm . getDongle ( ) except comm . CommException as e : raise interface . NotFoundError ( '{} not connected: "{}"' . format ( self , e ) ) | Enumerate and connect to the first USB HID interface . |
53,986 | def pubkey ( self , identity , ecdh = False ) : curve_name = identity . get_curve_name ( ecdh ) path = _expand_path ( identity . get_bip32_address ( ecdh ) ) if curve_name == 'nist256p1' : p2 = '01' else : p2 = '02' apdu = '800200' + p2 apdu = binascii . unhexlify ( apdu ) apdu += bytearray ( [ len ( path ) + 1 , len (... | Get PublicKey object for specified BIP32 address and elliptic curve . |
53,987 | def download_setuptools ( version = DEFAULT_VERSION , download_base = DEFAULT_URL , to_dir = os . curdir , delay = 15 ) : to_dir = os . path . abspath ( to_dir ) try : from urllib . request import urlopen except ImportError : from urllib2 import urlopen tgz_name = "distribute-%s.tar.gz" % version url = download_base + ... | Download distribute from a specified location and return its filename |
53,988 | def tokenize ( stream , separator ) : for value in stream : for token in value . split ( separator ) : if token : yield token . strip ( ) | Tokenize and yield query parameter values . |
53,989 | def build_query ( self , ** filters ) : applicable_filters = [ ] applicable_exclusions = [ ] for param , value in filters . items ( ) : excluding_term = False param_parts = param . split ( "__" ) base_param = param_parts [ 0 ] negation_keyword = constants . DRF_HAYSTACK_NEGATION_KEYWORD if len ( param_parts ) > 1 and p... | Creates a single SQ filter from querystring parameters that correspond to the SearchIndex fields that have been registered in view . fields . |
53,990 | def build_query ( self , ** filters ) : field_facets = { } date_facets = { } query_facets = { } facet_serializer_cls = self . view . get_facet_serializer_class ( ) if self . view . lookup_sep == ":" : raise AttributeError ( "The %(cls)s.lookup_sep attribute conflicts with the HaystackFacetFilter " "query parameter pars... | Creates a dict of dictionaries suitable for passing to the SearchQuerySet facet date_facet or query_facet method . All key word arguments should be wrapped in a list . |
53,991 | def parse_field_options ( self , * options ) : defaults = { } for option in options : if isinstance ( option , six . text_type ) : tokens = [ token . strip ( ) for token in option . split ( self . view . lookup_sep ) ] for token in tokens : if not len ( token . split ( ":" ) ) == 2 : warnings . warn ( "The %s token is ... | Parse the field options query string and return it as a dictionary . |
53,992 | def build_query ( self , ** filters ) : applicable_filters = None filters = dict ( ( k , filters [ k ] ) for k in chain ( self . D . UNITS . keys ( ) , [ constants . DRF_HAYSTACK_SPATIAL_QUERY_PARAM ] ) if k in filters ) distance = dict ( ( k , v ) for k , v in filters . items ( ) if k in self . D . UNITS . keys ( ) ) ... | Build queries for geo spatial filtering . |
53,993 | def merge_dict ( a , b ) : if not isinstance ( b , dict ) : return b result = deepcopy ( a ) for key , val in six . iteritems ( b ) : if key in result and isinstance ( result [ key ] , dict ) : result [ key ] = merge_dict ( result [ key ] , val ) elif key in result and isinstance ( result [ key ] , list ) : result [ ke... | Recursively merges and returns dict a with dict b . Any list values will be combined and returned sorted . |
53,994 | def get_queryset ( self , index_models = [ ] ) : if self . queryset is not None and isinstance ( self . queryset , self . object_class ) : queryset = self . queryset . all ( ) else : queryset = self . object_class ( ) . _clone ( ) if len ( index_models ) : queryset = queryset . models ( * index_models ) elif len ( self... | Get the list of items for this view . Returns self . queryset if defined and is a self . object_class instance . |
53,995 | def get_object ( self ) : queryset = self . get_queryset ( ) if "model" in self . request . query_params : try : app_label , model = map ( six . text_type . lower , self . request . query_params [ "model" ] . split ( "." , 1 ) ) ctype = ContentType . objects . get ( app_label = app_label , model = model ) queryset = se... | Fetch a single document from the data store according to whatever unique identifier is available for that document in the SearchIndex . |
53,996 | def more_like_this ( self , request , pk = None ) : obj = self . get_object ( ) . object queryset = self . filter_queryset ( self . get_queryset ( ) ) . more_like_this ( obj ) page = self . paginate_queryset ( queryset ) if page is not None : serializer = self . get_serializer ( page , many = True ) return self . get_p... | Sets up a detail route for more - like - this results . Note that you ll need backend support in order to take advantage of this . |
53,997 | def filter_facet_queryset ( self , queryset ) : for backend in list ( self . facet_filter_backends ) : queryset = backend ( ) . filter_queryset ( self . request , queryset , self ) if self . load_all : queryset = queryset . load_all ( ) return queryset | Given a search queryset filter it with whichever facet filter backends in use . |
53,998 | def get_facet_serializer ( self , * args , ** kwargs ) : assert "objects" in kwargs , "`objects` is a required argument to `get_facet_serializer()`" facet_serializer_class = self . get_facet_serializer_class ( ) kwargs [ "context" ] = self . get_serializer_context ( ) kwargs [ "context" ] . update ( { "objects" : kwarg... | Return the facet serializer instance that should be used for serializing faceted output . |
53,999 | def get_facet_serializer_class ( self ) : if self . facet_serializer_class is None : raise AttributeError ( "%(cls)s should either include a `facet_serializer_class` attribute, " "or override %(cls)s.get_facet_serializer_class() method." % { "cls" : self . __class__ . __name__ } ) return self . facet_serializer_class | Return the class to use for serializing facets . Defaults to using self . facet_serializer_class . |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.