# encoding: utf-8 # # Copyright 2009-2017 Greg Neagle. # # Licensed under the Apache License, Version 2.0 (the 'License'); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an 'AS IS' BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ munkihash.py Created by Greg Neagle on 2016-12-14. Munki's hash functions """ import hashlib import os def gethash(filename, hash_function): """ Calculates the hashvalue of the given file with the given hash_function. Args: filename: The file name to calculate the hash value of. hash_function: The hash function object to use, which was instanciated before calling this function, e.g. hashlib.md5(). Returns: The hashvalue of the given file as hex string. """ if not os.path.isfile(filename): return 'NOT A FILE' fileref = open(filename, 'rb') while 1: chunk = fileref.read(2**16) if not chunk: break hash_function.update(chunk) fileref.close() return hash_function.hexdigest() def getmd5hash(filename): """ Returns hex of MD5 checksum of a file """ hash_function = hashlib.md5() return gethash(filename, hash_function) def getsha256hash(filename): """ Returns the SHA-256 hash value of a file as a hex string. """ hash_function = hashlib.sha256() return gethash(filename, hash_function) if __name__ == '__main__': print 'This is a library of support tools for the Munki Suite.'