from hashlib import sha1 import gzip with open('phar.phar', 'rb') as file: f = file.read() s = f[:-28] # 获取要签名的数据 h = f[-8:] # 获取签名类型以及GBMB标识 new_file = s + sha1(s).digest() + h # 数据 + 签名 + (类型 + GBMB) f_gzip = gzip.GzipFile("phar.gz", "wb") f_gzip.write(new_file) f_gzip.close()