本地计算文件的保管箱哈希 [英] Locally calculate dropbox hash of files

查看:71
本文介绍了本地计算文件的保管箱哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dropbox rest api,在metatada函数中有一个名为哈希的参数 https:/ /www.dropbox.com/developers/reference/api#metadata

Dropbox rest api, in function metatada has a parameter named "hash" https://www.dropbox.com/developers/reference/api#metadata

我可以在不调用任何远程api rest函数的情况下在本地计算此哈希值吗?

Can I calculate this hash locally without call any remote api rest function?

我需要知道此值以减少上传带宽。

I need know this value to reduce upload bandwidth.

推荐答案

https://www.dropbox.com/developers/reference/content-hash 解释了如何Dropbox计算其文件哈希。以下是Python的实现:

https://www.dropbox.com/developers/reference/content-hash explains how Dropbox computes their file hashes. A Python implementation of this is below:

import hashlib
import math
import os

DROPBOX_HASH_CHUNK_SIZE = 4*1024*1024

def compute_dropbox_hash(filename):
    file_size = os.stat(filename).st_size
    with open(filename, 'rb') as f:
        block_hashes = b''
        while True:
            chunk = f.read(DROPBOX_HASH_CHUNK_SIZE)
            if not chunk:
                break
            block_hashes += hashlib.sha256(chunk).digest()
        return hashlib.sha256(block_hashes).hexdigest()

这篇关于本地计算文件的保管箱哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆