Splitting .bin file to get signature in Python 3

Viewed 19

I'm upgrading code from Python 2 to Python 3. It reads in a .bin file and splits it into a .sig and .img so that I can verify the signature with gpg. This works fine in Python 2, but in Python 3 the .sig and .img files don't seem to be split correctly. When I run gpg from root I can see this error:

/usr/bin/gpg --no-default-keyring --keyring /boot/EFI/BOOT/pubring.gpg --verify /tmp/gpg.sig /tmp/gpg.img gpg: packet(3) with unknown version 68
gpg: standalone signature of class 0x04
gpg: Signature made Thu Jan 1 18:46:24 1970 UTC
gpg: using ? key 06050263077F5200
gpg: Can't check signature: Invalid signature class

The working code in python2 is as follows:

    try:
        with open(file_src, mode='r') as file:
            fileOut = file.read()
        filelines = fileOut.splitlines()
        i = 0
        last = filelines[-1]
        for line in filelines:
            i += 1
            if ('test') in line:
                break
            if line is last:
                # No test mentioned in file, not valid
                return("","")

        endstring = fileOut.split(filelines[i-1])[1]
        sig = endstring[1:-64]
        image = fileOut.split(filelines[i])[0]
        return(sig,image)
    except:
        # Assume any failure here is because of an invalid file
        return("","")

in another function I write the files correctly and all is well

    sig = open(sig_file,'w')
    sig.write(gpg_sig)
    sig.close()
    img = open(img_file,'w')
    img.write(gpg_img)
    img.close()

In python 3 I only changed the read as there are now byte objects, I have experimented with different formats such as latin-1 and ISO-8859-1:

with open(file_src, mode='rb') as file:
        fileOut = file.read().decode('latin-1')

...

with open("file_src", mode='r',encoding='ISO-8859-1') as file:
        fileOut = file.read()

However it always leads to the bug at the top of this post. What am I doing wrong, how is it leading to a different result than Python 2?

0 Answers
Related