我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用OpenSSL.crypto.TYPE_DSA。
def test_failedGeneration(self): """ L{PKeyType.generate_key} takes two arguments, the first giving the key type as one of L{TYPE_RSA} or L{TYPE_DSA} and the second giving the number of bits to generate. If an invalid type is specified or generation fails, L{Error} is raised. If an invalid number of bits is specified, L{ValueError} or L{Error} is raised. """ key = PKey() self.assertRaises(TypeError, key.generate_key) self.assertRaises(TypeError, key.generate_key, 1, 2, 3) self.assertRaises(TypeError, key.generate_key, "foo", "bar") self.assertRaises(Error, key.generate_key, -1, 0) self.assertRaises(ValueError, key.generate_key, TYPE_RSA, -1) self.assertRaises(ValueError, key.generate_key, TYPE_RSA, 0) # XXX RSA generation for small values of bits is fairly buggy in a wide # range of OpenSSL versions. I need to figure out what the safe lower # bound for a reasonable number of OpenSSL versions is and explicitly # check for that in the wrapper. The failure behavior is typically an # infinite loop inside OpenSSL. # self.assertRaises(Error, key.generate_key, TYPE_RSA, 2) # XXX DSA generation seems happy with any number of bits. The DSS # says bits must be between 512 and 1024 inclusive. OpenSSL's DSA # generator doesn't seem to care about the upper limit at all. For # the lower limit, it uses 512 if anything smaller is specified. # So, it doesn't seem possible to make generate_key fail for # TYPE_DSA with a bits argument which is at least an int. # self.assertRaises(Error, key.generate_key, TYPE_DSA, -7)
def test_dsaGeneration(self): """ L{PKeyType.generate_key} generates a DSA key when passed L{TYPE_DSA} as a type and a reasonable number of bits. """ # 512 is a magic number. The DSS (Digital Signature Standard) # allows a minimum of 512 bits for DSA. DSA_generate_parameters # will silently promote any value below 512 to 512. bits = 512 key = PKey() key.generate_key(TYPE_DSA, bits) self.assertEqual(key.type(), TYPE_DSA) self.assertEqual(key.bits(), bits)
def test_regeneration(self): """ L{PKeyType.generate_key} can be called multiple times on the same key to generate new keys. """ key = PKey() for type, bits in [(TYPE_RSA, 512), (TYPE_DSA, 576)]: key.generate_key(type, bits) self.assertEqual(key.type(), type) self.assertEqual(key.bits(), bits)
def inspect(self): t = self.original.type() if t == crypto.TYPE_RSA: ts = 'RSA' elif t == crypto.TYPE_DSA: ts = 'DSA' else: ts = '(Unknown Type!)' L = (self.original.bits(), ts, self.keyHash()) return '%s-bit %s Key Pair with Hash: %s' % L
def createKeyPair(type, bits): """ Create a public/private key pair. Arguments: type - Key type, must be one of TYPE_RSA and TYPE_DSA bits - Number of bits to use in the key Returns: The public/private key pair in a PKey object """ pkey = crypto.PKey() pkey.generate_key(type, bits) return pkey
def test_failed_generation(self): """ `PKey.generate_key` takes two arguments, the first giving the key type as one of `TYPE_RSA` or `TYPE_DSA` and the second giving the number of bits to generate. If an invalid type is specified or generation fails, `Error` is raised. If an invalid number of bits is specified, `ValueError` or `Error` is raised. """ key = PKey() with pytest.raises(TypeError): key.generate_key("foo", "bar") with pytest.raises(Error): key.generate_key(-1, 0) with pytest.raises(ValueError): key.generate_key(TYPE_RSA, -1) with pytest.raises(ValueError): key.generate_key(TYPE_RSA, 0) with pytest.raises(TypeError): key.generate_key(TYPE_RSA, object()) # XXX RSA generation for small values of bits is fairly buggy in a wide # range of OpenSSL versions. I need to figure out what the safe lower # bound for a reasonable number of OpenSSL versions is and explicitly # check for that in the wrapper. The failure behavior is typically an # infinite loop inside OpenSSL. # with pytest.raises(Error): # key.generate_key(TYPE_RSA, 2) # XXX DSA generation seems happy with any number of bits. The DSS # says bits must be between 512 and 1024 inclusive. OpenSSL's DSA # generator doesn't seem to care about the upper limit at all. For # the lower limit, it uses 512 if anything smaller is specified. # So, it doesn't seem possible to make generate_key fail for # TYPE_DSA with a bits argument which is at least an int. # with pytest.raises(Error): # key.generate_key(TYPE_DSA, -7)
def test_dsa_generation(self): """ `PKey.generate_key` generates a DSA key when passed `TYPE_DSA` as a type and a reasonable number of bits. """ # 512 is a magic number. The DSS (Digital Signature Standard) # allows a minimum of 512 bits for DSA. DSA_generate_parameters # will silently promote any value below 512 to 512. bits = 512 key = PKey() key.generate_key(TYPE_DSA, bits) assert key.type() == TYPE_DSA assert key.bits() == bits with pytest.raises(TypeError): key.check()
def test_regeneration(self): """ `PKey.generate_key` can be called multiple times on the same key to generate new keys. """ key = PKey() for type, bits in [(TYPE_RSA, 512), (TYPE_DSA, 576)]: key.generate_key(type, bits) assert key.type() == type assert key.bits() == bits
def test_dump_privatekey_not_rsa_key(self): """ `dump_privatekey` raises `TypeError` if called with a key that is not RSA. """ key = PKey() key.generate_key(TYPE_DSA, 512) with pytest.raises(TypeError): dump_privatekey(FILETYPE_TEXT, key)
def test_failedGeneration(self): """ :py:meth:`PKeyType.generate_key` takes two arguments, the first giving the key type as one of :py:data:`TYPE_RSA` or :py:data:`TYPE_DSA` and the second giving the number of bits to generate. If an invalid type is specified or generation fails, :py:exc:`Error` is raised. If an invalid number of bits is specified, :py:exc:`ValueError` or :py:exc:`Error` is raised. """ key = PKey() self.assertRaises(TypeError, key.generate_key) self.assertRaises(TypeError, key.generate_key, 1, 2, 3) self.assertRaises(TypeError, key.generate_key, "foo", "bar") self.assertRaises(Error, key.generate_key, -1, 0) self.assertRaises(ValueError, key.generate_key, TYPE_RSA, -1) self.assertRaises(ValueError, key.generate_key, TYPE_RSA, 0) # XXX RSA generation for small values of bits is fairly buggy in a wide # range of OpenSSL versions. I need to figure out what the safe lower # bound for a reasonable number of OpenSSL versions is and explicitly # check for that in the wrapper. The failure behavior is typically an # infinite loop inside OpenSSL. # self.assertRaises(Error, key.generate_key, TYPE_RSA, 2) # XXX DSA generation seems happy with any number of bits. The DSS # says bits must be between 512 and 1024 inclusive. OpenSSL's DSA # generator doesn't seem to care about the upper limit at all. For # the lower limit, it uses 512 if anything smaller is specified. # So, it doesn't seem possible to make generate_key fail for # TYPE_DSA with a bits argument which is at least an int. # self.assertRaises(Error, key.generate_key, TYPE_DSA, -7)
def test_dsaGeneration(self): """ :py:meth:`PKeyType.generate_key` generates a DSA key when passed :py:data:`TYPE_DSA` as a type and a reasonable number of bits. """ # 512 is a magic number. The DSS (Digital Signature Standard) # allows a minimum of 512 bits for DSA. DSA_generate_parameters # will silently promote any value below 512 to 512. bits = 512 key = PKey() key.generate_key(TYPE_DSA, bits) # self.assertEqual(key.type(), TYPE_DSA) # self.assertEqual(key.bits(), bits) # self.assertRaises(TypeError, key.check)
def test_regeneration(self): """ :py:meth:`PKeyType.generate_key` can be called multiple times on the same key to generate new keys. """ key = PKey() for type, bits in [(TYPE_RSA, 512), (TYPE_DSA, 576)]: key.generate_key(type, bits) self.assertEqual(key.type(), type) self.assertEqual(key.bits(), bits)
def _create_account_key(self): account_key = crypto.PKey() if self.type == 'RSA': crypto_type = crypto.TYPE_RSA else: crypto_type = crypto.TYPE_DSA try: LOG.info('[global] Generating account key: %s \ (type: %s, size: %s)' % (self.account_key_name, self.type, self.size)) account_key.generate_key(crypto_type, self.size) except (TypeError, ValueError): raise try: LOG.debug('[global] Writting account key: %s/private/%s' % (self.path, self.account_key_name)) accountkey_file = os.open('%s/private/%s' % (self.path, self.account_key_name), os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) os.write(accountkey_file, crypto.dump_privatekey(crypto.FILETYPE_PEM, account_key)) os.close(accountkey_file) except IOError: try: os.remove('%s/private/%s.key' % (self.path, self.account_key_name)) except OSError: pass raise
def _create_private_key(self): private_key = crypto.PKey() if self.type == 'RSA': crypto_type = crypto.TYPE_RSA else: crypto_type = crypto.TYPE_DSA try: LOG.info('[%s] Generating private key (type: %s, size: %s)' % (self.name, self.type, self.size)) private_key.generate_key(crypto_type, self.size) except (TypeError, ValueError): raise try: LOG.debug('[%s] Writting private key: %s/private/%s.key' % (self.name, self.path, self.name)) privatekey_file = os.open('%s/private/%s.key' % (self.path, self.name), os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) os.write(privatekey_file, crypto.dump_privatekey(crypto.FILETYPE_PEM, private_key)) os.close(privatekey_file) except IOError: try: os.remove('%s/private/%s.key' % (self.path, self.name)) except OSError: pass raise
def test_dsaGeneration(self): """ L{PKeyType.generate_key} generates a DSA key when passed L{TYPE_DSA} as a type and a reasonable number of bits. """ # 512 is a magic number. The DSS (Digital Signature Standard) # allows a minimum of 512 bits for DSA. DSA_generate_parameters # will silently promote any value below 512 to 512. bits = 512 key = PKey() key.generate_key(TYPE_DSA, bits) self.assertEqual(key.type(), TYPE_DSA) self.assertEqual(key.bits(), bits) self.assertRaises(TypeError, key.check)