Package org.hive2hive.core.model

Examples of org.hive2hive.core.model.Chunk


    KeyPair encryptionKeys = EncryptionUtil.generateRSAKeyPair(H2HConstants.KEYLENGTH_CHUNK);
    KeyPair protectionKeysOld = EncryptionUtil.generateRSAKeyPair();
    KeyPair protectionKeysNew = EncryptionUtil.generateRSAKeyPair();

    // generate a fake chunk
    Chunk chunk = new Chunk(proxy.getNodeId(), NetworkTestUtil.randomString().getBytes(), 0);
    // encrypt the chunk
    HybridEncryptedContent encryptedChunk = H2HEncryptionUtil.encryptHybrid(chunk,
        encryptionKeys.getPublic());

    // initialize put
    Parameters parameters = new Parameters().setLocationKey(chunk.getId())
        .setContentKey(H2HConstants.FILE_CHUNK).setProtectionKeys(protectionKeysOld)
        .setData(encryptedChunk);
    // indicate to generate hash
    parameters.setHashFlag(true);
    // put encrypted chunk into network
View Full Code Here


    }

    if (FileUtil.getFileSize(file) == 0 && chunkNumber == 0) {
      // special case: file exists but is empty.
      // return an empty chunk
      return new Chunk(chunkId, new byte[0], 0);
    }

    int read = 0;
    long offset = chunkSize * (long)chunkNumber;
    byte[] data = new byte[chunkSize];

    // read the next chunk of the file considering the offset
    RandomAccessFile rndAccessFile = new RandomAccessFile(file, "r");
    rndAccessFile.seek(offset);
    read = rndAccessFile.read(data);
    rndAccessFile.close();

    if (read > 0) {
      // the byte-Array may contain many empty slots if last chunk. Truncate it
      data = truncateData(data, read);
      return new Chunk(chunkId, data, chunkNumber);
    } else {
      return null;
    }
  }
View Full Code Here

      logger.info("File not found on disk, cannot return a chunk");
      sendDirectResponse(createResponse(null));
      return;
    }

    Chunk chunk = null;
    try {
      // retrieve the requested file part (offset and length)
      chunk = FileChunkUtil.getChunk(path.toFile(), chunkLength, chunkNumber, "chunk-" + chunkNumber);
    } catch (IOException e) {
      logger.error("Cannot read the chunk", e);
      sendDirectResponse(createResponse(null));
      return;
    }

    // verify the chunk hash
    byte[] md5Hash = EncryptionUtil.generateMD5Hash(chunk.getData());
    if (H2HEncryptionUtil.compareMD5(md5Hash, chunkHash)) {
      logger.debug("MD5 hash of the chunk {} has been verified, returning the chunk", chunkNumber);

      // return the content of the file part
      sendDirectResponse(createResponse(chunk));
View Full Code Here

  @Override
  protected void doExecute() throws InvalidProcessStateException, ProcessExecutionException {
    File file = context.getFile();

    Chunk chunk;
    try {
      chunk = FileChunkUtil.getChunk(file, config.getChunkSize(), index, chunkId);
    } catch (IOException e) {
      logger.error("File {}: Could not read the file.", file.getAbsolutePath());
      throw new ProcessExecutionException("File " + file.getAbsolutePath()
          + ": Could not read the file", e);
    }

    if (chunk != null) {
      try {
        // encrypt the chunk prior to put such that nobody can read it
        HybridEncryptedContent encryptedContent = H2HEncryptionUtil.encryptHybrid(chunk, context
            .consumeChunkKeys().getPublic());

        logger.debug("Uploading chunk {} of file {}.", chunk.getOrder(), file.getName());
        Parameters parameters = new Parameters().setLocationKey(chunk.getId())
            .setContentKey(H2HConstants.FILE_CHUNK).setData(encryptedContent)
            .setProtectionKeys(context.consumeProtectionKeys()).setTTL(chunk.getTimeToLive());

        // data manager has to produce the hash, which gets used for signing
        parameters.setHashFlag(true);
        // put the encrypted chunk into the network
        put(parameters);
View Full Code Here

      int genNOC = rnd.nextInt(100) + 1; // avoid 0's
      File randomFile = FileTestUtil.createFileRandomContent(genNOC, parent, CHUNK_SIZE);

      // get chunk 0 ... n-1
      int chosenChunk = rnd.nextInt(genNOC - 2); // index starts at 0
      Chunk chunk = FileChunkUtil.getChunk(randomFile, CHUNK_SIZE, chosenChunk,
          NetworkTestUtil.randomString());
      assertEquals(CHUNK_SIZE, chunk.getSize());
      assertEquals(chosenChunk, chunk.getOrder());

      // get last chunk n
      int lastChunkIndex = genNOC - 1; // index starts at 0
      chunk = FileChunkUtil.getChunk(randomFile, CHUNK_SIZE, lastChunkIndex,
          NetworkTestUtil.randomString());
      assertTrue(CHUNK_SIZE > chunk.getSize());
      assertEquals(lastChunkIndex, chunk.getOrder());

      randomFile.deleteOnExit(); // cleanup
    }
  }
View Full Code Here

  @Test
  public void testGetChunkEmpty() throws IOException {
    File file = new File(parent, NetworkTestUtil.randomString());
    FileUtils.write(file, "");
    Chunk chunk = FileChunkUtil.getChunk(file, CHUNK_SIZE, 0, NetworkTestUtil.randomString());
    assertEquals(0, chunk.getSize());
    assertEquals(0, chunk.getOrder());

    file.deleteOnExit(); // clenaup
  }
View Full Code Here

  @Test
  public void testGetChunkTooHighIndex() throws IOException {
    File file = new File(parent, NetworkTestUtil.randomString());
    FileUtils.write(file, "test");
    Chunk chunk = FileChunkUtil.getChunk(file, CHUNK_SIZE, 100, NetworkTestUtil.randomString());
    assertNull(chunk);
  }
View Full Code Here

      task.abortDownload("Chunk not found in the DHT");
      return;
    }

    HybridEncryptedContent encrypted = (HybridEncryptedContent) content;
    Chunk chunk;

    try {
      NetworkContent decrypted = H2HEncryptionUtil.decryptHybrid(encrypted, task.getDecryptionKey());
      chunk = (Chunk) decrypted;
    } catch (ClassNotFoundException | InvalidKeyException | DataLengthException
        | IllegalBlockSizeException | BadPaddingException | IllegalStateException
        | InvalidCipherTextException | IllegalArgumentException | IOException e) {
      task.abortDownload("Decryption of the chunk failed");
      return;
    }

    try {
      FileUtils.writeByteArrayToFile(tempDestination, chunk.getData());
    } catch (IOException e) {
      task.abortDownload("Cannot write the chunk data to temporary file");
      return;
    }
View Full Code Here

      logger.error("Peer {} did not send the chunk {}", context.getSelectedPeer(), metaChunk.getIndex());
      rerunProcess();
      return;
    }

    Chunk chunk = (Chunk) responseMessage.getContent();

    // verify the md5 hash
    byte[] respondedHash = EncryptionUtil.generateMD5Hash(chunk.getData());
    if (H2HEncryptionUtil.compareMD5(respondedHash, metaChunk.getChunkHash())) {
      logger.debug("Peer {} sent a valid content for chunk {}. MD5 verified.",
          context.getSelectedPeer(), metaChunk.getIndex());
    } else {
      logger.error("Peer {} sent an invalid content for chunk {}.", context.getSelectedPeer(),
          metaChunk.getIndex());
      rerunProcess();
      return;
    }

    // hash is ok, write it to the file
    try {
      FileUtils.writeByteArrayToFile(context.getTempDestination(), chunk.getData());
      logger.debug("Wrote chunk {} to temporary file {}", context.getMetaChunk().getIndex(),
          context.getTempDestination());

      // finalize the sub-process
      context.getTask().setDownloaded(context.getMetaChunk().getIndex(), context.getTempDestination());
View Full Code Here

    logger.trace(String.format("%s chunks for large file '%s'.", Integer.toString(chunks), file.getName()));

    // process chunk for chunk, hash it and add the meta information to the context
    for (int i = 0; i < chunks; i++) {
      String chunkId = UUID.randomUUID().toString();
      Chunk chunk;

      try {
        chunk = FileChunkUtil.getChunk(file, config.getChunkSize(), i, chunkId);
      } catch (IOException e) {
        throw new ProcessExecutionException("Cannot read the large file", e);
      }

      byte[] md5Hash = EncryptionUtil.generateMD5Hash(chunk.getData());
      context.getMetaChunks().add(new MetaChunk(chunkId, md5Hash, i));
    }
  }
View Full Code Here

TOP

Related Classes of org.hive2hive.core.model.Chunk

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.