home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / M2Crypto / RSA.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  7.8 KB  |  215 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import sys
  5. import util
  6. import BIO
  7. import Err
  8. import m2
  9.  
  10. class RSAError(Exception):
  11.     pass
  12.  
  13. m2.rsa_init(RSAError)
  14. no_padding = m2.no_padding
  15. pkcs1_padding = m2.pkcs1_padding
  16. sslv23_padding = m2.sslv23_padding
  17. pkcs1_oaep_padding = m2.pkcs1_oaep_padding
  18.  
  19. class RSA:
  20.     m2_rsa_free = m2.rsa_free
  21.     
  22.     def __init__(self, rsa, _pyfree = 0):
  23.         self.rsa = rsa
  24.         self._pyfree = _pyfree
  25.  
  26.     
  27.     def __del__(self):
  28.         if getattr(self, '_pyfree', 0):
  29.             self.m2_rsa_free(self.rsa)
  30.         
  31.  
  32.     
  33.     def __len__(self):
  34.         return m2.rsa_size(self.rsa) << 3
  35.  
  36.     
  37.     def __getattr__(self, name):
  38.         if name == 'e':
  39.             return m2.rsa_get_e(self.rsa)
  40.         elif name == 'n':
  41.             return m2.rsa_get_n(self.rsa)
  42.         else:
  43.             raise AttributeError
  44.  
  45.     
  46.     def pub(self):
  47.         return (m2.rsa_get_e(self.rsa), m2.rsa_get_n(self.rsa))
  48.  
  49.     
  50.     def public_encrypt(self, data, padding):
  51.         return m2.rsa_public_encrypt(self.rsa, data, padding)
  52.  
  53.     
  54.     def public_decrypt(self, data, padding):
  55.         return m2.rsa_public_decrypt(self.rsa, data, padding)
  56.  
  57.     
  58.     def private_encrypt(self, data, padding):
  59.         return m2.rsa_private_encrypt(self.rsa, data, padding)
  60.  
  61.     
  62.     def private_decrypt(self, data, padding):
  63.         return m2.rsa_private_decrypt(self.rsa, data, padding)
  64.  
  65.     
  66.     def save_key_bio(self, bio, cipher = 'aes_128_cbc', callback = util.passphrase_callback):
  67.         if cipher is None:
  68.             return m2.rsa_write_key_no_cipher(self.rsa, bio._ptr(), callback)
  69.         else:
  70.             ciph = getattr(m2, cipher, None)
  71.             if ciph is None:
  72.                 raise RSAError, 'not such cipher %s' % cipher
  73.             else:
  74.                 ciph = ciph()
  75.             return m2.rsa_write_key(self.rsa, bio._ptr(), ciph, callback)
  76.  
  77.     
  78.     def save_key(self, file, cipher = 'aes_128_cbc', callback = util.passphrase_callback):
  79.         bio = BIO.openfile(file, 'wb')
  80.         return self.save_key_bio(bio, cipher, callback)
  81.  
  82.     save_pem = save_key
  83.     
  84.     def as_pem(self, cipher = 'aes_128_cbc', callback = util.passphrase_callback):
  85.         bio = BIO.MemoryBuffer()
  86.         self.save_key_bio(bio, cipher, callback)
  87.         return bio.read()
  88.  
  89.     
  90.     def save_key_der_bio(self, bio):
  91.         return m2.rsa_write_key_der(self.rsa, bio._ptr())
  92.  
  93.     
  94.     def save_key_der(self, file):
  95.         bio = BIO.openfile(file, 'wb')
  96.         return self.save_key_der_bio(bio)
  97.  
  98.     
  99.     def save_pub_key_bio(self, bio):
  100.         return m2.rsa_write_pub_key(self.rsa, bio._ptr())
  101.  
  102.     
  103.     def save_pub_key(self, file):
  104.         bio = BIO.openfile(file, 'wb')
  105.         return m2.rsa_write_pub_key(self.rsa, bio._ptr())
  106.  
  107.     
  108.     def check_key(self):
  109.         return m2.rsa_check_key(self.rsa)
  110.  
  111.     
  112.     def sign(self, digest, algo = 'sha1'):
  113.         digest_type = getattr(m2, 'NID_' + algo, None)
  114.         if digest_type is None:
  115.             raise ValueError, ('unknown algorithm', algo)
  116.         
  117.         return m2.rsa_sign(self.rsa, digest, digest_type)
  118.  
  119.     
  120.     def verify(self, data, signature, algo = 'sha1'):
  121.         digest_type = getattr(m2, 'NID_' + algo, None)
  122.         if digest_type is None:
  123.             raise ValueError, ('unknown algorithm', algo)
  124.         
  125.         return m2.rsa_verify(self.rsa, data, signature, digest_type)
  126.  
  127.  
  128.  
  129. class RSA_pub(RSA):
  130.     
  131.     def __setattr__(self, name, value):
  132.         if name in ('e', 'n'):
  133.             raise RSAError, 'use factory function new_pub_key() to set (e, n)'
  134.         else:
  135.             self.__dict__[name] = value
  136.  
  137.     
  138.     def private_encrypt(self, *argv):
  139.         raise RSAError, 'RSA_pub object has no private key'
  140.  
  141.     
  142.     def private_decrypt(self, *argv):
  143.         raise RSAError, 'RSA_pub object has no private key'
  144.  
  145.     
  146.     def save_key(self, file, *args, **kw):
  147.         return self.save_pub_key(file)
  148.  
  149.     
  150.     def save_key_bio(self, bio, *args, **kw):
  151.         return self.save_pub_key_bio(bio)
  152.  
  153.     
  154.     def check_key(self):
  155.         return m2.rsa_check_pub_key(self.rsa)
  156.  
  157.  
  158.  
  159. def rsa_error():
  160.     raise RSAError, m2.err_reason_error_string(m2.err_get_error())
  161.  
  162.  
  163. def keygen_callback(p, n, out = sys.stdout):
  164.     ch = [
  165.         '.',
  166.         '+',
  167.         '*',
  168.         '\n']
  169.     out.write(ch[p])
  170.     out.flush()
  171.  
  172.  
  173. def gen_key(bits, e, callback = keygen_callback):
  174.     return RSA(m2.rsa_generate_key(bits, e, callback), 1)
  175.  
  176.  
  177. def load_key(file, callback = util.passphrase_callback):
  178.     bio = BIO.openfile(file)
  179.     return load_key_bio(bio, callback)
  180.  
  181.  
  182. def load_key_bio(bio, callback = util.passphrase_callback):
  183.     rsa = m2.rsa_read_key(bio._ptr(), callback)
  184.     if rsa is None:
  185.         rsa_error()
  186.     
  187.     return RSA(rsa, 1)
  188.  
  189.  
  190. def load_key_string(string, callback = util.passphrase_callback):
  191.     bio = BIO.MemoryBuffer(string)
  192.     return load_key_bio(bio, callback)
  193.  
  194.  
  195. def load_pub_key(file):
  196.     bio = BIO.openfile(file)
  197.     return load_pub_key_bio(bio)
  198.  
  199.  
  200. def load_pub_key_bio(bio):
  201.     rsa = m2.rsa_read_pub_key(bio._ptr())
  202.     if rsa is None:
  203.         rsa_error()
  204.     
  205.     return RSA_pub(rsa, 1)
  206.  
  207.  
  208. def new_pub_key(.0):
  209.     (e, n) = .0
  210.     rsa = m2.rsa_new()
  211.     m2.rsa_set_e(rsa, e)
  212.     m2.rsa_set_n(rsa, n)
  213.     return RSA_pub(rsa, 1)
  214.  
  215.