Advertisement
DeaD_EyE

file signature checker example

Nov 23rd, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. import pathlib
  2. import glob
  3.  
  4.  
  5. def check(pattern):
  6.     signatures = {
  7.         'jpg': bytes([255, 216, 255]),
  8.         'png': bytes([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]),
  9.         }
  10.     read_size = max(len(sig) for sig in signatures.values())
  11.     for file in glob.glob(pattern):
  12.         file = pathlib.Path(file)
  13.         if not file.is_file():
  14.             continue
  15.         with file.open('rb') as fd:
  16.             data = fd.read(read_size)
  17.         for extension, signature in signatures.items():
  18.             if data.startswith(signature):
  19.                 print('{} is a {} file.'.format(file, extension))
  20.  
  21.  
  22. check('Downloads/*')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement