Python implements a method for calculating the CRC value of the resource icon

  • 2020-04-02 14:11:44
  • OfStack

This article is an example of how to calculate the CRC value of the resource icon in python. Specific methods are as follows:

The key to this functionality is to parse the resource information, find the data for the icon, and then calculate the CRC for that data

The specific implementation code is as follows:


  def _get_iconcrc(self, file_path): 
    """ 
    Generates the crc32 hash of the icon of the file. 
    @return: str, the str value of the file's icon 
    """ 
    iconData = "" 
 
    mype = pefile.PE(file_path) 
    if hasattr(mype, "DIRECTORY_ENTRY_RESOURCE"): 
      resIcons = filter(lambda x: x.id==pefile.RESOURCE_TYPE['RT_ICON'], mype.DIRECTORY_ENTRY_RESOURCE.entries) 
      if len(resIcons)>0: 
        resIcons = resIcons[0] 
        if hasattr(resIcons, "directory"): 
          for resId in resIcons.directory.entries: 
            if hasattr(resId, 'directory'): 
              for resLang in resId.directory.entries: 
                iconData += mype.get_data(resLang.data.struct.OffsetToData, resLang.data.struct.Size) 
     
    if not iconData: 
      print "not iconData" 
      return None 
    else: 
      return self._crc32(iconData) 

I hope this article has helped you with your Python programming.


Related articles: