move sha256sum() and sha256base64() to common

This commit is contained in:
Hans-Christoph Steiner 2021-05-12 09:43:48 +02:00
parent 0fbd04f1c2
commit 7a7ecbf9dc
No known key found for this signature in database
GPG key ID: 3E177817BA1B9BFA
2 changed files with 30 additions and 30 deletions

View file

@ -58,6 +58,7 @@ try:
except ImportError: except ImportError:
import xml.etree.ElementTree as XMLElementTree # nosec this is a fallback only import xml.etree.ElementTree as XMLElementTree # nosec this is a fallback only
from base64 import urlsafe_b64encode
from binascii import hexlify from binascii import hexlify
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from distutils.version import LooseVersion from distutils.version import LooseVersion
@ -3964,3 +3965,27 @@ def run_yamllint(path, indent=0):
for problem in problems: for problem in problems:
result.append(' ' * indent + path + ':' + str(problem.line) + ': ' + problem.message) result.append(' ' * indent + path + ':' + str(problem.line) + ': ' + problem.message)
return '\n'.join(result) return '\n'.join(result)
def sha256sum(filename):
'''Calculate the sha256 of the given file'''
sha = hashlib.sha256()
with open(filename, 'rb') as f:
while True:
t = f.read(16384)
if len(t) == 0:
break
sha.update(t)
return sha.hexdigest()
def sha256base64(filename):
'''Calculate the sha256 of the given file as URL-safe base64'''
hasher = hashlib.sha256()
with open(filename, 'rb') as f:
while True:
t = f.read(16384)
if len(t) == 0:
break
hasher.update(t)
return urlsafe_b64encode(hasher.digest()).decode()

View file

@ -36,7 +36,6 @@ import yaml
import copy import copy
from datetime import datetime from datetime import datetime
from argparse import ArgumentParser from argparse import ArgumentParser
from base64 import urlsafe_b64encode
try: try:
from yaml import CSafeLoader as SafeLoader from yaml import CSafeLoader as SafeLoader
except ImportError: except ImportError:
@ -590,30 +589,6 @@ def get_icon_bytes(apkzip, iconsrc):
return apkzip.read(iconsrc.encode('utf-8').decode('cp437')) return apkzip.read(iconsrc.encode('utf-8').decode('cp437'))
def sha256sum(filename):
'''Calculate the sha256 of the given file'''
sha = hashlib.sha256()
with open(filename, 'rb') as f:
while True:
t = f.read(16384)
if len(t) == 0:
break
sha.update(t)
return sha.hexdigest()
def sha256base64(filename):
'''Calculate the sha256 of the given file as URL-safe base64'''
hasher = hashlib.sha256()
with open(filename, 'rb') as f:
while True:
t = f.read(16384)
if len(t) == 0:
break
hasher.update(t)
return urlsafe_b64encode(hasher.digest()).decode()
def has_known_vulnerability(filename): def has_known_vulnerability(filename):
"""checks for known vulnerabilities in the APK """checks for known vulnerabilities in the APK
@ -723,7 +698,7 @@ def insert_obbs(repodir, apps, apks):
obbWarnDelete(f, _('OBB file has newer versionCode({integer}) than any APK:') obbWarnDelete(f, _('OBB file has newer versionCode({integer}) than any APK:')
.format(integer=str(versionCode))) .format(integer=str(versionCode)))
continue continue
obbsha256 = sha256sum(f) obbsha256 = common.sha256sum(f)
obbs.append((packagename, versionCode, obbfile, obbsha256)) obbs.append((packagename, versionCode, obbfile, obbsha256))
for apk in apks: for apk in apks:
@ -1267,7 +1242,7 @@ def insert_localized_app_metadata(apps):
if not os.path.samefile(f, basepath): if not os.path.samefile(f, basepath):
os.unlink(f) os.unlink(f)
else: else:
sha256 = sha256base64(f) sha256 = common.sha256base64(f)
filename = base + '_' + sha256 + '.' + extension filename = base + '_' + sha256 + '.' + extension
index_file = os.path.join(os.path.dirname(f), filename) index_file = os.path.join(os.path.dirname(f), filename)
if not os.path.exists(index_file): if not os.path.exists(index_file):
@ -1313,7 +1288,7 @@ def scan_repo_files(apkcache, repodir, knownapks, use_date_from_file=False):
raise FDroidException(_('{path} is zero size!') raise FDroidException(_('{path} is zero size!')
.format(path=filename)) .format(path=filename))
shasum = sha256sum(filename) shasum = common.sha256sum(filename)
usecache = False usecache = False
if name_utf8 in apkcache: if name_utf8 in apkcache:
repo_file = apkcache[name_utf8] repo_file = apkcache[name_utf8]
@ -1378,7 +1353,7 @@ def scan_apk(apk_file):
:return A dict containing APK metadata :return A dict containing APK metadata
""" """
apk = { apk = {
'hash': sha256sum(apk_file), 'hash': common.sha256sum(apk_file),
'hashType': 'sha256', 'hashType': 'sha256',
'uses-permission': [], 'uses-permission': [],
'uses-permission-sdk-23': [], 'uses-permission-sdk-23': [],
@ -1613,7 +1588,7 @@ def process_apk(apkcache, apkfilename, repodir, knownapks, use_date_from_apk=Fal
usecache = False usecache = False
if apkfilename in apkcache: if apkfilename in apkcache:
apk = apkcache[apkfilename] apk = apkcache[apkfilename]
if apk.get('hash') == sha256sum(apkfile): if apk.get('hash') == common.sha256sum(apkfile):
logging.debug(_("Reading {apkfilename} from cache") logging.debug(_("Reading {apkfilename} from cache")
.format(apkfilename=apkfilename)) .format(apkfilename=apkfilename))
usecache = True usecache = True