Advertisement
k98kurz

selfhash.py

May 28th, 2022 (edited)
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1. #!/bin/python
  2. from hashlib import sha256
  3. from sys import argv
  4.  
  5.  
  6. FILE_HASH = '23a11c611ff4f4e04dd5ccd17fc297ad6b38dd9a295a212d2bdee0ba53cb8b27'
  7.  
  8.  
  9. def initial_hash(filename: str) -> str:
  10.     """Calculates the initial hash of this file."""
  11.     with open(filename, 'rb') as f:
  12.         return sha256(f.read()).digest().hex()
  13.  
  14.  
  15. def final_hash(filename: str) -> str:
  16.     """Calculates the hash of this file without the FILE_HASH line."""
  17.     with open(filename, 'rb') as f:
  18.         # parse file into lines
  19.         contents = f.read()
  20.         lines = contents.split(b'\n')
  21.         line_index = ''
  22.  
  23.         # find the FILE_HASH line
  24.         for i, l in enumerate(lines):
  25.             if l[:9] == b'FILE_HASH':
  26.                 line_index = i
  27.                 break
  28.  
  29.         # remove the FILE_HASH line
  30.         lines.remove(lines[line_index])
  31.         contents = b'\n'.join(lines)
  32.  
  33.         # hash resulting contents
  34.         return sha256(contents).digest().hex()
  35.  
  36.  
  37. def persist_hash(filename: str) -> None:
  38.     """Overwrite the FILE_HASH line with the result of final_hash."""
  39.     # calculate correct hash
  40.     digest = final_hash(filename)
  41.  
  42.     # open file
  43.     with open(filename, 'rb') as f:
  44.         # parse file
  45.         contents = f.read()
  46.         lines = contents.split(b'\n')
  47.         line_index = ''
  48.  
  49.         # find correct line to replace
  50.         for i, l in enumerate(lines):
  51.             if l[:9] == b'FILE_HASH':
  52.                 line_index = i
  53.                 break
  54.  
  55.         # replace line
  56.         lines[line_index] = b'FILE_HASH = \'' + bytes(digest, 'utf-8') + b'\''
  57.         contents = b'\n'.join(lines)
  58.  
  59.         # write contents
  60.         with open(filename, 'wb') as f2:
  61.             f2.write(contents)
  62.  
  63.  
  64. def verify_hash(filename: str) -> bool:
  65.     """Verify that the hash specified in the FILE_HASH is valid."""
  66.     digest = final_hash(filename)
  67.     return digest == FILE_HASH
  68.  
  69.  
  70. def license():
  71.     """Copyleft (c) 2022 k98kurz
  72.  
  73.        Permission to use, copy, modify, and/or distribute this software
  74.        for any purpose with or without fee is hereby granted, provided
  75.        that the above copyleft notice and this permission notice appear in
  76.        all copies.
  77.  
  78.        THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  79.        WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  80.        WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  81.        AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  82.        CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  83.        OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  84.        NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  85.        CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  86.    """
  87.     return license.__doc__
  88.  
  89.  
  90. def main(args: list):
  91.     filename = args[0]
  92.     mode = args[1] if len(args) > 1 else 'help'
  93.  
  94.     if mode == 'help':
  95.         print(f'usage: python {filename} [print|persist|verify]')
  96.     elif mode == 'print':
  97.         print(f'initial hash: {initial_hash(filename)}')
  98.         print(f'final hash: {final_hash(filename)}')
  99.     elif mode == 'persist':
  100.         persist_hash(filename)
  101.     elif mode == 'verify':
  102.         if verify_hash(filename):
  103.             print('verified')
  104.         else:
  105.             print('invalidated')
  106.  
  107.  
  108. if __name__ == '__main__':
  109.     main(argv)
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement