hashing.py 文件源码

python
阅读 36 收藏 0 点赞 0 评论 0

项目:DevOps 作者: YoLoveLife 项目源码 文件源码
def secure_hash(filename, hash_func=sha1):
    ''' Return a secure hash hex digest of local file, None if file is not present or a directory. '''

    if not os.path.exists(to_bytes(filename, errors='surrogate_or_strict')) or os.path.isdir(to_bytes(filename, errors='strict')):
        return None
    digest = hash_func()
    blocksize = 64 * 1024
    try:
        infile = open(to_bytes(filename, errors='surrogate_or_strict'), 'rb')
        block = infile.read(blocksize)
        while block:
            digest.update(block)
            block = infile.read(blocksize)
        infile.close()
    except IOError as e:
        raise AnsibleError("error while accessing the file %s, error was: %s" % (filename, e))
    return digest.hexdigest()

# The checksum algorithm must match with the algorithm in ShellModule.checksum() method
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号