Package yawPyCrypto :: Module Key :: Class Key
[show private | hide private]
[frames | no frames]

Class Key

     object --+    
              |    
       IFlatten --+
                  |
     object --+   |
              |   |
AlgoDefinitions --+
                  |
                 Key

Known Subclasses:
SecureKey, SecureAsciiKey, AsciiKey

This class is the main entrypoint for working with public key algorithms from yawPyCrypto. It allows you to create keys, store them to string form, load them from strings (previously stored by calling one of the store*() functions), and also do standard operations on them such as en/decrypt data and sign/verify data. The Key class extends PyCrypto keys by providing them with a key attribute system. Key attributes may be private; this is checked by using the _isPrivate() method of this class. Keys stored/loaded by using this class are not protected, use the SecureKey from the AdvKey.py in case you need this.
Method Summary
  __init__(self, keysize, algo_pub, **kwargs)
Create a new key.
  __cmp__(self, other)
Compares two keys.
  __delitem__(self, name)
Deletes the specified key argument from this key.
  __getitem__(self, name)
Loads the key attribute name from the current key object, and unserializes it.
  __hash__(self)
Returns a hash value of a key instance.
  __iter__(self)
Return an iterator over all names of key arguments.
a new object with type S, a subtype of T __new__(S, ...)
  __setitem__(self, name, value)
Sets the passed in keyword argument called name to the value that is passed in along.
  canDecrypt(self)
Returns a boolean whether this key can decrypt data.
  canEncrypt(self)
Returns a boolean whether this key can encrypt data.
  canSign(self)
Returns a boolean whether this key can sign data.
  canVerify(self)
Returns a boolean whether this key can verify signed data.
  cryptSize(self)
Returns the maximum string length of data which can be encrypted or signed.
  decrypt(self, encdata, wrap, *args, **kwargs)
Decrypt string data as passed in using data.
  encrypt(self, data, wrap, *args, **kwargs)
Encrypt string data as passed in using data.
  hasPrivate(self)
Checks whether this key has a private key part.
  hasPublic(self)
Checks whether this key has a public key part.
  keySize(self)
Returns the key's keysize in bits.
  publicKey(self)
Returns a new key object which contains only the public key associated with the current key, and only public key arguments.
  sign(self, data, hash_algo, wrap, *args, **kwargs)
Create a new signature for the data that is passed in.
  storeKey(self, wrap, *args, **kwargs)
Stores a key to a string of bytes.
  storePublicKey(self, wrap, *args, **kwargs)
Stores only the public key of this key to a string of bytes.
  update(self, args)
Dictionary style updating for a Key instance.
  verify(self, data, signature, wrap, *args, **kwargs)
Verify the signature of the passed in string.
  _IFlatten__serialize(self, *args, **kwargs)
(inherited from IFlatten)
  _IFlatten__unserialize(self, *args, **kwargs)
(inherited from IFlatten)
  _isPrivate(self, key)
Function which is called to check whether the current key argument should be treated as a private key argument (and as such is stored only on a call to storeKey()).
  _Key__createKey(self, keysize)
Function used to create a new key for this Key object.
  _Key__decrypt(self, data)
Decrypt the passed in PyCrypto output of encryption, and return the decrypted string.
  _Key__encrypt(self, data)
Encrypt data as passed in, and return the PyCrypto output of the encrypted data.
  _Key__getEncPrime(self)
Function used to get an encryption prime as required by the El Gamal public key encryption algorithm.
  _Key__getID(self)
Returns the yawPyCrypto ID of the current key object.
  _Key__serialize(self, private, network, *args, **kwargs)
Function called by the Flatten serialization library to serialize a key instance.
  _Key__sign(self, data)
Function that returns a PyCrypto signature for the data that is passed in.
  _Key__unserialize(self, serstate, *args, **kwargs)
Function called by the Flatten serialization library to unserialize a key instance from the dictionary passed in as serstate.
  _Key__verify(self, data, signature)
Function that verifies a PyCrypto signature for the data that is passed in.

Class Variable Summary
property id
classmethod loadKey
dict _Key__typeMap
classmethod _Key__unwrap
classmethod _Key__wrap
list _unwrappers
list _wrappers

Method Details

__init__(self, keysize=1024, algo_pub=3, **kwargs)
(Constructor)

Create a new key. The arguments passed to this function have the
following meaning:
    
    keysize - Integer specifying the size in bits of the key to
              create.
    algo_pub - One of the PUBLICKEY_* constants, specifies the
               public key algorithm to use.
    **kwargs - All extra keyword arguments are used to set key
               attributes.
               
All values have safe defaults; in case you instantiate this class
without specifying any parameters, an RSA key of length 1024 bits
is created, with no key arguments set.

__cmp__(self, other)
(Comparison operator)

Compares two keys. Two keys are equal if and only if their IDs match. This means that two keys can compare equal, although their key arguments are not equal.

__delitem__(self, name)
(Index deletion operator)

Deletes the specified key argument from this key. In case it is not present, a KeyError is raised.

__getitem__(self, name)
(Indexing operator)

Loads the key attribute name from the current key object, and unserializes it. Loading a key attribute checks for validity of the data by comparing the stored string representation against a signature which was stored on setting the attribute (in case the key contained a private key). In case no signature is found, the object is (by default) returned in an Untrusted() wrapping object. In case the signature found is bad, an exception is raised. This behaviour is configurable through the Config.py option WARN_ABOUT_UNTRUSTED_KEY_ARGUMENTS.

__hash__(self)
(Hashing function)

Returns a hash value of a key instance. The hash value is computed by computing the Python hash of the key's ID.

__iter__(self)

Return an iterator over all names of key arguments.

__new__(S, ...)

Returns:
a new object with type S, a subtype of T

__setitem__(self, name, value)
(Index assignment operator)

Sets the passed in keyword argument called name to the value that is passed in along. The value is stored using the serialize function from the Flatten module, and in case the current key has the possibility to sign values, a signature is stored along with the key-data.

canDecrypt(self)

Returns a boolean whether this key can decrypt data.

canEncrypt(self)

Returns a boolean whether this key can encrypt data.

canSign(self)

Returns a boolean whether this key can sign data.

canVerify(self)

Returns a boolean whether this key can verify signed data.

cryptSize(self)

Returns the maximum string length of data which can be encrypted or signed. This is equivalent to doing keySize() // 8.

decrypt(self, encdata, wrap=True, *args, **kwargs)

Decrypt string data as passed in using data. The string must have
been created by the encrypt() method of this class for decryption
to succeed. The parameters passed in have the following meaning:

    data - String data to be decrypted.
    wrap - Defaults to True. In case you use a Key derived class
           such as AsciiKey, specifies whether the decrypted data
           should be unwrapped before decrypting it.
    *args, **kwargs - All extra arguments are passed to the
                      unwrapping functions declared by the current
                      Key(-derived) class.

encrypt(self, data, wrap=True, *args, **kwargs)

Encrypt string data as passed in using data. The string data may
not be longer than self.cryptSize() for encryption to succeed. The
parameters passed in have the following meaning:

    data - String data to be encrypted.
    wrap - Defaults to True. In case you use a Key derived class
           such as AsciiKey, specifies whether the encrypted data
           should be wrapped after storing it.
    *args, **kwargs - All extra arguments are passed to the
                      wrapping functions declared by the current
                      Key(-derived) class.

hasPrivate(self)

Checks whether this key has a private key part.

hasPublic(self)

Checks whether this key has a public key part. Always returns true at the moment.

keySize(self)

Returns the key's keysize in bits.

publicKey(self)

Returns a new key object which contains only the public key associated with the current key, and only public key arguments. This is done by serializing the class, and then loading it again, so the two copies are disjoint.

sign(self, data, hash_algo=5, wrap=True, *args, **kwargs)

Create a new signature for the data that is passed in. The
signature is a string, which is returned. The parameters passed
in have the following meaning:

    data - String data to be signed.
    hash_algo - Hash algorithm to use to hash the data before
                signing it. Special value HASH_NONE for hash_algo
                means that no hashing of the data is done before
                signing it.
    wrap - Defaults to True. In case you use a Key derived class
           such as AsciiKey, specifies whether the signature data
           should be wrapped on return.
    *args, **kwargs - All extra arguments are passed to the
                      wrapping functions declared by the current
                      Key(-derived) class.

storeKey(self, wrap=True, *args, **kwargs)

Stores a key to a string of bytes. The arguments passed to this
function have the following meaning:
    
    wrap - Defaults to True. In case you use a Key derived class
           such as SecureKey or AsciiKey, specifies whether the
           keydata should be wrapped after storing it.
    *args, **kwargs - All extra arguments are passed to the
                      wrapping functions declared by the current
                      Key(-derived) class.

This function stores all key arguments to the key stream, even
when the key only contains a public key. In case the key contains
only a public key, the data is stored as PUBKEYDATA nevertheless
(even though private key arguments are stored to the stream), so
that e.g. SecureKey doesn't do encryption on the key. This means
that by using this function, you may leak private key arguments
that have been set on a public key (these are Untrusted anyway).

storePublicKey(self, wrap=True, *args, **kwargs)

Stores only the public key of this key to a string of bytes. The
arguments passed to this function have the following meaning:

    wrap - Defaults to True. In case you use a Key derived class
           such as SecureKey or AsciiKey, specifies whether the
           keydata should be wrapped after storing it.
    *args, **kwargs - All extra arguments are passed to the
                      wrapping functions declared by the current
                      Key(-derived) class.
                      
This function only stores public key arguments to the output
stream.

update(self, args)

Dictionary style updating for a Key instance. It allows you to set key arguments based on another Key instance, or based on any object supporting dictionary style interation and access.

verify(self, data, signature, wrap=True, *args, **kwargs)

Verify the signature of the passed in string. The signature must
have been created by the sign() method.

    data - String data to be verified.
    signature - Signature of the string data which was returned by
                the sign() function.
    wrap - Defaults to True. In case you use a Key derived class
           such as AsciiKey, specifies whether the signature data
           should be unwrapped before trying to verify it.
    *args, **kwargs - All extra arguments are passed to the
                      unwrapping functions declared by the current
                      Key(-derived) class.

_isPrivate(self, key)

Function which is called to check whether the current key argument should be treated as a private key argument (and as such is stored only on a call to storeKey()). The default implementation checks whether the key argument name is a string, and in case it is, whether it starts with an underscore. In this case it returns True, in all other cases False. In case you need different behaviour, override this method in a Key-derived class.

_Key__createKey(self, keysize)

Function used to create a new key for this Key object.

_Key__decrypt(self, data)

Decrypt the passed in PyCrypto output of encryption, and return the decrypted string.

_Key__encrypt(self, data)

Encrypt data as passed in, and return the PyCrypto output of the encrypted data.

_Key__getEncPrime(self)

Function used to get an encryption prime as required by the El Gamal public key encryption algorithm. It returns a random prime in big-endian string representation.

_Key__getID(self)

Returns the yawPyCrypto ID of the current key object. This is a string, which basically is a hash of all key numbers the public key contains, represented in big-endian byte-order. The key ID is used to compute a hash value of a key, and also to compare two keys.

_Key__serialize(self, private=True, network=False, *args, **kwargs)

Function called by the Flatten serialization library to serialize a key instance. The parameters passed in are the same as passed to the serialize() function. The serialization implementation stores the key to a dictionary. In case the keyword argument private is True and network is False, the private key (in case it is there) is stored, along with all private key arguments; in all other cases only the public key with the public key arguments is stored.

_Key__sign(self, data)

Function that returns a PyCrypto signature for the data that is passed in.

_Key__unserialize(self, serstate, *args, **kwargs)

Function called by the Flatten serialization library to unserialize a key instance from the dictionary passed in as serstate. This function checks whether the key is in a valid format, and loads the data from the key and all key arguments into this instances private variables.

_Key__verify(self, data, signature)

Function that verifies a PyCrypto signature for the data that is passed in.

Class Variable Details

id

Type:
property
Value:
<property object at 0x4022d39c>                                        

loadKey

Type:
classmethod
Value:
<classmethod object at 0x401b598c>                                     

_Key__typeMap

Type:
dict
Value:
{'algopub': <type 'int'>,
 'attrs': <type 'dict'>,
 'attrsigns': <type 'dict'>,
 'key': (<type 'long'>,)}                                              

_Key__unwrap

Type:
classmethod
Value:
<classmethod object at 0x401b56a4>                                     

_Key__wrap

Type:
classmethod
Value:
<classmethod object at 0x401b5ac4>                                     

_unwrappers

Type:
list
Value:
[]                                                                     

_wrappers

Type:
list
Value:
[]                                                                     

Generated by Epydoc 1.1 on Sun Aug 17 03:32:34 2003 http://epydoc.sf.net