Changeset 182

Show
Ignore:
Timestamp:
11/16/09 11:18:22 (9 months ago)
Author:
lrousseau
Message:

fix problems reported by pep8

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pykcs11/trunk/PyKCS11/__init__.py

    r172 r182  
    2525CK_INVALID_HANDLE = PyKCS11.LowLevel.CK_INVALID_HANDLE 
    2626 
    27 CKM = {}; 
    28 CKR = {}; 
    29 CKA = {}; 
    30 CKO = {}; 
    31 CKU = {}; 
    32 CKK = {}; 
    33 CKC = {}; 
    34 CKF = {}; 
    35 CKS = {}; 
     27CKM = {} 
     28CKR = {} 
     29CKA = {} 
     30CKO = {} 
     31CKU = {} 
     32CKK = {} 
     33CKC = {} 
     34CKF = {} 
     35CKS = {} 
    3636 
    3737# redefine PKCS#11 constants using well known prefixes 
     
    4545      or x[:4] == 'CKC_' \ 
    4646      or x[:4] == 'CKF_' \ 
    47       or x[:4] == 'CKS_' \ 
    48       : 
    49         a = "%s=PyKCS11.LowLevel.%s" % (x, x)  
     47      or x[:4] == 'CKS_': 
     48        a = "%s=PyKCS11.LowLevel.%s" % (x, x) 
    5049        exec(a) 
    5150        if x[3:] != "_VENDOR_DEFINED": 
     
    5655CKR[-2] = "Unkown PKCS#11 type" 
    5756CKR[-1] = "Load" 
     57 
    5858 
    5959class CK_SLOT_INFO(object): 
     
    7676        CKF_TOKEN_PRESENT: "CKF_TOKEN_PRESENT", 
    7777        CKF_REMOVABLE_DEVICE: "CKF_REMOVABLE_DEVICE", 
    78         CKF_HW_SLOT: "CKF_HW_SLOT" 
    79     } 
     78        CKF_HW_SLOT: "CKF_HW_SLOT"} 
    8079 
    8180    def flags2text(self): 
     
    9291                r.append(CK_SLOT_INFO.flags_dict[v]) 
    9392        return r 
     93 
    9494 
    9595class CK_INFO(object): 
     
    109109    """ 
    110110 
     111 
    111112class CK_SESSION_INFO(object): 
    112113    """ 
     
    151152        """ 
    152153        return CKS[self.state] 
     154 
    153155 
    154156class CK_TOKEN_INFO(object): 
     
    229231        return r 
    230232 
     233 
    231234class CK_MECHANISM_INFO(object): 
    232235    """ 
     
    272275        return r 
    273276 
     277 
    274278class PyKCS11Error(Exception): 
    275279    """ define the possible PKCS#11 error codes """ 
    276280 
    277     def __init__(self, value, text = ""): 
     281    def __init__(self, value, text=""): 
    278282        self.value = value 
    279283        self.text = text 
     
    289293            return CKR[self.value] + " (0x%08X)" % self.value 
    290294 
     295 
    291296class PyKCS11Lib(object): 
    292297    """ high level PKCS#11 binding """ 
     
    298303        self.lib.Unload() 
    299304 
    300     def load(self, pkcs11dll_filename = None, *init_string): 
     305    def load(self, pkcs11dll_filename=None, *init_string): 
    301306        """ 
    302307        load a PKCS#11 library 
     
    382387        @return: a L{CK_TOKEN_INFO} object 
    383388        """ 
    384         tokeninfo = PyKCS11.LowLevel.CK_TOKEN_INFO() 
     389        tokeninfo = PyKCS11.LowLevel.CK_TOKEN_INFO() 
    385390        rv = self.lib.C_GetTokenInfo(slot, tokeninfo) 
    386391        if rv != CKR_OK: 
     
    428433        return t 
    429434 
    430     def openSession(self, slot, flags = 0): 
     435    def openSession(self, slot, flags=0): 
    431436        """ 
    432437        C_OpenSession 
     
    486491        return i 
    487492 
    488     def waitForSlotEvent(self, flags = 0): 
     493    def waitForSlotEvent(self, flags=0): 
    489494        """ 
    490495        C_WaitForSlotEvent 
     
    502507        return slot 
    503508 
     509 
    504510class Mechanism(object): 
    505511    """Wraps CK_MECHANISM""" 
     512 
    506513    def __init__(self, mechanism, param): 
    507514        """ 
     
    511518        (i.e. the IV for some agorithms) 
    512519        @type param: string or list/tuple of bytes 
    513          
     520 
    514521        @see: L{Session.decrypt}, L{Session.sign} 
    515522        """ 
     
    518525 
    519526MechanismRSAPKCS1 = Mechanism(CKM_RSA_PKCS, None) 
     527 
    520528 
    521529class Session(object): 
     
    556564        return s 
    557565 
    558     def login(self, pin, user_type = CKU_USER): 
     566    def login(self, pin, user_type=CKU_USER): 
    559567        """ 
    560568        C_Login 
     
    603611        if rv != CKR_OK: 
    604612            raise PyKCS11Error(rv) 
    605              
     613 
    606614    def sign(self, key, data, mecha=MechanismRSAPKCS1): 
    607615        """ 
    608616        C_SignInit/C_Sign 
    609          
     617 
    610618        @param key: a key handle, obtained calling L{findObjects}. 
    611619        @type key: integer 
     
    613621        @type data:  (binary) sring or list/tuple of bytes 
    614622        @param mecha: the signing mechanism to be used 
    615         @type mecha: L{Mechanism} instance or L{MechanismRSAPKCS1}  
     623        @type mecha: L{Mechanism} instance or L{MechanismRSAPKCS1} 
    616624        for CKM_RSA_PKCS 
    617625        @return: the computed signature 
    618626        @rtype: list of bytes 
    619          
     627 
    620628        @note: the returned value is an istance of L{LowLevel.ckbytelist}. 
    621629        You can easly convert it to a binary string with:: 
    622630            ''.join(chr(i) for i in ckbytelistSignature) 
    623          
     631 
    624632        """ 
    625633        m = PyKCS11.LowLevel.CK_MECHANISM() 
     
    651659            raise PyKCS11Error(rv) 
    652660        #first call get signature size 
    653         rv = self.lib.C_Sign(self.session, data1, signature); 
     661        rv = self.lib.C_Sign(self.session, data1, signature) 
    654662        if (rv != 0): 
    655663            raise PyKCS11Error(rv) 
    656664        #second call get actual signature data 
    657         rv = self.lib.C_Sign(self.session, data1, signature); 
     665        rv = self.lib.C_Sign(self.session, data1, signature) 
    658666        if (rv != 0): 
    659667            raise PyKCS11Error(rv) 
    660668        return signature 
    661          
     669 
    662670    def decrypt(self, key, data, mecha=MechanismRSAPKCS1): 
    663671        """ 
    664672        C_DecryptInit/C_Decrypt 
    665          
     673 
    666674        @param key: a key handle, obtained calling L{findObjects}. 
    667675        @type key: integer 
     
    673681        @return: the decrypted data 
    674682        @rtype: list of bytes 
    675          
     683 
    676684        @note: the returned value is an istance of L{LowLevel.ckbytelist}. 
    677685        You can easly convert it to a binary string with:: 
    678686            ''.join(chr(i) for i in ckbytelistData) 
    679          
     687 
    680688        """ 
    681689        m = PyKCS11.LowLevel.CK_MECHANISM() 
     
    707715            raise PyKCS11Error(rv) 
    708716        #first call get decrypted size 
    709         rv = self.lib.C_Decrypt(self.session, data1, decrypted); 
     717        rv = self.lib.C_Decrypt(self.session, data1, decrypted) 
    710718        if (rv != 0): 
    711719            raise PyKCS11Error(rv) 
    712720        #second call get actual decrypted data 
    713         rv = self.lib.C_Decrypt(self.session, data1, decrypted); 
     721        rv = self.lib.C_Decrypt(self.session, data1, decrypted) 
    714722        if (rv != 0): 
    715723            raise PyKCS11Error(rv) 
    716724        return decrypted 
    717          
     725 
    718726    def isNum(self, type): 
    719727        if type in (CKA_CERTIFICATE_TYPE, 
     
    759767        return (not self.isBool(type)) and (not self.isString(type)) and (not self.isNum(type)) 
    760768 
    761     def findObjects(self, template = ()): 
     769    def findObjects(self, template=()): 
    762770        """ 
    763771        find the objects matching the template pattern 
     
    802810        return res 
    803811 
    804     def getAttributeValue(self, obj_id, attr, allAsBinary = False): 
     812    def getAttributeValue(self, obj_id, attr, allAsBinary=False): 
    805813        """ 
    806814        C_GetAttributeValue 
     
    814822        @return: a list of values corresponding to the list of attributes 
    815823        @rtype: list 
    816          
     824 
    817825        @see: L{getAttributeValue_fragmented} 
    818          
     826 
    819827        @note: if allAsBinary is True the function do not convert results to 
    820828        Python types (i.e.: CKA_TOKEN to Bool, CKA_CLASS to int, ...). 
     
    823831        You can easly convert it to a binary string with:: 
    824832            ''.join(chr(i) for i in ckbytelistVariable) 
    825          
     833 
    826834        """ 
    827835        valTemplate = PyKCS11.LowLevel.ckattrlist(len(attr)) 
     
    858866        return res 
    859867 
    860     def getAttributeValue_fragmented(self, obj_id, attr, allAsBinary = False): 
     868    def getAttributeValue_fragmented(self, obj_id, attr, allAsBinary=False): 
    861869        """ 
    862870        Same as L{getAttributeValue} except that when some attribute 
    863871        is sensitive or unknown an empty value (None) is retruned. 
    864          
     872 
    865873        Note: this is achived getting attributes one by one. 
    866          
     874 
    867875        @see: L{getAttributeValue} 
    868876        """ 
     
    888896            if rv != CKR_OK: 
    889897                raise PyKCS11Error(rv) 
    890              
     898 
    891899            if (allAsBinary): 
    892900                res.append(valTemplate[0].GetBin()) 
     
    971979    print 
    972980    print "login" 
    973     se.login(pin = "12345678") 
     981    se.login(pin="12345678") 
    974982 
    975983    print 
     
    10011009    print "closeSession" 
    10021010    se.closeSession() 
    1003  
(C) 2006 bit4id srl, for informations please contact info@bit4id.com
visitors since August 21, 2006