home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / M2Crypto / RSA.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  7.8 KB  |  213 lines

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