optimize code for hash signed data generation

Signed-off-by: lidanyang <lidanyang12@huawei.com>
This commit is contained in:
lidanyang 2023-03-03 17:23:38 +08:00
parent 8fa169b228
commit 9633b04e94
2 changed files with 15 additions and 2 deletions

View File

@ -32,6 +32,9 @@ def sign_func_sha256(sign_file, private_key_file):
while chunk := file.read(BLCOK_SIZE):
hash_sha256.update(chunk)
signature = sign_digest(hash_sha256.digest(), private_key_file)
if signature == False:
UPDATE_LOGGER.print_log("sign digest failed", log_type=UPDATE_LOGGER.ERROR_LOG)
return ""
return str(b64encode(signature).decode("ascii"))
@ -59,5 +62,11 @@ def generate_signed_data(file_lists, sign_func, private_key_file):
UPDATE_LOGGER.print_log("signed file can't be more than %d" % max_file_num,
log_type=UPDATE_LOGGER.ERROR_LOG)
return ""
return "\n".join(["Name: {}\nsigned-data: {}\n".format(
name, sign_func(file, private_key_file)) for (file, name) in file_lists])
sign_res_list = []
for file, name in file_lists:
sign_res = sign_func(file, private_key_file)
if sign_res == "":
UPDATE_LOGGER.print_log("sign file {} failed".format(name), log_type=UPDATE_LOGGER.ERROR_LOG)
return ""
sign_res_list += ["Name: {}\nsigned-data: {}\n".format(name, sign_res)]
return "\n".join(sign_res_list)

View File

@ -381,6 +381,10 @@ def create_build_tools_zip():
# add hash signed data to build_tools.zip
signed_data = generate_signed_data(files_to_sign, sign_func_sha256, OPTIONS_MANAGER.private_key)
if signed_data == "":
UPDATE_LOGGER.print_log("generate_signed_data failed", log_type=UPDATE_LOGGER.ERROR_LOG)
zip_file.close()
return False
zip_file.writestr("hash_signed_data", signed_data)
zip_file.close()
return file_obj