计算apk的checksum方法
方法1:
def test_count_apk_checksum(server_checksum):
print('\ntest_count_apk_checksum===')
with open('c_download/app_download.apk', 'rb') as f:
m = hashlib.new('SHA256', f.read()).hexdigest() # read file by file object
if server_checksum == m:
print('server_checksum: ', server_checksum)
print('count_checksum: ', m)
print('app download success')
else:
print('server_checksum: ', server_checksum)
print('count_checksum: ', m)
print('app download failed')
方法2:
def test_count_apk_checksum(server_checksum):
print('\ntest_count_apk_checksum===')
with open('c_download/app_download.apk', 'rb') as f:
m = hashlib.sha256(f.read()).hexdigest() # read file by file object
if server_checksum == m:
print('server_checksum: ', server_checksum)
print('count_checksum: ', m)
print('app download success')
else:
print('server_checksum: ', server_checksum)
print('count_checksum: ', m)
print('app download failed')
方法3:
def test_count_apk_checksum(server_checksum):
print('\ntest_count_apk_checksum===')
with open('c_download/app_download.apk', 'rb') as f:
m = hashlib.sha256()
m.update(f.read())
value_checksum = m.hexdigest()
if server_checksum == value_checksum :
print('server_checksum: ', server_checksum)
print('count_checksum: ', value_checksum )
print('app download success')
else:
print('server_checksum: ', server_checksum)
print('count_checksum: ', value_checksum )
print('app download failed')
注意:
1.如果读取文件,不能直接放入文件的路径进行读入,否则计算的checksum值会是错误的,必须换成file的对象来读取
如:
def test_count_apk_checksum(server_checksum):
print('\ntest_count_apk_checksum===')
with open('c_download/app_download.apk', 'rb') as f:
m = hashlib.new('SHA256', **b'c_download/app_download.apk'**).hexdigest() # this method is not fit for reading file
# m = hashlib.sha256(**'c_download/app_download.apk'**.encode()).hexdigest() # this method is not fit for reading file
if server_checksum == m:
print('server_checksum: ', server_checksum)
print('count_checksum: ', m)
print('app download success')
else:
print('server_checksum: ', server_checksum)
print('count_checksum: ', m)
print('app download failed')