Package smartcard :: Module guid
[hide private]
[frames] | no frames]

Source Code for Module smartcard.guid

 1  """smartcard.guid 
 2   
 3  Utility functions to handle GUIDs as strings or list of bytes 
 4   
 5  __author__ = "http://www.gemalto.com" 
 6   
 7  Copyright 2001-2011 gemalto 
 8  Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com 
 9   
10  This file is part of pyscard. 
11   
12  pyscard is free software; you can redistribute it and/or modify 
13  it under the terms of the GNU Lesser General Public License as published by 
14  the Free Software Foundation; either version 2.1 of the License, or 
15  (at your option) any later version. 
16   
17  pyscard is distributed in the hope that it will be useful, 
18  but WITHOUT ANY WARRANTY; without even the implied warranty of 
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
20  GNU Lesser General Public License for more details. 
21   
22  You should have received a copy of the GNU Lesser General Public License 
23  along with pyscard; if not, write to the Free Software 
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
25  """ 
26  from struct import unpack 
27   
28  # guid is ulong+ushort+ushort+uchar[8]; we need a map because bytes 
29  # are swappted for the first three 
30  map = {0: 3, 1: 2, 2: 1, 3: 0, 4: 5, 5: 4, 6: 7, 7: 6, 8: 8, 9: 9, 
31      10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15} 
32   
33   
34 -def strToGUID(s):
35 """Converts a GUID string into a list of bytes.""" 36 l = [] 37 for i in unpack('x' + '2s' * 4 + 'x' + '2s2sx' * 3 + '2s' * 6 + 'x', s): 38 l += [int(i, 16)] 39 zr = [] 40 for i in xrange(len(l)): 41 zr.append(l[map[i]]) 42 return zr
43 44
45 -def GUIDToStr(g):
46 """Converts a GUID sequence of bytes into a string.""" 47 zr = [] 48 for i in xrange(len(g)): 49 zr.append(g[map[i]]) 50 return "{%2X%2X%2X%2X-%2X%2X-%2X%2X-%2X%2X-%2X%2X%2X%2X%2X%2X}" % tuple(zr)
51 52 if __name__ == "__main__": 53 """Small sample illustrating the use of guid.py.""" 54 import smartcard.guid 55 dummycardguid1 = strToGUID('{AD4F1667-EA75-4124-84D4-641B3B197C65}') 56 print dummycardguid1 57 print GUIDToStr(dummycardguid1) 58