From 5b835a2fe2c19e5a9910b4f5bb428a5b51169b47 Mon Sep 17 00:00:00 2001 From: JohnE Date: Fri, 9 Sep 2016 11:07:18 -0700 Subject: [PATCH] MOD: making more progress to having the jpg image binary data into a hash --- libs/crypto_pub.py | 17 +++++++++++-- libs/jpg_bin.py | 61 +++++++++++++++++++++++++++++++++++++--------- libs/jpg_tools.py | 10 +++----- 3 files changed, 68 insertions(+), 20 deletions(-) diff --git a/libs/crypto_pub.py b/libs/crypto_pub.py index 0cc5d10..8a8381b 100644 --- a/libs/crypto_pub.py +++ b/libs/crypto_pub.py @@ -11,13 +11,26 @@ class Signature: key_data = None pub_key = None sig_data = None + hh = None # def __init__(self): + hh = SHA512.new() self.genKeys() # - def genSig(self, bin_data): + def genSig(self, hshh): + signer = PKCS1_v1_5.new(self.key_data) + self.sig_data = signer.sign(hshh) + return self.sig_data + + # + def verifySig(self, hshh, bin_sig): + 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) @@ -25,7 +38,7 @@ class Signature: return self.sig_data # - def verifySig(self, bin_data, bin_sig): + def verifySig222(self, bin_data, bin_sig): hshh = SHA512.new() hshh.update(bin_data) verifier = PKCS1_v1_5.new(self.pub_key) diff --git a/libs/jpg_bin.py b/libs/jpg_bin.py index 534f068..fefb658 100644 --- a/libs/jpg_bin.py +++ b/libs/jpg_bin.py @@ -29,6 +29,11 @@ class JpgBin: data_idx = 0 data_len = 0 fh = None + hh = None + + markers = { + 'SOS': 0xffd9 + } continue_process = True @@ -48,20 +53,39 @@ class JpgBin: return False # - def processFile(self, fhandle): - self.fh = fhandle + def processFile(self, file_h): + self.fh = file_h self.getMoreBytes(True) if (self.data_buf): if (not self.__isJPG()): return False while(self.continue_process): - self.findMarker() + self.findAllMarker() self.getMoreBytes() return True return False + # + def genHash(self, file_h, hash_h): + self.hh = hash_h + self.processFile(file_h) + + while(self.continue_process): + if (self.findMarker(self.makers['SOS'])): + self.genImgHash() + self.getMoreBytes() + + return self.hh + + # + def genImgHash(self): + self.hh.update(self.data_buf[self.data_idx:]) + while(self.continue_process): + self.getMoreBytes() + self.hh.update(self.data_buf) + # def getMoreBytes(self, force_bytes=False): if (self.data_idx >= (self.data_len-1) or force_bytes): @@ -91,7 +115,12 @@ class JpgBin: return pos # - def findMarker(self): + def findMarker(self, marker): + + pass + + # + def findAllMarker(self): (word_b,) = struct.unpack('>H', self.data_buf[self.data_idx:self.data_idx+2]) hex_str = word_b.to_bytes(2, 'big').hex() # RST 0xD(n) (n==0..7) @@ -138,11 +167,27 @@ class JpgBin: else: self.data_idx += 1 + + # + # Image Metadata, Exif + # + # + def markerAppData(self): + self.calcSeekBytes() # def markerComment(self): self.calcSeekBytes() # + # Image Data + # + # + def markerSOS(self): + self.calcSeekBytes() + + def markerRST(self): + self.data_idx += 2 + def markerDQT(self): self.calcSeekBytes() @@ -161,18 +206,10 @@ class JpgBin: def markerDHT(self): self.calcSeekBytes() - def markerSOS(self): - self.calcSeekBytes() - - def markerRST(self): - self.data_idx += 2 - def markerEOI(self): self.data_idx += 2 # self.continue_process = False - def markerAppData(self): - self.calcSeekBytes() def __repr__(self): pass diff --git a/libs/jpg_tools.py b/libs/jpg_tools.py index bfafbd5..b559f8c 100644 --- a/libs/jpg_tools.py +++ b/libs/jpg_tools.py @@ -17,13 +17,11 @@ class JpgTools: # def jpgHash(self): - pass + self.fh = open(fname, "rb") + self.jpg = JpgBin() + retval = self.jpg.processFile(self.fh) - # - def getBytes(self, fname): - if (not self.fh): - self.fh = open(fname, "rb") - self.data_buf = self.fh.read(self.BUF_CHUNK_SIZE) + print("processFile()=={}".format(retval)) # def processFile(self, fname):