FLAGS_OFFSET HEADER_SIZE 10 gzip FCOMMENT 16 LEN_MAGIC_KEY 15 def sign

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
FLAGS_OFFSET = 3
HEADER_SIZE = 10
gzip.FCOMMENT = 16
LEN_MAGIC_KEY = 15
def sign_archive(pkey, cert, package, **kwargs) -> None:
with gzip.open(package) as fs:
signature = sign(pkey=pkey, data=iter(partial(fs.read, 64000), b""), digest="SHA1") # массив байтов
with open(package, "ab") as fs:
fs.write(make_gzip_comment_frame(signature))
def make_gzip_comment_frame(comment):
comment = base64.b64encode(comment)
magic_key = "SIGNATURE{:06}".format(len(comment))
comment_len = bytes(magic_key, "utf8")
buf = bytearray(gzip.compress(b''))
buf[FLAGS_OFFSET] |= gzip.FCOMMENT
buf[HEADER_SIZE:HEADER_SIZE] = comment + comment_len + b'\0'
return buf
def get_sig(archive):
with open(archive, "rb") as fs:
fs.seek(-11 - LEN_MAGIC_KEY, os.SEEK_END)
signature_len = int(fs.read(LEN_MAGIC_KEY)[-6:])
fs.seek(-11 - LEN_MAGIC_KEY - signature_len, os.SEEK_END)
signature_base64 = fs.read(signature_len)
signature = base64.b64decode(signature_base64)
return signature