def testPRF(self):
     logging.debug('Running testPRF method.')
     output1 = ccrypto.PRF(_KEY1, _PLAINTEXT1, hashfunc='sha256')
     output1_again = ccrypto.PRF(_KEY1, _PLAINTEXT1, hashfunc='sha256')
     output2 = ccrypto.PRF(_KEY1, _PLAINTEXT2, hashfunc='sha256')
     self.assertEqual(16, len(output1))
     self.assertEqual(output1, output1_again)
     self.assertNotEqual(output1, output2)
     self.assertEqual(
         '\xa7\x15\xd9\xbbyO@\xad\xfc\x9f\x02\xcb\x9cD\xb7\x29', output1)
     output3 = ccrypto.PRF(_KEY1,
                           _PLAINTEXT1,
                           output_len=37,
                           hashfunc='sha256')
     self.assertEqual(37, len(output3))
     self.assertTrue(output1 in output3)
     self.assertFalse(output1
                      in output3[16:])  # check prefix is not repeated.
     # test using default hash function sha1
     output4 = ccrypto.PRF(_KEY1, _PLAINTEXT1)
     self.assertEqual(16, len(output4))
     self.assertNotEqual(output1, output4)
    def GetStringKeyHash(self,
                         field_name,
                         data,
                         output_len=None,
                         hashfunc=None):
        """Calculates a keyed hash of a string.

    Args:
      field_name: unicode string that is used as part of hash calculation - i.e.
        an 8 byte length of field_name and field_name is prepended to data
        before hashing.
      data: unicode string.
      output_len: in bytes, if not set then use value from constructor.
      hashfunc: if not set then use value from constructor.

    Returns:
      base64 encoding of keyed hash of utf8 encoded data.

    Raises:
      ValueError: when data or fieldname is not a unicode string or when
        fieldname is an empty string.
    """

        if not isinstance(data, unicode):
            raise ValueError('SubstringHash methods only works with data '
                             'input type unicode, given: %s' % type(data))
        if not isinstance(field_name, unicode):
            raise ValueError(
                'fieldname input type should be unicode, given: %s' %
                type(field_name))
        if not field_name:
            raise ValueError('field_name cannot be empty.')
        output_digest_len = output_len or self._output_len
        digest_hashfunc = hashfunc or self._hashfunc
        # data --> len, field_name, data
        extended_data = ccrypto.IntToFixedSizeString(len(field_name))
        extended_data += field_name + data
        utf8_data = extended_data.encode('utf-8')
        raw_hash = ccrypto.PRF(self._key, utf8_data, output_digest_len,
                               digest_hashfunc)
        return base64.b64encode(raw_hash)
def GenerateStringHashKey(key, identifier):
    return ccrypto.PRF(key, 'stringhash_' + str(identifier))
def GenerateHomomorphicCipherKey(key, identifier):
    return ccrypto.PRF(key, 'homomorphic_' + str(identifier))
def GenerateProbabilisticCipherKey(key, identifier):
    return ccrypto.PRF(key, 'probabilistic_' + str(identifier))
def GeneratePseudonymCipherKey(key, identifier):
    return ccrypto.PRF(key, 'pseudonym_' + str(identifier))