NEW: hashing of jpg image date complete!

This commit is contained in:
JohnE 2016-10-12 15:28:22 -07:00
parent 8938531449
commit 79e35190a8
6 changed files with 64 additions and 71 deletions

View File

@ -8,14 +8,14 @@ from Crypto.Hash import SHA512
class Signature: class Signature:
key_data = None
pub_key = None
sig_data = None
hh = None
# #
def __init__(self): def __init__(self):
hh = SHA512.new() key_data = None
pub_key = None
sig_data = None
hh = None
self.hh = SHA512.new()
self.genKeys() self.genKeys()
# #
@ -29,21 +29,6 @@ class Signature:
verifier = PKCS1_v1_5.new(self.pub_key) verifier = PKCS1_v1_5.new(self.pub_key)
return verifier.verify(hshh, bin_sig) 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): def genKeys(self):
logging.info("Generating public keys...") logging.info("Generating public keys...")
@ -52,6 +37,8 @@ class Signature:
logging.debug("public key==\n{}".format(self.pub_key.exportKey('PEM'))) 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'))) 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): def getPrivKeyPEM(self):
return self.key_data.exportKey('PEM') return self.key_data.exportKey('PEM')

View File

@ -36,7 +36,7 @@ class JpgBin:
self.data_idx = 0 self.data_idx = 0
self.data_len = 0 self.data_len = 0
self.fh = None self.fh = None
self.hh = None # self.hh = None
self.continue_process = True self.continue_process = True
@ -81,19 +81,17 @@ class JpgBin:
return False return False
# #
def genHash(self, file_h, hash_h): def genHash(self, hash_h):
self.hh = hash_h self.fh.seek(0)
for marker in self.jpg_fp.markers_img: 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 img_hash = hash_h.digest()
return img_hash
#
def genImgHash222(self):
self.hh.update(self.data_buf[self.data_idx:])
while(self.continue_process):
self.__getMoreBytes()
self.hh.update(self.data_buf)
# #
def findAllMarkers(self): def findAllMarkers(self):

View File

@ -40,7 +40,7 @@ class JpgFingerprint:
total = 0 total = 0
for marker in markers: for marker in markers:
str += repr(marker) + "\n" str += repr(marker) + "\n"
total += marker.marker_size total += marker.len
str += "[TOT ] bytes=={}".format(total) str += "[TOT ] bytes=={}".format(total)
str += "\n" str += "\n"
return str return str
@ -52,15 +52,15 @@ class JpgMarker:
Marker Data Type Marker Data Type
""" """
def __init__(self, mhex, fpos, mlen, mstr): def __init__(self, mhex, fpos, mlen, mstr):
self.marker_hex = mhex self.hex = mhex
self.marker_hexstr = self.marker_hex.to_bytes(2, 'big').hex() self.hexstr = self.hex.to_bytes(2, 'big').hex()
self.marker_filepos = fpos self.fpos = fpos
self.marker_size = mlen self.len = mlen
self.marker_cat = mstr self.type = mstr
def __repr__(self): 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)

25
libs/jpg_picseal.py Normal file
View File

@ -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

View File

@ -4,13 +4,16 @@
import logging import logging
# from PIL import Image # from PIL import Image
from libs.jpg_bin import JpgBin from libs.jpg_bin import JpgBin
from libs.crypto_pub import Signature
class JpgTools: class JpgTools:
def __init__(self): def __init__(self):
self.fh = None self.fh = None
pass self.jpg = None
self.sig = None
self.is_processed = False
# #
# process a jpg file, read only # process a jpg file, read only
@ -20,14 +23,18 @@ class JpgTools:
self.jpg = JpgBin() self.jpg = JpgBin()
retval = self.jpg.processFile(self.fh) retval = self.jpg.processFile(self.fh)
logging.info("processFile()=={}".format(retval)) logging.info("processFile()=={}".format(retval))
self.is_processed = True
return self.jpg return self.jpg
# #
# process a jpg file, create new jpg with crypto keys # hash the jpg image data
# #
def jpgHash(self): def jpgHash(self):
self.fh = open(fname, "rb") if (not self.is_processed):
self.jpg = JpgBin() self.getJpgBin(fname)
retval = self.jpg.processFile(self.fh)
logging.info("processFile()=={}".format(retval))
# 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

View File

@ -6,7 +6,6 @@ import argparse
import logging import logging
from shutil import copyfile from shutil import copyfile
#from subprocess import Popen, PIPE, check_call #from subprocess import Popen, PIPE, check_call
from libs.crypto_pub import Signature
from libs.toolbox import Toolbox from libs.toolbox import Toolbox
from libs.jpg_tools import JpgTools from libs.jpg_tools import JpgTools
@ -25,33 +24,10 @@ def main():
def processImage(image_fn): def processImage(image_fn):
jpg = JpgTools() jpg = JpgTools()
jpg_bin = jpg.getJpgBin(image_fn) jpg_bin = jpg.getJpgBin(image_fn)
img_hash = jpg.jpgHash()
printImageInfo(jpg_bin) 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): def copyImage(image_fn):