Re: [ctypes-users] crypto api
Brought to you by:
theller
From: Matt H. (matherbe) <mat...@ci...> - 2008-07-17 23:05:19
|
> -----Original Message----- > From: cty...@li... > [mailto:cty...@li...] On Behalf > Of Matt Herbert (matherbe) > Sent: Thursday, July 17, 2008 6:45 PM > To: Thomas Heller; cty...@li... > Subject: Re: [ctypes-users] crypto api > > > Hey all, > > > > > > > typedef struct _CRYPTOAPI_BLOB { > > > DWORD cbData; > > > BYTE *pbData; > > > } > > > > > > and here is my ctypes translation of it: > > > > > > class _CRYPTOAPI_BLOB(ctypes.Structure): > > > _fields_ = [ > > > ('cbData', ctypes.wintypes.DWORD), > > > ('pbData', ctypes.POINTER(ctypes.wintypes.BYTE))] > > > > > So, now I've moved along a little further, thanks to Thomas. I am now > calling a function that returns one of these blobs. I can't figure out > how to access the data that is stored in the pbData member. When I try > to access it like this: > > >>> print blob.cbData > 17 > >>> print blob.pbData.contents > c_byte(4) > > The cbData member contains the length of data stored in the pbData > member. But I can't figure out how to get to the other 16 > bytes of data > when all I have is a c_byte (no array). I'm assuming this is where I > would normally use pointer arithmetic?? > > -Matt > Ok, so I've found a way, but it seems a bit hacky: # Get the blob for the certificate serial number sn = self._pCertContext.contents.pCertInfo.contents.SerialNumber datastr = ctypes.string_at(sn.pbData, sn.cbData) # now we have str of binary data, convert to an array of hex numbers sn_array = [] for i in range(sn.cbData): data.append("%02x" %ord(sn_array[i])) # For some reason MS little-endian, so we need to reverse the order sn_array.reverse() # Final serial number serial_number = " ".join(sn_array) Is there something like "array_at" that I am missing? Thanks -Matt |