From 3da723c89d9fd0c0c091d01e15de9b7a67ffeb1e Mon Sep 17 00:00:00 2001 From: JohnE Date: Fri, 14 Oct 2016 18:35:41 -0700 Subject: [PATCH] WIP: writing files but they are malformed...investigating --- libs/jpg_bin_w.py | 73 ++++++++++++++++++++++++++++++++++++++--------- libs/jpg_fp.py | 4 +-- libs/jpg_proc.py | 40 +++++++++++++++++++++----- picseal.py | 36 +++++++++++------------ 4 files changed, 110 insertions(+), 43 deletions(-) diff --git a/libs/jpg_bin_w.py b/libs/jpg_bin_w.py index 13830f6..731214a 100644 --- a/libs/jpg_bin_w.py +++ b/libs/jpg_bin_w.py @@ -4,22 +4,69 @@ class JpgBinWrite: soi_marker = 0xffd8 + eof_marker = 0xffd9 picseal_marker = 0xffe0 - def __init__(self): - pass - - - def writeJpgPicSeal(self): - pass - - - def writeJpgMetadata(self): - pass - + # + def __init__(self, jpg_in, jpg_out): + self.fhr = jpg_in + self.fhw = jpg_out # - def writeJpgImgData(self): - pass + # input is the Crypto Sig class + # + 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)) diff --git a/libs/jpg_fp.py b/libs/jpg_fp.py index 8436c34..2a10c54 100644 --- a/libs/jpg_fp.py +++ b/libs/jpg_fp.py @@ -58,9 +58,7 @@ class JpgMarker: self.len = mlen self.type = mstr - + # def __repr__(self): return "[{}] {} {}(len) {}(fpos)".format(self.type, self.hexstr, self.len, self.fpos) - - diff --git a/libs/jpg_proc.py b/libs/jpg_proc.py index b374c66..8e7613e 100644 --- a/libs/jpg_proc.py +++ b/libs/jpg_proc.py @@ -1,14 +1,19 @@ # # # +import os import logging -# from PIL import Image 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: + PSPUB = "PSPUB" + PSPVT = "PSPVT" + def __init__(self): self.fh = None self.fn = "" @@ -19,7 +24,7 @@ class JpgProc: # # process a jpg file, read only # - def jpgProc(self, fname): + def process(self, fname): self.fh = open(fname, "rb") self.fn = fname self.jpg = JpgBin() @@ -31,7 +36,7 @@ class JpgProc: # # hash the jpg image data # - def jpgHash(self): + def hash(self): if (not self.is_processed): return 0xdeadbeef @@ -41,10 +46,31 @@ class JpgProc: logging.info("img_hash-size=={}, img_hash=={}".format(len(img_hash), img_hash)) return img_hash - + # def writePicSealJpg(self, fname=None): if not fname: - pass - pass + fname = self.fn + + (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) + diff --git a/picseal.py b/picseal.py index 3e67841..1c1c11f 100644 --- a/picseal.py +++ b/picseal.py @@ -1,18 +1,16 @@ # # # -import os import argparse import logging from shutil import copyfile -#from subprocess import Popen, PIPE, check_call from libs.toolbox import Toolbox from libs.jpg_proc import JpgProc printall = False printmeta = False printimage = False - +write_picseal = False def main(): parseArgs() @@ -23,31 +21,24 @@ def main(): # export signature & public key to a new image file def processImage(image_fn): jpg = JpgProc() - jpg.getProc(image_fn) - img_hash = jpg.jpgHash() - - printImageInfo(jpg_bin) + jpg.process(image_fn) + jpg.hash() + if (write_picseal): + print("Writing PicSeal JPG files...") + jpg.writePicSealJpg() + printImageInfo(jpg) # -def copyImage(image_fn): - 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): +def printImageInfo(jpg_bin): if (printall): - print( str(jpg_bin2) ) + print( str(jpg_bin) ) if (printimage): - print( jpg_bin2.printMarkerImg()) + print( jpg_bin.printMarkerImg()) if (printmeta): - print( jpg_bin2.printMarkerMeta()) + print( jpg_bin.printMarkerMeta()) def parseArgs(): @@ -62,6 +53,7 @@ def parseArgs(): 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('-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() if (args.logging): @@ -86,6 +78,10 @@ def parseArgs(): global printimage printimage = True + if (args.write): + global write_picseal + write_picseal = True + if (args.file): processImage(args.file) else: