def __init__(self, key, msg = None, digestmod = None): """Create a new HMAC object. key: key for the keyed hash object. msg: Initial input for the hash, if provided. digestmod: A module hash type string """ from hashlib import new as newhash if digestmod is None: digestmod = 'md5' self.outer = newhash(digestmod) self.inner = newhash(digestmod) self.digest_size = self.inner.digest_size blocksize = self.inner.block_size if len(key) > blocksize: key = self.newhash(digestmod).update(key).digest() key = key + chr(0) * (blocksize - len(key)) self.outer.update(key.translate(trans_5C)) self.inner.update(key.translate(trans_36)) if msg is not None: self.update(msg)
def verifyFile(filePath, fileLength, checksums): actualLength = stat(filePath).st_size if actualLength != fileLength: raise IOError('Expected length %d, actual length %d' % (fileLength, actualLength)) hashers = {} for algo in checksums.iterkeys(): try: hashers[algo] = newhash(algo) except ValueError, ex: raise IOError('Failed to create "%s" hasher: %s' % (algo, ex))
def verifyFile(filePath, fileLength, checksums): actualLength = stat(filePath).st_size if actualLength != fileLength: raise IOError( 'Expected length %d, actual length %d' % (fileLength, actualLength) ) hashers = {} for algo in checksums.iterkeys(): try: hashers[algo] = newhash(algo) except ValueError, ex: raise IOError('Failed to create "%s" hasher: %s' % (algo, ex))
def verifyFile(filePath, fileLength, checksums): actualLength = stat(filePath).st_size if actualLength != fileLength: raise IOError('Expected length %d, actual length %d' % (fileLength, actualLength)) hashers = {} for algo in checksums.keys(): try: hashers[algo] = newhash(algo) except ValueError as ex: raise IOError('Failed to create "%s" hasher: %s' % (algo, ex)) bufSize = 16384 with open(filePath, 'rb') as inp: while True: buf = inp.read(bufSize) if not buf: break for hasher in hashers.values(): hasher.update(buf) for algo, hasher in sorted(hashers.items()): if checksums[algo] != hasher.hexdigest(): raise IOError('%s checksum mismatch' % algo)
v = input(MSG_SEL_ALG) if v == '': exit(MSG_NO_ALG) try: v = int(v) if 0 < v < choice_len: chosen_algorithm = list(algorithms_guaranteed)[v - 1] except ValueError: # type(v) is str if v in algorithms_guaranteed: chosen_algorithm = v setattr(args, 'f', chosen_algorithm) # prompt signature if args[ARG_SIGN]: signature = args[ARG_SIGN] elif args[ARG_INPUT]: with open(args[ARG_INPUT]) as f: signature = f.read().replace('\n', '') else: signature = input(MSG_SIGN) # verify with open(args[ARG_FILE], 'rb') as f: h = newhash(args[ARG_A]) h.update(f.read()) print(MSG_MATCH if h.hexdigest() == signature else MSG_NO_MATCH)