예제 #1
0
    def check_check_func(self):
        key = BitcoinVersionedPrivateKey(
            "cN5YgNRq8rbcJwngdp3fRzv833E7Z74TsF8nB6GhzRg8Gd9aGWH1")
        client = TalosVCRestClient()
        chunk = generate_random_chunk(1)
        policy = client.get_policy_with_txid(chunk.get_tag_hex())

        def get_bitcoin_address_for_pubkey_tmp(hex_pubkey):
            before = timer()
            priv = extract_bin_bitcoin_pubkey(hex_pubkey)
            hash_priv = get_bin_hash160(priv)
            addr = bin_hash160_to_address(hash_priv, version_byte=111)
            print "Time creation %s" % ((timer() - before) * 1000, )
            return addr

        def check_access_allowed_tmp(hex_pubkey, policy):
            before = timer()
            addr = get_bitcoin_address_for_pubkey_tmp(str(hex_pubkey))
            print "Bitcoin_lib %s" % ((timer() - before) * 1000, )
            if addr == policy.owner:
                return True
            if addr in policy.shares:
                return True
            return False

        self.assertTrue(
            check_access_allowed_tmp(key.public_key().to_hex(), policy))
예제 #2
0
    def test_storage1(self):
        key = BitcoinVersionedPrivateKey(
            "cN5YgNRq8rbcJwngdp3fRzv833E7Z74TsF8nB6GhzRg8Gd9aGWH1")
        talosStorage = TalosLevelDBDHTStorage("db_tmp")
        client = TalosVCRestClient()
        num_iter = 100
        for i in range(num_iter):
            chunk = generate_random_chunk(i)
            policy = client.get_policy_with_txid(chunk.get_tag_hex())
            before = timer()
            talosStorage.store_check_chunk(chunk, i, policy)
            print "Time store %s" % ((timer() - before) * 1000, )
            keeper = TimeKeeper()
            before = timer()
            talosStorage.get_check_chunk(chunk.key,
                                         key.public_key().to_hex(),
                                         policy,
                                         time_keeper=keeper)
            print "Time get %s" % ((timer() - before) * 1000, )
        count = 0
        for (key, value) in talosStorage.iteritemsOlderThan(100):
            count += 1
        self.assertEquals(0, count)

        count = 0
        for (key, value) in talosStorage.iteritems():
            count += 1
        self.assertEquals(num_iter, count)

        time.sleep(6)
        count = 0
        for (key, value) in talosStorage.iteritemsOlderThan(5):
            count += 1
        self.assertEquals(num_iter, count)
예제 #3
0
    def test_token_check(self):
        privkey = "cPuiZfHTkWAPhPvMSPetvP1jRarkQ8BRtPrEVuP5PhDsTGrrcm2f"
        dataSign = "64666173646661736661736466647366320000000000000000000000000000000000000000000000000000000000000000"
        tokenS = """{"owner":"dfasdfasfasdfdsf","chunk_key":"AAAAAAAAAAAAAAAAAAAAAA==","stream_id":2,"signature":"MEQCIBtOgOqsBR5K0RQs7MP4ef2oL+ycM9sMklf1OZIdHTH4AiAs+zD8iU5iFQML1OXF9ORFiNwyacF16jMUSTsNoJYXGQ==","nonce":"AAAAAAAAAAAAAAAAAAAAAA==","pubkey":"0222d41a2f7e3fb398cfe320bfcd25712f675c5d916664e3f5132feaecc8a4603f"}"""
        key = BitcoinVersionedPrivateKey(privkey)

        token = QueryToken.from_json(json.loads(tokenS))
        dataSignHere = hexlify(token.get_signature_data())
        print key.to_hex()
        print key.public_key().to_hex()
        print token.pubkey
        print key.public_key().to_hex() == token.pubkey

        print dataSignHere
        print dataSign == dataSignHere

        print "ok?"
        print check_valid(token)
예제 #4
0
 def test_chunk_java(self):
     privkey = "cQ1HBRRvJ9DaV2UZsEf5w1uLAoXjSVpLYVH5dB5hZUWk5jeJ8KCL"
     chunk = CloudChunk.decode(unhexlify(data_chunk_java))
     key = BitcoinVersionedPrivateKey(privkey)
     data_pub = chunk.encode_without_signature()
     print hexlify(data_pub)
     print data_pub_java
     print hexlify(data_pub) == data_pub_java
     print chunk.get_key_hex()
     print chunk.get_tag_hex()
     print chunk.check_signature(
         get_crypto_ecdsa_pubkey_from_bitcoin_hex(
             key.public_key().to_hex()))
예제 #5
0
    def test_storage(self):
        key = BitcoinVersionedPrivateKey(
            "cN5YgNRq8rbcJwngdp3fRzv833E7Z74TsF8nB6GhzRg8Gd9aGWH1")
        talosStorage = LevelDBStorage("db_tmp")
        client = TalosVCRestClient()
        for i in range(100):
            chunk = generate_random_chunk(i)
            policy = client.get_policy_with_txid(chunk.get_tag_hex())
            before = timer()
            talosStorage.store_check_chunk(chunk, i, policy)
            print "Time store %s" % ((timer() - before) * 1000, )
            keeper = TimeKeeper()
            before = timer()
            talosStorage.get_check_chunk(chunk.key,
                                         key.public_key().to_hex(),
                                         policy,
                                         time_keeper=keeper)
            print "Time get %s" % ((timer() - before) * 1000, )

        for (key, value) in talosStorage.db.RangeIter():
            print base64.b64encode(key)
            print base64.b64encode(value)
class ImageProducer(object):
    def __init__(self,
                 name,
                 start_time,
                 bc_privatekey,
                 policy_nonce,
                 stream_id,
                 txid,
                 ip='127.0.0.1',
                 port=14000):
        self.name = name
        self.start_time = start_time
        self.bc_privatekey = BitcoinVersionedPrivateKey(bc_privatekey)
        self.policy_nonce = base64.b64decode(policy_nonce)
        self.stream_id = stream_id
        self.txid = txid
        self.ip = ip
        self.port = port
        self.local_private_key = get_priv_key(self.bc_privatekey)

    def _generate_cloud_chunk(self, block_id, sym_key, chunk, timer_chunk):
        stream_ident = DataStreamIdentifier(
            self.bc_privatekey.public_key().address(), self.stream_id,
            self.policy_nonce, self.txid)

        return create_cloud_chunk(stream_ident,
                                  block_id,
                                  self.local_private_key,
                                  0,
                                  sym_key,
                                  chunk,
                                  time_keeper=timer_chunk)

    def _store_to_cloud(self, chunk_encoded):
        req = requests.post("http://%s:%d/store_chunk" % (self.ip, self.port),
                            data=chunk_encoded)
        return req.reason, req.status_code

    def run_loop(self,
                 image_capture,
                 time_file,
                 sym_key="a" * 16,
                 interval=3600):
        while True:
            try:
                timer_chunk = TimeKeeper()
                total_id = timer_chunk.start_clock_unique()
                timestamp_data = int(time.time())
                block_id = (timestamp_data - self.start_time) / interval
                # Take a picture
                picture_name = "%s%d.jpg" % (self.name, block_id)
                image_capture(picture_name)
                print picture_name

                # load image
                with open(picture_name, 'r') as f:
                    picture = f.read()

                chunk_tmp = ChunkData()

                chunk_tmp.add_entry(
                    PictureEntry(timestamp_data,
                                 picture_name,
                                 picture,
                                 time_keeper=timer_chunk))

                cur_time = timer()
                cloud_chunk = self._generate_cloud_chunk(
                    block_id, sym_key, chunk_tmp, timer_chunk)
                chunk_creation = timer() - cur_time

                len_normal = len(chunk_tmp.encode(use_compression=False))
                len_compressed = len(
                    compress_data(chunk_tmp.encode(use_compression=True)))

                cloud_chunk_encoded = cloud_chunk.encode()
                length_final = len(cloud_chunk_encoded)
                cur_time = timer()
                self._store_to_cloud(cloud_chunk_encoded)
                chunk_store = timer() - cur_time

                times = timer_chunk.logged_times

                time_file.write(
                    "%s, %s, %s, %s, %s, %s, %d, %d, %d,\n" %
                    (times['chunk_compression'], times['gcm_encryption'],
                     times['ecdsa_signature'],
                     times['time_lepton_compression'], chunk_creation * 1000,
                     chunk_store * 1000, len_normal, len_compressed,
                     length_final))
                time_file.flush()
                timer_chunk.stop_clock_unique('time_total', total_id)
                time.sleep(interval -
                           int(timer_chunk.logged_times['time_total'] / 1000))
            except RuntimeError as e:
                print e.message
                logging.error("Exception occured %s" % e.message)