我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用Crypto.PublicKey.RSA._RSAobj()。
def initialize_users(self, users, keystore): """ Generates users' keys, wraps them with their public keys, and stores the keywraps and associated info in the key store. Arguments: users ({string: (_RSAobj, [(string, string, string, integer)])}) - a dictionary mapping user IDs to (RSA_pk, info) tuples, where RSA_pk is the user's RSA public key, and info is a list of (attr, vers, metadata, keylen) tuples describing the attribute, version, metadata, and key length (in bytes) of the keys to generate, wrap, and store. Note: attr, vers, and metadata strings must not contain the '|' character. keystore (AbstractKeyStore) - the key store to be written to """ for userid, (RSA_pk, info) in users.iteritems(): keywraps = [] for attr, vers, metadata, keylen in info: sk = self._generate_key(attr, vers, metadata, keylen) keywrap = utils.wrap_key(sk, RSA_pk) keywraps.append(KeyInfo(attr, vers, metadata, keywrap, keylen)) keystore.batch_insert(userid, keywraps)
def prepare_key(self, key): if isinstance(key, RSA._RSAobj): return key if isinstance(key, string_types): if isinstance(key, text_type): key = key.encode('utf-8') key = RSA.importKey(key) else: raise TypeError('Expecting a PEM- or RSA-formatted key.') return key
def keyObject(self, value): # Lazy import to have PyCrypto as a soft dependency. from Crypto.PublicKey import DSA, RSA if isinstance(value, RSA._RSAobj): rawKey = value.key if rawKey.has_private(): newKey = self._fromRSAComponents( e=rawKey.e, n=rawKey.n, p=rawKey.p, q=rawKey.q, d=rawKey.d, u=rawKey.u, ) else: newKey = self._fromRSAComponents(e=rawKey.e, n=rawKey.n) elif isinstance(value, DSA._DSAobj): rawKey = value.key if rawKey.has_private(): newKey = self._fromDSAComponents( y=rawKey.y, p=rawKey.p, q=rawKey.q, g=rawKey.g, x=rawKey.x, ) else: newKey = self._fromDSAComponents( y=rawKey.y, p=rawKey.p, q=rawKey.q, g=rawKey.g, ) else: raise BadKeyError('PyCrypto key type not supported.') self._keyObject = newKey._keyObject
def test_can_convert_to_RSA(self): jwk_public = JWK(**self.public_key) jwk_private = JWK(**self.private_key) self.assertIsInstance(jwk_public.to_rsa(), _RSAobj) self.assertIsInstance(jwk_private.to_rsa(), _RSAobj)
def file_to_dict(user_file): """ Constructs a dictionary mapping users to public keys and attribute key information from a user configuration file. Arguments: user_file (string or [string]): a user config file name or list of file names. Each file should consist of sections with a user ID as the section header followed by 'public_key' and 'key_info' options. The 'public_key' value should be the name of a file containing an exported RSA public key. The 'key_info' value should be a newline-separated list of pipe-delimited strings specifying an attribute, version, metadata, and key length (in bytes). The attribute, version, and metadata must not contain the '|' character. See user_info.cfg for an example. #TODO: allow pipe character within a quoted string Returns: A dictionary of type {string: (RSA._RSAobj, [(string, string, string, integer)])}) mapping user IDs to (RSA_pk, info) tuples, where RSA_pk is the user's RSA public key, and info is a list of (attr, vers, metadata, keylen) tuples describing the attribute, version, metadata, and key length (in bytes) of the user's keys. Raises an IOError if any of the public key files listed within the config file cannot be opened. """ abs_path = os.path.dirname(user_file) user_config = ConfigParser.ConfigParser() try: user_config.read(user_file) except IOError as e: print 'Error opening config file:', e raise e users = {} userids = user_config.sections() for userid in userids: try: f = open(abs_path+'/'+user_config.get(userid, 'public_key'), 'r') except IOError as e: print 'Error opening user', userid +'\'s', 'public key file:' print e raise e RSA_pk = RSA.importKey(f.read()) f.close() info = [] key_infos = user_config.get(userid, 'key_info').splitlines() for key_info in key_infos: attr, vers, metadata, keylen = key_info.split('|') info.append((attr, int(vers), metadata, int(keylen))) users[userid] = (RSA_pk, info) return users