WIP: writing files but they are malformed...investigating
This commit is contained in:
parent
f1d7a07257
commit
3da723c89d
|
@ -4,22 +4,69 @@
|
||||||
class JpgBinWrite:
|
class JpgBinWrite:
|
||||||
|
|
||||||
soi_marker = 0xffd8
|
soi_marker = 0xffd8
|
||||||
|
eof_marker = 0xffd9
|
||||||
picseal_marker = 0xffe0
|
picseal_marker = 0xffe0
|
||||||
|
|
||||||
def __init__(self):
|
#
|
||||||
pass
|
def __init__(self, jpg_in, jpg_out):
|
||||||
|
self.fhr = jpg_in
|
||||||
|
self.fhw = jpg_out
|
||||||
def writeJpgPicSeal(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def writeJpgMetadata(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
def writeJpgImgData(self):
|
# input is the Crypto Sig class
|
||||||
pass
|
#
|
||||||
|
def writeJpgPicSealPub(self, crypto_sig, fp):
|
||||||
|
self.__writeJpgHeader()
|
||||||
|
|
||||||
|
# write picseal serialize data
|
||||||
|
|
||||||
|
self.__writeJpgImg(fp)
|
||||||
|
|
||||||
|
#
|
||||||
|
# input is the Crypto Sig class
|
||||||
|
#
|
||||||
|
def writeJpgPicSealPvt(self, crypto_sig, fp):
|
||||||
|
self.__writeJpgHeader()
|
||||||
|
|
||||||
|
# write picseal serialize data
|
||||||
|
|
||||||
|
self.__writeJpgImg(fp)
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
def __writeJpgImg(self, fp):
|
||||||
|
self.__writeJpgMetadata(fp.markers_meta)
|
||||||
|
self.__writeJpgImgData(fp.markers_img)
|
||||||
|
self.__writeJpgFooter()
|
||||||
|
self.fhw.flush()
|
||||||
|
self.fhw.close()
|
||||||
|
|
||||||
|
#
|
||||||
|
# array of the marker information
|
||||||
|
#
|
||||||
|
def __writeJpgMetadata(self, markers):
|
||||||
|
for marker in markers:
|
||||||
|
self.fhr.seek(marker.fpos)
|
||||||
|
data = self.fhr.read(marker.len)
|
||||||
|
self.fhw.write(data)
|
||||||
|
|
||||||
|
#
|
||||||
|
# array of the marker information
|
||||||
|
#
|
||||||
|
def __writeJpgImgData(self, markers):
|
||||||
|
for marker in markers:
|
||||||
|
# most of the image data is sequential
|
||||||
|
cpos = self.fhr.tell()
|
||||||
|
if (marker.fpos != cpos):
|
||||||
|
self.fhr.seek(marker.fpos)
|
||||||
|
|
||||||
|
data = self.fhr.read(marker.len)
|
||||||
|
self.fhw.write(data)
|
||||||
|
|
||||||
|
#
|
||||||
|
def __writeJpgHeader(self):
|
||||||
|
self.fhw.write(bytes(JpgBinWrite.soi_marker))
|
||||||
|
|
||||||
|
#
|
||||||
|
def __writeJpgFooter(self):
|
||||||
|
self.fhw.write(bytes(JpgBinWrite.eof_marker))
|
||||||
|
|
|
@ -58,9 +58,7 @@ class JpgMarker:
|
||||||
self.len = mlen
|
self.len = mlen
|
||||||
self.type = mstr
|
self.type = mstr
|
||||||
|
|
||||||
|
#
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "[{}] {} {}(len) {}(fpos)".format(self.type, self.hexstr, self.len, self.fpos)
|
return "[{}] {} {}(len) {}(fpos)".format(self.type, self.hexstr, self.len, self.fpos)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,19 @@
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
import os
|
||||||
import logging
|
import logging
|
||||||
# from PIL import Image
|
|
||||||
from libs.jpg_bin import JpgBin
|
from libs.jpg_bin import JpgBin
|
||||||
from libs.crypto_pub import Signature
|
from libs.jpg_bin_w import JpgBinWrite
|
||||||
|
from libs.crypto_sig import Signature
|
||||||
|
from libs.toolbox import Toolbox
|
||||||
|
|
||||||
|
|
||||||
class JpgProc:
|
class JpgProc:
|
||||||
|
|
||||||
|
PSPUB = "PSPUB"
|
||||||
|
PSPVT = "PSPVT"
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.fh = None
|
self.fh = None
|
||||||
self.fn = ""
|
self.fn = ""
|
||||||
|
@ -19,7 +24,7 @@ class JpgProc:
|
||||||
#
|
#
|
||||||
# process a jpg file, read only
|
# process a jpg file, read only
|
||||||
#
|
#
|
||||||
def jpgProc(self, fname):
|
def process(self, fname):
|
||||||
self.fh = open(fname, "rb")
|
self.fh = open(fname, "rb")
|
||||||
self.fn = fname
|
self.fn = fname
|
||||||
self.jpg = JpgBin()
|
self.jpg = JpgBin()
|
||||||
|
@ -31,7 +36,7 @@ class JpgProc:
|
||||||
#
|
#
|
||||||
# hash the jpg image data
|
# hash the jpg image data
|
||||||
#
|
#
|
||||||
def jpgHash(self):
|
def hash(self):
|
||||||
if (not self.is_processed):
|
if (not self.is_processed):
|
||||||
return 0xdeadbeef
|
return 0xdeadbeef
|
||||||
|
|
||||||
|
@ -41,10 +46,31 @@ class JpgProc:
|
||||||
logging.info("img_hash-size=={}, img_hash=={}".format(len(img_hash), img_hash))
|
logging.info("img_hash-size=={}, img_hash=={}".format(len(img_hash), img_hash))
|
||||||
return img_hash
|
return img_hash
|
||||||
|
|
||||||
|
#
|
||||||
def writePicSealJpg(self, fname=None):
|
def writePicSealJpg(self, fname=None):
|
||||||
if not fname:
|
if not fname:
|
||||||
pass
|
fname = self.fn
|
||||||
pass
|
|
||||||
|
(fname_pub, fname_pvt) = JpgProc.__getFileNames(fname)
|
||||||
|
|
||||||
|
# Write a new JPG PicSeal file w/ Public Key
|
||||||
|
self.fh.seek(0)
|
||||||
|
fhw = open(fname_pub, "xb")
|
||||||
|
jpgw = JpgBinWrite(self.fh, fhw)
|
||||||
|
jpgw.writeJpgPicSealPub(self.sig, self.jpg.jpg_fp)
|
||||||
|
|
||||||
|
# Write a new JPG PicSeal file w/ Private Key
|
||||||
|
self.fh.seek(0)
|
||||||
|
fhw = open(fname_pvt, "xb")
|
||||||
|
jpgw = JpgBinWrite(self.fh, fhw)
|
||||||
|
jpgw.writeJpgPicSealPvt(self.sig, self.jpg.jpg_fp)
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
def __getFileNames(image_fn):
|
||||||
|
basename = os.path.basename(image_fn)
|
||||||
|
(filename, ext) = Toolbox.parseFilenameIncExt(basename)
|
||||||
|
pubFileName = filename + '_' + JpgProc.PSPUB + '_' + Toolbox.getTimestampStr() + ext
|
||||||
|
pvtFileName = filename + '_' + JpgProc.PSPVT + '_' + Toolbox.getTimestampStr() + ext
|
||||||
|
return (pubFileName, pvtFileName)
|
||||||
|
|
||||||
|
|
36
picseal.py
36
picseal.py
|
@ -1,18 +1,16 @@
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
import os
|
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
from shutil import copyfile
|
from shutil import copyfile
|
||||||
#from subprocess import Popen, PIPE, check_call
|
|
||||||
from libs.toolbox import Toolbox
|
from libs.toolbox import Toolbox
|
||||||
from libs.jpg_proc import JpgProc
|
from libs.jpg_proc import JpgProc
|
||||||
|
|
||||||
printall = False
|
printall = False
|
||||||
printmeta = False
|
printmeta = False
|
||||||
printimage = False
|
printimage = False
|
||||||
|
write_picseal = False
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parseArgs()
|
parseArgs()
|
||||||
|
@ -23,31 +21,24 @@ def main():
|
||||||
# export signature & public key to a new image file
|
# export signature & public key to a new image file
|
||||||
def processImage(image_fn):
|
def processImage(image_fn):
|
||||||
jpg = JpgProc()
|
jpg = JpgProc()
|
||||||
jpg.getProc(image_fn)
|
jpg.process(image_fn)
|
||||||
img_hash = jpg.jpgHash()
|
jpg.hash()
|
||||||
|
if (write_picseal):
|
||||||
printImageInfo(jpg_bin)
|
print("Writing PicSeal JPG files...")
|
||||||
|
jpg.writePicSealJpg()
|
||||||
|
|
||||||
|
printImageInfo(jpg)
|
||||||
|
|
||||||
#
|
#
|
||||||
def copyImage(image_fn):
|
def printImageInfo(jpg_bin):
|
||||||
basename = os.path.basename(image_fn)
|
|
||||||
(filename, ext) = Toolbox.parseFilenameIncExt(basename)
|
|
||||||
pubFileName = filename + '_' + Toolbox.getTimestampStr() + ext
|
|
||||||
privFileName = filename + '_' + Toolbox.getTimestampStr() + ext
|
|
||||||
copyfile(image_fn, newFileName)
|
|
||||||
return (pubFileName, privFileName)
|
|
||||||
|
|
||||||
#
|
|
||||||
def printImageInfo(jpg_bin2):
|
|
||||||
if (printall):
|
if (printall):
|
||||||
print( str(jpg_bin2) )
|
print( str(jpg_bin) )
|
||||||
|
|
||||||
if (printimage):
|
if (printimage):
|
||||||
print( jpg_bin2.printMarkerImg())
|
print( jpg_bin.printMarkerImg())
|
||||||
|
|
||||||
if (printmeta):
|
if (printmeta):
|
||||||
print( jpg_bin2.printMarkerMeta())
|
print( jpg_bin.printMarkerMeta())
|
||||||
|
|
||||||
|
|
||||||
def parseArgs():
|
def parseArgs():
|
||||||
|
@ -62,6 +53,7 @@ def parseArgs():
|
||||||
parser.add_argument('-pm', '--printmeta', action='store_true', help="print the metadata markers")
|
parser.add_argument('-pm', '--printmeta', action='store_true', help="print the metadata markers")
|
||||||
parser.add_argument('-pi', '--printimage', action='store_true', help="print the image markers")
|
parser.add_argument('-pi', '--printimage', action='store_true', help="print the image markers")
|
||||||
parser.add_argument('-pa', '--printall', action='store_true', help="print all markers")
|
parser.add_argument('-pa', '--printall', action='store_true', help="print all markers")
|
||||||
|
parser.add_argument('-w', '--write', action='store_true', help="write picseal files")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if (args.logging):
|
if (args.logging):
|
||||||
|
@ -86,6 +78,10 @@ def parseArgs():
|
||||||
global printimage
|
global printimage
|
||||||
printimage = True
|
printimage = True
|
||||||
|
|
||||||
|
if (args.write):
|
||||||
|
global write_picseal
|
||||||
|
write_picseal = True
|
||||||
|
|
||||||
if (args.file):
|
if (args.file):
|
||||||
processImage(args.file)
|
processImage(args.file)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue