diff --git a/libs/crypto_pub.py b/libs/crypto_pub.py index 8a8381b..3430b8e 100644 --- a/libs/crypto_pub.py +++ b/libs/crypto_pub.py @@ -8,14 +8,14 @@ from Crypto.Hash import SHA512 class Signature: - key_data = None - pub_key = None - sig_data = None - hh = None # def __init__(self): - hh = SHA512.new() + key_data = None + pub_key = None + sig_data = None + hh = None + self.hh = SHA512.new() self.genKeys() # @@ -29,21 +29,6 @@ class Signature: verifier = PKCS1_v1_5.new(self.pub_key) return verifier.verify(hshh, bin_sig) - # - def genSig222(self, bin_data): - hshh = SHA512.new() - hshh.update(bin_data) - signer = PKCS1_v1_5.new(self.key_data) - self.sig_data = signer.sign(hshh) - return self.sig_data - - # - def verifySig222(self, bin_data, bin_sig): - hshh = SHA512.new() - hshh.update(bin_data) - verifier = PKCS1_v1_5.new(self.pub_key) - return verifier.verify(hshh, bin_sig) - # def genKeys(self): logging.info("Generating public keys...") @@ -52,6 +37,8 @@ class Signature: logging.debug("public key==\n{}".format(self.pub_key.exportKey('PEM'))) logging.debug("private key (and pub, other info)==\n{}".format(self.key_data.exportKey('PEM'))) + # + # Private key also includes public key data...SO ANNOYING! # def getPrivKeyPEM(self): return self.key_data.exportKey('PEM') diff --git a/libs/jpg_bin.py b/libs/jpg_bin.py index 9c7aeb8..6a1fa52 100644 --- a/libs/jpg_bin.py +++ b/libs/jpg_bin.py @@ -36,7 +36,7 @@ class JpgBin: self.data_idx = 0 self.data_len = 0 self.fh = None - self.hh = None + # self.hh = None self.continue_process = True @@ -81,19 +81,17 @@ class JpgBin: return False # - def genHash(self, file_h, hash_h): - self.hh = hash_h + def genHash(self, hash_h): + self.fh.seek(0) for marker in self.jpg_fp.markers_img: - pass + cpos = self.fh.tell() + if (marker.fpos != cpos): + self.fh.seek(marker.fpos) + buf = self.fh.read(marker.len) + hash_h.update(buf) - pass - - # - def genImgHash222(self): - self.hh.update(self.data_buf[self.data_idx:]) - while(self.continue_process): - self.__getMoreBytes() - self.hh.update(self.data_buf) + img_hash = hash_h.digest() + return img_hash # def findAllMarkers(self): diff --git a/libs/jpg_fp.py b/libs/jpg_fp.py index 2d434c5..5a65860 100644 --- a/libs/jpg_fp.py +++ b/libs/jpg_fp.py @@ -40,7 +40,7 @@ class JpgFingerprint: total = 0 for marker in markers: str += repr(marker) + "\n" - total += marker.marker_size + total += marker.len str += "[TOT ] bytes=={}".format(total) str += "\n" return str @@ -52,15 +52,15 @@ class JpgMarker: Marker Data Type """ def __init__(self, mhex, fpos, mlen, mstr): - self.marker_hex = mhex - self.marker_hexstr = self.marker_hex.to_bytes(2, 'big').hex() - self.marker_filepos = fpos - self.marker_size = mlen - self.marker_cat = mstr + self.hex = mhex + self.hexstr = self.hex.to_bytes(2, 'big').hex() + self.fpos = fpos + self.len = mlen + self.type = mstr def __repr__(self): - return "[{}] {} {}(len) {}(fpos)".format(self.marker_cat, self.marker_hexstr, self.marker_size, self.marker_filepos) + return "[{}] {} {}(len) {}(fpos)".format(self.type, self.hexstr, self.len, self.fpos) diff --git a/libs/jpg_picseal.py b/libs/jpg_picseal.py new file mode 100644 index 0000000..616289c --- /dev/null +++ b/libs/jpg_picseal.py @@ -0,0 +1,25 @@ +# +# Purpose: Write 2 new JPG image files => PicSeal JPG Files +# +# Data: public key, private key, image hash, image signature +# Has: JPG fingerprint +# +# +class JpgPicSeal: + + def __init_(self): + self.pubkey = None + self.pvtkey = None + self.imghash = None + self.imgsig = None + pass + + # + def writePicSealJpg(self, fname): + pass + + + # + def readPicSealJpg(self, fname): + pass + diff --git a/libs/jpg_tools.py b/libs/jpg_tools.py index 50219cd..ab8eea5 100644 --- a/libs/jpg_tools.py +++ b/libs/jpg_tools.py @@ -4,13 +4,16 @@ import logging # from PIL import Image from libs.jpg_bin import JpgBin +from libs.crypto_pub import Signature class JpgTools: def __init__(self): - self.fh = None - pass + self.fh = None + self.jpg = None + self.sig = None + self.is_processed = False # # process a jpg file, read only @@ -20,14 +23,18 @@ class JpgTools: self.jpg = JpgBin() retval = self.jpg.processFile(self.fh) logging.info("processFile()=={}".format(retval)) + self.is_processed = True return self.jpg # - # process a jpg file, create new jpg with crypto keys + # hash the jpg image data # def jpgHash(self): - self.fh = open(fname, "rb") - self.jpg = JpgBin() - retval = self.jpg.processFile(self.fh) - logging.info("processFile()=={}".format(retval)) + if (not self.is_processed): + self.getJpgBin(fname) + # hash the jpg image data + self.sig = Signature() + img_hash = self.jpg.genHash(self.sig.hh) + logging.info("img_hash-size=={}, img_hash=={}".format(len(img_hash), img_hash)) + return img_hash diff --git a/picseal.py b/picseal.py index 08fe17d..06d2b1d 100644 --- a/picseal.py +++ b/picseal.py @@ -6,7 +6,6 @@ import argparse import logging from shutil import copyfile #from subprocess import Popen, PIPE, check_call -from libs.crypto_pub import Signature from libs.toolbox import Toolbox from libs.jpg_tools import JpgTools @@ -25,33 +24,10 @@ def main(): def processImage(image_fn): jpg = JpgTools() jpg_bin = jpg.getJpgBin(image_fn) + img_hash = jpg.jpgHash() printImageInfo(jpg_bin) - # sig = Signature() - # sig.genSig(img_bin) - - # (pub_fn, priv_fn) = copyImage(image_fn) - - # writePubImg(pub_fn, sig) - # writePrivImg(priv_fn, sig) - - -# add a digital signature to the metadata -def writePubImg(pub_fn, sig): - pass - # img = ImgExif(pub_fn) - # img.addKey(sig.getPubKeyPEM()) - # img.addSig(sig.sig_data) - # img.saveFile() - -# -def writePrivImg(priv_fn, sig): - pass - # img = ImgExif(priv_fn) - # img.addKey(sig.getPrivKeyPEM()) - #img.addSig(sig.sig_data) - # img.saveFile() # def copyImage(image_fn):