/gdata/Crypto/PublicKey/pubkey.py

http://radioappz.googlecode.com/ · Python · 172 lines · 101 code · 14 blank · 57 comment · 9 complexity · e746ca9a9b80cb42d44b1ab6ecefe165 MD5 · raw file

  1. #
  2. # pubkey.py : Internal functions for public key operations
  3. #
  4. # Part of the Python Cryptography Toolkit
  5. #
  6. # Distribute and use freely; there are no restrictions on further
  7. # dissemination and usage except those imposed by the laws of your
  8. # country of residence. This software is provided "as is" without
  9. # warranty of fitness for use or suitability for any purpose, express
  10. # or implied. Use at your own risk or not at all.
  11. #
  12. __revision__ = "$Id: pubkey.py,v 1.11 2003/04/03 20:36:14 akuchling Exp $"
  13. import types, warnings
  14. from Crypto.Util.number import *
  15. # Basic public key class
  16. class pubkey:
  17. def __init__(self):
  18. pass
  19. def __getstate__(self):
  20. """To keep key objects platform-independent, the key data is
  21. converted to standard Python long integers before being
  22. written out. It will then be reconverted as necessary on
  23. restoration."""
  24. d=self.__dict__
  25. for key in self.keydata:
  26. if d.has_key(key): d[key]=long(d[key])
  27. return d
  28. def __setstate__(self, d):
  29. """On unpickling a key object, the key data is converted to the big
  30. number representation being used, whether that is Python long
  31. integers, MPZ objects, or whatever."""
  32. for key in self.keydata:
  33. if d.has_key(key): self.__dict__[key]=bignum(d[key])
  34. def encrypt(self, plaintext, K):
  35. """encrypt(plaintext:string|long, K:string|long) : tuple
  36. Encrypt the string or integer plaintext. K is a random
  37. parameter required by some algorithms.
  38. """
  39. wasString=0
  40. if isinstance(plaintext, types.StringType):
  41. plaintext=bytes_to_long(plaintext) ; wasString=1
  42. if isinstance(K, types.StringType):
  43. K=bytes_to_long(K)
  44. ciphertext=self._encrypt(plaintext, K)
  45. if wasString: return tuple(map(long_to_bytes, ciphertext))
  46. else: return ciphertext
  47. def decrypt(self, ciphertext):
  48. """decrypt(ciphertext:tuple|string|long): string
  49. Decrypt 'ciphertext' using this key.
  50. """
  51. wasString=0
  52. if not isinstance(ciphertext, types.TupleType):
  53. ciphertext=(ciphertext,)
  54. if isinstance(ciphertext[0], types.StringType):
  55. ciphertext=tuple(map(bytes_to_long, ciphertext)) ; wasString=1
  56. plaintext=self._decrypt(ciphertext)
  57. if wasString: return long_to_bytes(plaintext)
  58. else: return plaintext
  59. def sign(self, M, K):
  60. """sign(M : string|long, K:string|long) : tuple
  61. Return a tuple containing the signature for the message M.
  62. K is a random parameter required by some algorithms.
  63. """
  64. if (not self.has_private()):
  65. raise error, 'Private key not available in this object'
  66. if isinstance(M, types.StringType): M=bytes_to_long(M)
  67. if isinstance(K, types.StringType): K=bytes_to_long(K)
  68. return self._sign(M, K)
  69. def verify (self, M, signature):
  70. """verify(M:string|long, signature:tuple) : bool
  71. Verify that the signature is valid for the message M;
  72. returns true if the signature checks out.
  73. """
  74. if isinstance(M, types.StringType): M=bytes_to_long(M)
  75. return self._verify(M, signature)
  76. # alias to compensate for the old validate() name
  77. def validate (self, M, signature):
  78. warnings.warn("validate() method name is obsolete; use verify()",
  79. DeprecationWarning)
  80. def blind(self, M, B):
  81. """blind(M : string|long, B : string|long) : string|long
  82. Blind message M using blinding factor B.
  83. """
  84. wasString=0
  85. if isinstance(M, types.StringType):
  86. M=bytes_to_long(M) ; wasString=1
  87. if isinstance(B, types.StringType): B=bytes_to_long(B)
  88. blindedmessage=self._blind(M, B)
  89. if wasString: return long_to_bytes(blindedmessage)
  90. else: return blindedmessage
  91. def unblind(self, M, B):
  92. """unblind(M : string|long, B : string|long) : string|long
  93. Unblind message M using blinding factor B.
  94. """
  95. wasString=0
  96. if isinstance(M, types.StringType):
  97. M=bytes_to_long(M) ; wasString=1
  98. if isinstance(B, types.StringType): B=bytes_to_long(B)
  99. unblindedmessage=self._unblind(M, B)
  100. if wasString: return long_to_bytes(unblindedmessage)
  101. else: return unblindedmessage
  102. # The following methods will usually be left alone, except for
  103. # signature-only algorithms. They both return Boolean values
  104. # recording whether this key's algorithm can sign and encrypt.
  105. def can_sign (self):
  106. """can_sign() : bool
  107. Return a Boolean value recording whether this algorithm can
  108. generate signatures. (This does not imply that this
  109. particular key object has the private information required to
  110. to generate a signature.)
  111. """
  112. return 1
  113. def can_encrypt (self):
  114. """can_encrypt() : bool
  115. Return a Boolean value recording whether this algorithm can
  116. encrypt data. (This does not imply that this
  117. particular key object has the private information required to
  118. to decrypt a message.)
  119. """
  120. return 1
  121. def can_blind (self):
  122. """can_blind() : bool
  123. Return a Boolean value recording whether this algorithm can
  124. blind data. (This does not imply that this
  125. particular key object has the private information required to
  126. to blind a message.)
  127. """
  128. return 0
  129. # The following methods will certainly be overridden by
  130. # subclasses.
  131. def size (self):
  132. """size() : int
  133. Return the maximum number of bits that can be handled by this key.
  134. """
  135. return 0
  136. def has_private (self):
  137. """has_private() : bool
  138. Return a Boolean denoting whether the object contains
  139. private components.
  140. """
  141. return 0
  142. def publickey (self):
  143. """publickey(): object
  144. Return a new key object containing only the public information.
  145. """
  146. return self
  147. def __eq__ (self, other):
  148. """__eq__(other): 0, 1
  149. Compare us to other for equality.
  150. """
  151. return self.__getstate__() == other.__getstate__()