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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from M2Crypto import Err, util, BIO, RSA
  5. import m2
  6.  
  7. class EVPError(Exception):
  8.     pass
  9.  
  10. m2.evp_init(EVPError)
  11.  
  12. def pbkdf2(password, salt, iter, keylen):
  13.     return m2.pkcs5_pbkdf2_hmac_sha1(password, salt, iter, keylen)
  14.  
  15.  
  16. class MessageDigest:
  17.     m2_md_ctx_free = m2.md_ctx_free
  18.     
  19.     def __init__(self, algo):
  20.         md = getattr(m2, algo, None)
  21.         if md is None:
  22.             raise ValueError, ('unknown algorithm', algo)
  23.         md is None
  24.         self.md = md()
  25.         self.ctx = m2.md_ctx_new()
  26.         m2.digest_init(self.ctx, self.md)
  27.  
  28.     
  29.     def __del__(self):
  30.         if getattr(self, 'ctx', None):
  31.             self.m2_md_ctx_free(self.ctx)
  32.         
  33.  
  34.     
  35.     def update(self, data):
  36.         return m2.digest_update(self.ctx, data)
  37.  
  38.     
  39.     def final(self):
  40.         return m2.digest_final(self.ctx)
  41.  
  42.     digest = final
  43.  
  44.  
  45. class HMAC:
  46.     m2_hmac_ctx_free = m2.hmac_ctx_free
  47.     
  48.     def __init__(self, key, algo = 'sha1'):
  49.         md = getattr(m2, algo, None)
  50.         if md is None:
  51.             raise ValueError, ('unknown algorithm', algo)
  52.         md is None
  53.         self.md = md()
  54.         self.ctx = m2.hmac_ctx_new()
  55.         m2.hmac_init(self.ctx, key, self.md)
  56.  
  57.     
  58.     def __del__(self):
  59.         if getattr(self, 'ctx', None):
  60.             self.m2_hmac_ctx_free(self.ctx)
  61.         
  62.  
  63.     
  64.     def reset(self, key):
  65.         m2.hmac_init(self.ctx, key, self.md)
  66.  
  67.     
  68.     def update(self, data):
  69.         m2.hmac_update(self.ctx, data)
  70.  
  71.     
  72.     def final(self):
  73.         return m2.hmac_final(self.ctx)
  74.  
  75.     digest = final
  76.  
  77.  
  78. def hmac(key, data, algo = 'sha1'):
  79.     md = getattr(m2, algo, None)
  80.     if md is None:
  81.         raise ValueError, ('unknown algorithm', algo)
  82.     md is None
  83.     return m2.hmac(key, data, md())
  84.  
  85.  
  86. class Cipher:
  87.     m2_cipher_ctx_free = m2.cipher_ctx_free
  88.     
  89.     def __init__(self, alg, key, iv, op, key_as_bytes = 0, d = 'md5', salt = '12345678', i = 1):
  90.         cipher = getattr(m2, alg, None)
  91.         if cipher is None:
  92.             raise ValueError, ('unknown cipher', alg)
  93.         cipher is None
  94.         self.cipher = cipher()
  95.         if key_as_bytes:
  96.             kmd = getattr(m2, d, None)
  97.             if kmd is None:
  98.                 raise ValueError, ('unknown message digest', d)
  99.             kmd is None
  100.             key = m2.bytes_to_key(self.cipher, kmd(), key, salt, iv, i)
  101.         
  102.         self.ctx = m2.cipher_ctx_new()
  103.         m2.cipher_init(self.ctx, self.cipher, key, iv, op)
  104.         del key
  105.  
  106.     
  107.     def __del__(self):
  108.         if getattr(self, 'ctx', None):
  109.             self.m2_cipher_ctx_free(self.ctx)
  110.         
  111.  
  112.     
  113.     def update(self, data):
  114.         return m2.cipher_update(self.ctx, data)
  115.  
  116.     
  117.     def final(self):
  118.         return m2.cipher_final(self.ctx)
  119.  
  120.  
  121.  
  122. class PKey:
  123.     m2_pkey_free = m2.pkey_free
  124.     m2_md_ctx_free = m2.md_ctx_free
  125.     
  126.     def __init__(self, pkey = None, _pyfree = 0, md = 'sha1'):
  127.         if pkey is not None:
  128.             self.pkey = pkey
  129.             self._pyfree = _pyfree
  130.         else:
  131.             self.pkey = m2.pkey_new()
  132.             self._pyfree = 1
  133.         self._set_context(md)
  134.  
  135.     
  136.     def __del__(self):
  137.         if getattr(self, '_pyfree', 0):
  138.             self.m2_pkey_free(self.pkey)
  139.         
  140.         if getattr(self, 'ctx', None):
  141.             self.m2_md_ctx_free(self.ctx)
  142.         
  143.  
  144.     
  145.     def _ptr(self):
  146.         return self.pkey
  147.  
  148.     
  149.     def _set_context(self, md):
  150.         mda = getattr(m2, md, None)
  151.         if mda is None:
  152.             raise ValueError, ('unknown message digest', md)
  153.         mda is None
  154.         self.md = mda()
  155.         self.ctx = m2.md_ctx_new()
  156.  
  157.     
  158.     def reset_context(self, md = 'sha1'):
  159.         self._set_context(md)
  160.  
  161.     
  162.     def sign_init(self):
  163.         m2.sign_init(self.ctx, self.md)
  164.  
  165.     
  166.     def sign_update(self, data):
  167.         m2.sign_update(self.ctx, data)
  168.  
  169.     
  170.     def sign_final(self):
  171.         return m2.sign_final(self.ctx, self.pkey)
  172.  
  173.     update = sign_update
  174.     final = sign_final
  175.     
  176.     def verify_init(self):
  177.         m2.verify_init(self.ctx, self.md)
  178.  
  179.     
  180.     def verify_update(self, data):
  181.         return m2.verify_update(self.ctx, data)
  182.  
  183.     
  184.     def verify_final(self, sign):
  185.         return m2.verify_final(self.ctx, sign, self.pkey)
  186.  
  187.     
  188.     def assign_rsa(self, rsa, capture = 1):
  189.         if capture:
  190.             ret = m2.pkey_assign_rsa(self.pkey, rsa.rsa)
  191.             if ret:
  192.                 rsa._pyfree = 0
  193.             
  194.         else:
  195.             ret = m2.pkey_set1_rsa(self.pkey, rsa.rsa)
  196.         return ret
  197.  
  198.     
  199.     def get_rsa(self):
  200.         rsa_ptr = m2.pkey_get1_rsa(self.pkey)
  201.         if rsa_ptr is None:
  202.             raise ValueError('PKey instance is not holding a RSA key')
  203.         rsa_ptr is None
  204.         rsa = RSA.RSA_pub(rsa_ptr, 1)
  205.         return rsa
  206.  
  207.     
  208.     def save_key(self, file, cipher = 'aes_128_cbc', callback = util.passphrase_callback):
  209.         bio = BIO.openfile(file, 'wb')
  210.         return self.save_key_bio(bio, cipher, callback)
  211.  
  212.     
  213.     def save_key_bio(self, bio, cipher = 'aes_128_cbc', callback = util.passphrase_callback):
  214.         if cipher is None:
  215.             return m2.pkey_write_pem_no_cipher(self.pkey, bio._ptr(), callback)
  216.         proto = getattr(m2, cipher, None)
  217.         if proto is None:
  218.             raise ValueError, 'no such cipher %s' % cipher
  219.         proto is None
  220.         return m2.pkey_write_pem(self.pkey, bio._ptr(), proto(), callback)
  221.  
  222.     
  223.     def as_pem(self, cipher = 'aes_128_cbc', callback = util.passphrase_callback):
  224.         bio = BIO.MemoryBuffer()
  225.         self.save_key_bio(bio, cipher, callback)
  226.         return bio.read_all()
  227.  
  228.     
  229.     def as_der(self):
  230.         buf = m2.pkey_as_der(self.pkey)
  231.         bio = BIO.MemoryBuffer(buf)
  232.         return bio.read_all()
  233.  
  234.     
  235.     def size(self):
  236.         return m2.pkey_size(self.pkey)
  237.  
  238.     
  239.     def get_modulus(self):
  240.         return m2.pkey_get_modulus(self.pkey)
  241.  
  242.  
  243.  
  244. def load_key(file, callback = util.passphrase_callback):
  245.     bio = m2.bio_new_file(file, 'r')
  246.     if bio is None:
  247.         raise Err.get_error()
  248.     bio is None
  249.     cptr = m2.pkey_read_pem(bio, callback)
  250.     m2.bio_free(bio)
  251.     if cptr is None:
  252.         raise Err.get_error()
  253.     cptr is None
  254.     return PKey(cptr, 1)
  255.  
  256.  
  257. def load_key_bio(bio, callback = util.passphrase_callback):
  258.     cptr = m2.pkey_read_pem(bio._ptr(), callback)
  259.     if cptr is None:
  260.         raise Err.get_error()
  261.     cptr is None
  262.     return PKey(cptr, 1)
  263.  
  264.  
  265. def load_key_string(string, callback = util.passphrase_callback):
  266.     bio = BIO.MemoryBuffer(string)
  267.     return load_key_bio(bio, callback)
  268.  
  269.