diff --git a/entropy.py b/entropy.py index 8ac2160..b2d03f7 100644 --- a/entropy.py +++ b/entropy.py @@ -20,7 +20,7 @@ -import subprocess,os +import subprocess,os,sys import hashlib import cv2,base64 import sounddevice @@ -28,36 +28,23 @@ """ system constants """ -NOISE_SAMPLE = 30 # main sampling seconds +NOISE_SAMPLE = 3 # main sampling seconds SHA256_ROUNDS = 2048 # sha256 rounds (number) -NOISE_SAMPLE_SALT = 5 # salt sampling seconds +NOISE_SAMPLE_SALT = 3 # salt sampling seconds SAMPLE_RATE = 44100 # samplerate SAMPLING_FMT = 'wav' IMG_SAMPLES = 64 IMG_SAMPLES_SALT = 8 + + class entropy(): def __init__(self,source='mic'): self.source=source - self.entropy=0 + self.entropy=1 return - def _checkMic(self): - try: - sounddevice.rec(int(1 * 44100)) - aaudio = True - except: - aaudio = False - return aaudio - - def _checkCam(self): - try: - camera.read() - ccam = True - except: - ccam = False - return ccam def _getsha256(self,z): return hashlib.sha256(z.encode('utf-8')).hexdigest() @@ -67,18 +54,18 @@ def _getMicSd(self): """ creating unique noise by sampling entropy and salting it for SHA256_ROUNDS. Returns sha256 salt hashed noise. """ - noise0 = sounddevice.rec(int(SAMPLE_RATE * NOISE_SAMPLE), samplerate=SAMPLE_RATE, channels=2, blocking=True) - salt0 = sounddevice.rec(int(SAMPLE_RATE * NOISE_SAMPLE_SALT), samplerate=SAMPLE_RATE, channels=2, blocking=True) - (noise,salt) =( hashlib.sha256(bytearray(b''.join(noise0))).hexdigest() , hashlib.sha256(bytearray(b''.join(salt0))).hexdigest() ) - for i in range(0,SHA256_ROUNDS): - noise=self._getsha256(noise+salt) + try: + noise0 = sounddevice.rec(int(SAMPLE_RATE * NOISE_SAMPLE), samplerate=SAMPLE_RATE, channels=2, blocking=True) + salt0 = sounddevice.rec(int(SAMPLE_RATE * NOISE_SAMPLE_SALT), samplerate=SAMPLE_RATE, channels=2, blocking=True) + (noise,salt) =( hashlib.sha256(bytearray(b''.join(noise0))).hexdigest() , hashlib.sha256(bytearray(b''.join(salt0))).hexdigest() ) + for i in range(0,SHA256_ROUNDS): + noise=self._getsha256(noise+salt) + except: + noise=False return noise def _getImgRnd(self): - # if false was returned, the device is not working, then exit function - if self.img_rnd==False: - return False """ salt hashing and converting image data into 256 bits final hash """ img_rnd_result= self._getsha256( str(self.img_rnd['base'] ) ) salt = self._getsha256(str( self.img_rnd['salt'] ) ) @@ -88,32 +75,35 @@ def _getImgRnd(self): def _takePhoto(self): - """ taking multiple photos from webcam in order to create randomness. Returns data and salt """ - camera = cv2.VideoCapture(0) - (all_data,all_salt)=("","") - for i in range(IMG_SAMPLES): - return_value, image = camera.read() - ocurrent = base64.b64encode(image) - all_data=all_data+str(ocurrent) - for z in range(IMG_SAMPLES_SALT): - return_value, image = camera.read() - ocurrent = base64.b64encode(image) - all_salt=all_salt+str(ocurrent) - del(camera) - self.img_rnd={ 'base': all_data, - 'salt': all_salt - } + """ taking multiple photos from webcam in order to create randomness. Returns data and salt. used device 0 """ + try: + camera = cv2.VideoCapture(0) + (all_data,all_salt)=("","") + for i in range(IMG_SAMPLES): + return_value, image = camera.read() + ocurrent = base64.b64encode(image) + all_data=all_data+str(ocurrent) + for z in range(IMG_SAMPLES_SALT): + return_value, image = camera.read() + ocurrent = base64.b64encode(image) + all_salt=all_salt+str(ocurrent) + del(camera) + self.img_rnd={ 'base': all_data, + 'salt': all_salt + } + except: + self.img_rnd=False return self.img_rnd def getEntropy(self): """ returns true entropy from chosen source """ if self.source=='mic': - self.entropy=self._getMicSd() if self._checkMic() else False + self.entropy=self._getMicSd() elif self.source=='photo': - self.entropy = False - if self._checkCam(): - self._takePhoto() + if self._takePhoto(): self.entropy=self._getImgRnd() + else: + self.entropy=False return self.entropy diff --git a/papergen.py b/papergen.py index 55d77be..4b47232 100755 --- a/papergen.py +++ b/papergen.py @@ -60,12 +60,12 @@ def clear(): def main(): a = ee.entropy(entropy_source) clear() + working_message="Getting data from mic.. please wait" if entropy_source=='mic' else "Getting data from webcam.. please wait" + print (working_message) priv = a.getEntropy() if priv==False: print("Error: sound or video devices not working, aborted") sys.exit() - working_message="Getting randomness from mic.. please wait" if entropy_source=='mic' else "Getting randomness from webcam.. please wait" - print (working_message) oWallet="" clear() if wType=='single':