@Override public boolean equals(Object o) { if(!(o instanceof ClientCHKBlock)) return false; ClientCHKBlock block = (ClientCHKBlock) o; if(!key.equals(block.key)) return false; return block.block.equals(this.block); }
void setKey(int blockNumber, ClientCHK key) throws IOException { if(logMINOR) Logger.minor(this, "Setting key "+key+" for block "+blockNumber+" on "+this, new Exception("debug")); try { ClientCHK oldKey = readKey(blockNumber); if(!oldKey.equals(key)) throw new IOException("Key for block has changed! Data corruption or bugs in SplitFileInserter code"); } catch (MissingKeyException e) { // Ok. writeKey(blockNumber, key); } // Must be called either way as we don't regenerate blocksHaveKeys on startup. setHasKey(blockNumber); }
private boolean checkEncodedDataBlocks(byte[][] checkBlocks, boolean[] checkBlocksPresent, SplitFileSegmentKeys keys, boolean capturingBinaryBlob) { for(int i=0;i<checkBlocks.length;i++) { if(checkBlocksPresent[i]) continue; ClientCHK decodeKey = keys.getKey(i+blocksForDecode(), null, false); // Encode it to check whether the key is the same. ClientCHKBlock block; try { block = ClientCHKBlock.encodeSplitfileBlock(checkBlocks[i], decodeKey.getCryptoKey(), decodeKey.getCryptoAlgorithm()); ClientCHK actualKey = block.getClientKey(); if(!actualKey.equals(decodeKey)) { Logger.error(this, "Splitfile check block "+i+" does not encode to expected key for "+this+" for "+parent); return false; } if(capturingBinaryBlob) parent.fetcher.maybeAddToBinaryBlob(block); } catch (CHKEncodeException e) { // Impossible! parent.fail(new FetchException(FetchExceptionMode.INTERNAL_ERROR, "Decoded block could not be encoded")); Logger.error(this, "Impossible: Decoded block could not be encoded"); return false; } } return true; }
public synchronized byte[] checkAndGetBlockData(int blockNum) throws IOException { if(!blockChooser.hasSucceeded(blockNum)) return null; ClientCHK key = getKey(blockNum); if(key == null) return null; for(int i=0;i<blocksFetched.length;i++) { if(blocksFetched[i] == blockNum) { byte[] buf = readBlock(i); try { ClientCHKBlock block = ClientCHKBlock.encodeSplitfileBlock(buf, key.getCryptoKey(), key.getCryptoAlgorithm()); if(!(block.getClientKey().equals(key))) { Logger.error(this, "Block "+blockNum+" in blocksFound["+i+"] is not valid!"); blockChooser.onUnSuccess(blockNum); succeeded = false; finished = false; } else { return buf; } } catch (CHKEncodeException e) { // Should not be possible. Logger.error(this, "Impossible: "+e); return null; } } } Logger.error(this, "Block "+blockNum+" in blocksFound but not in blocksFetched on "+this); return null; }
block = ClientCHKBlock.encodeSplitfileBlock(dataBlocks[i], decodeKey.getCryptoKey(), decodeKey.getCryptoAlgorithm()); ClientCHK actualKey = block.getClientKey(); if(!actualKey.equals(decodeKey)) { if(i == dataBlocks.length-1 && this.segNo == parent.segments.length-1 && parent.lastBlockMightNotBePadded()) {
private void checkBlock(byte[] data, boolean newAlgo) throws CHKEncodeException, InvalidCompressionCodecException, CHKVerifyException, CHKDecodeException, IOException { byte cryptoAlgorithm = newAlgo ? Key.ALGO_AES_CTR_256_SHA256 : Key.ALGO_AES_PCFB_256_SHA256; byte[] copyOfData = new byte[data.length]; System.arraycopy(data, 0, copyOfData, 0, data.length); ClientCHKBlock encodedBlock = ClientCHKBlock.encode(new ArrayBucket(data), false, false, (short)-1, data.length, null, false, null, cryptoAlgorithm); // Not modified in-place. assert(Arrays.equals(data, copyOfData)); ClientCHK key = encodedBlock.getClientKey(); if(newAlgo) { // Check with no JCA. ClientCHKBlock otherEncodedBlock = ClientCHKBlock.encode(new ArrayBucket(data), false, false, (short)-1, data.length, null, false, null, cryptoAlgorithm, true); assertTrue(key.equals(otherEncodedBlock.getClientKey())); assertTrue(Arrays.equals(otherEncodedBlock.getBlock().data, encodedBlock.getBlock().data)); assertTrue(Arrays.equals(otherEncodedBlock.getBlock().headers, encodedBlock.getBlock().headers)); } // Verify it. CHKBlock block = CHKBlock.construct(encodedBlock.getBlock().data, encodedBlock.getBlock().headers, cryptoAlgorithm); ClientCHKBlock checkBlock = new ClientCHKBlock(block, key); ArrayBucket checkData = (ArrayBucket) checkBlock.decode(new ArrayBucketFactory(), data.length, false); assert(Arrays.equals(checkData.toByteArray(), data)); if(newAlgo) { checkData = (ArrayBucket) checkBlock.decode(new ArrayBucketFactory(), data.length, false, true); assert(Arrays.equals(checkData.toByteArray(), data)); } }
ClientCHKBlock.encodeSplitfileBlock(buf, decodeKey.getCryptoKey(), decodeKey.getCryptoAlgorithm()); ClientCHK actualKey = block.getClientKey(); if(decodeKey == null || !decodeKey.equals(actualKey)) {