def request_agreement(self):
        """7. Alice starts key agreement
        generate fingerprint and doing fuzzy commitment
        send hash and delta to Bob
        """
        log.info('7. Alice starts key agreement')

        #===============================================================================
        # Fingerprinting and Fuzzy Cryptography
        #===============================================================================
        # generate fingerprint
        self.fingerprint = fingerprint_energy_diff.get_fingerprint(self.recording_data, self.recording_samplerate)
        
        # save fingerprint for debugging
        scipy.savetxt("client_fingerprint.txt", self.fingerprint)

        log.debug('Alice fingerprint:\n'+str(self.fingerprint))
        
        # doing commit, rs codes can correct up to (n-m)/2 errors
        self.hash, self.delta, self.private_key = crypto_fuzzy_jw.JW_commit(self.fingerprint, m=self.rs_code_m, n=self.rs_code_n, symsize=self.rs_code_symsize)
        
        log.debug('Alice Blob:\nHash:\n'+str(self.hash)+'\nDelta:\n'+str(self.delta))
        
        # save delta for debugging
        scipy.savetxt("client_delta.txt", self.delta)
        
        # remote call for key agreement
        # using debug means sending also the fingerprint in clear text!!!
        # meaning no security!
        if self.debug:
            accept_agreement = self.pairing_server.callRemote("agreement_debug", self.fingerprint.tolist(), self.hash, self.delta.tolist())
            accept_agreement.addCallbacks(self.answer_agreement)
        else:
            accept_agreement = self.pairing_server.callRemote("agreement", self.hash, self.delta.tolist())
            accept_agreement.addCallbacks(self.answer_agreement)
def get_possible_fingerprints(recording_data, recording_samplerate):
    """generate many fingerprints varying in time
    
    :param recording_data: complete recording data
    :param recording_samplerate: samplerate of recording
    :return: many fingerprints
    """
    fingerprint = fingerprint_energy_diff.get_fingerprint(recording_data, recording_samplerate)
    possible_fingerprints = [fingerprint]
    # correct 0,20 seconds in time (0,20*44100=~8800) -> 88*100 data chunks!
    n = 176 # -> 0,4 seconds
    for i in range(1, n):
        log.debug('Generating possible fingerprint '+str(i)+' of '+str(n))
        # move 100 data chunks to left and build fingerprint
        data_left = move_data_left(recording_data, i)
        possible_fingerprints += [fingerprint_energy_diff.get_fingerprint(data_left, recording_samplerate)]
        # move 100 data chunks to right and build fingerprint
        data_right = move_data_right(recording_data, i)
        possible_fingerprints += [fingerprint_energy_diff.get_fingerprint(data_right, recording_samplerate)]

    return possible_fingerprints
    def remote_agreement_debug(self, fingerprint_debug, hash, delta):
        """THIS IS A DEBUG FUNCTION
        using the fingerprint from the client
        Using this means NO security!
        
        8. Key Agreement on Server
        generates fingerprint and decommits
        using received ``hash`` and ``delta``
        
        :param hash: SHA-512 Hash of codeword c
        :type hash: str
        :param delta: difference
        :type delta: list
        """
        log.info('8. Key Agreement on Server')
        
        #===============================================================================
        # Fingerprinting and Fuzzy Cryptography
        #===============================================================================       
        # generate fingerprint, not used see possible fingerprints
        self.fingerprint = fingerprint_energy_diff.get_fingerprint(self.recording_data, self.recording_samplerate)
        
        # save fingerprint for debugging
        scipy.savetxt("server_fingerprint.txt", self.fingerprint)

        log.debug('Bob fingerprint:\n'+str(self.fingerprint))
        
        # get possible fingerprints
        possible_fingerprints = get_possible_fingerprints(self.recording_data, self.recording_samplerate)
        
        # DEBUG
        length = len(fingerprint_debug)
        
        distances = []
        
        for fingerprint in possible_fingerprints:
            # calculate hamming distance between fingerprints
            distance = hamming_distance(fingerprint, fingerprint_debug)
            print('Distance: '+str(distance)+' of '+str(length))
            print('Correlation percentage: '+str(1-float(distance)/float(length)))
            distances += [distance]
        
        min_distance = min(distances)
        min_correlation = 1-float(min(distances))/float(length)
        print('Minimal distance: '+str(min_distance)+' of '+str(length))
        print('Minimal correlation percentage: '+str(min_correlation))
        
        try:
            minimals = scipy.genfromtxt(self.debug_file)
        except Exception, err:
            log.error('%s' % str(err))
            print('first time, so creating minimals')
            minimals = scipy.array([])
    def remote_agreement(self, hash, delta):
        """8. Key Agreement on Server
        generates fingerprint and decommits
        using received ``hash`` and ``delta``
        
        :param hash: SHA-512 Hash of codeword c
        :type hash: str
        :param delta: difference
        :type delta: list
        """
        log.info('8. Key Agreement on Server')
        
        #===============================================================================
        # Fingerprinting and Fuzzy Cryptography
        #===============================================================================       
        # generate fingerprint, not used see possible fingerprints
        self.fingerprint = fingerprint_energy_diff.get_fingerprint(self.recording_data, self.recording_samplerate)
        
        # save fingerprint for debugging
        scipy.savetxt("server_fingerprint.txt", self.fingerprint)

        log.debug('Bob fingerprint:\n'+str(self.fingerprint))
        
        # get possible fingerprints
        possible_fingerprints = get_possible_fingerprints(self.recording_data, self.recording_samplerate)
                
        for fingerprint in possible_fingerprints:
            try:
                # trying to decommit
                self.private_key, corr = crypto_fuzzy_jw.JW_decommit(hash, delta, fingerprint, m=self.rs_code_m, n=self.rs_code_n, symsize=self.rs_code_symsize)
            except Exception, err:
                log.error('%s' % str(err))
                
            else:
                # if hash is the same accept key agreement,
                # test is in JW_decommit, try fails when not!
                # return True for accepted connection
                return True
# fingerprinting based on energy difference
import fingerprint_energy_diff

import logging
# Logging all above INFO level, output to stderr
logging.basicConfig(#format='%(asctime)s %(levelname)-8s %(message)s')
                    format='%(levelname)-8s %(message)s')
log = logging.getLogger("fuzzy_pairing")
log.setLevel(logging.DEBUG)


# load channels:
left_channel, samplerate = load_mono(base+'1.5_3/high/music5.wav')
right_channel, samplerate = load_mono(base+'1.5_3/high/music1.wav')


# fingerprint energy diff
fingerprint_left = fingerprint_energy_diff.get_fingerprint(left_channel, samplerate)
fingerprint_right = fingerprint_energy_diff.get_fingerprint(right_channel, samplerate)

print repr(fingerprint_left)
print repr(fingerprint_right)

# calculate hamming distance between fingerprints
distance = hamming_distance(fingerprint_left, fingerprint_right)
length = len(fingerprint_left)
print length, distance
print('Correlation %: '+str(1-float(distance)/float(length)))


path = "/home/ds1/Projekte/Fuzzy Pairing Paper/Versuche Audiodateien/"


matrix = list()
      
# get all files in all subdirectories etc.
for root, dirs, files in os.walk(path):
    for name in files:
        filename = os.path.join(root, name)
        print filename
       
        print "current file is: " + name
        mono_channel, samplerate = load_mono(filename)
        
        # fingerprint energy diff
        fingerprint = fingerprint_energy_diff.get_fingerprint(mono_channel, samplerate)
        
        # you can add more analysis using helper_analysis here
        # and write the results in the matrix for further analyzing

        matrix.append([filename,fingerprint])

print matrix

# save it with pickle, can later be analyzed
with file('analysis_matrix.txt', 'wr') as f:
    pickle.dump(matrix, f)