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-2012 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 __future__ import print_function
27 import uuid
28
29
30
31
33 """Converts a GUID string into a list of bytes.
34
35 >>> strToGUID('{AD4F1667-EA75-4124-84D4-641B3B197C65}')
36 [103, 22, 79, 173, 117, 234, 36, 65, 132, 212, 100, 27, 59, 25, 124, 101]
37 """
38 dat = uuid.UUID(hex=s)
39 if isinstance(dat.bytes_le, str):
40
41 dat = [ord(e) for e in dat.bytes_le]
42 else:
43
44 dat = list(dat.bytes_le)
45 return dat
46
47
49 """Converts a GUID sequence of bytes into a string.
50
51 >>> GUIDToStr([103,22,79,173, 117,234, 36,65,
52 ... 132, 212, 100, 27, 59, 25, 124, 101])
53 '{AD4F1667-EA75-4124-84D4-641B3B197C65}'
54 """
55 try:
56 dat = uuid.UUID(bytes_le=bytes(g))
57 except:
58 dat = uuid.UUID(bytes_le=''.join(map(chr, g)))
59 return '{' + str(dat).upper() + '}'
60
61
62 if __name__ == "__main__":
63 """Small sample illustrating the use of guid.py."""
64 guid_in = '{AD4F1667-EA75-4124-84D4-641B3B197C65}'
65 print(guid_in)
66 dummycardguid1 = strToGUID(guid_in)
67 print(dummycardguid1)
68 guid_out = GUIDToStr(dummycardguid1)
69 print(guid_out)
70 if guid_in != guid_out:
71 print("Failure")
72 else:
73 print("Success")
74