mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-21 01:21:34 -08:00
Working HashedBlock implementation.
This commit is contained in:
1 parent
df30f09c2d
commit
2ebeacdf35
21 files changed
+501
-150
No files matched your search
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Copyright 2010 Brian Pellin.
|
||||
*
|
||||
* This file is part of KeePassDroid.
|
||||
*
|
||||
* KeePassDroid is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* KeePassDroid is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with KeePassDroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.keepassdroid.stream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class HashedBlockInputStream extends InputStream {
|
||||
|
||||
// private final static int BUFFER_SIZE = 1024 * 1024; // I think this is only needed in the writer
|
||||
private final static int HASH_SIZE = 32;
|
||||
|
||||
private BetterDataInputStream baseStream;
|
||||
private int bufferPos = 0;
|
||||
private byte[] buffer = new byte[0];
|
||||
private long bufferIndex = 0;
|
||||
private boolean atEnd = false;
|
||||
|
||||
|
||||
@Override
|
||||
public int read(byte[] b) throws IOException {
|
||||
return read(b, 0, b.length);
|
||||
}
|
||||
|
||||
public HashedBlockInputStream(InputStream is) {
|
||||
baseStream = new BetterDataInputStream(is);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] b, int offset, int length) throws IOException {
|
||||
if ( atEnd ) return -1;
|
||||
|
||||
int remaining = length;
|
||||
|
||||
while ( remaining > 0 ) {
|
||||
if ( bufferPos == buffer.length ) {
|
||||
// Get more from the source into the buffer
|
||||
if ( ! ReadHashedBlock() ) {
|
||||
return length - remaining;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Copy from buffer out
|
||||
int copyLen = Math.min(buffer.length - bufferPos, remaining);
|
||||
|
||||
System.arraycopy(buffer, bufferPos, b, offset, copyLen);
|
||||
|
||||
offset += copyLen;
|
||||
bufferPos += copyLen;
|
||||
|
||||
remaining -= copyLen;
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false, when the end of the source stream is reached
|
||||
* @throws IOException
|
||||
*/
|
||||
private boolean ReadHashedBlock() throws IOException {
|
||||
if ( atEnd ) return false;
|
||||
|
||||
bufferPos = 0;
|
||||
|
||||
long index = baseStream.readUInt();
|
||||
if ( index != bufferIndex ) {
|
||||
throw new IOException("Invalid data format");
|
||||
}
|
||||
bufferIndex++;
|
||||
|
||||
byte[] storedHash = baseStream.readBytes(HASH_SIZE);
|
||||
if ( storedHash == null || storedHash.length != HASH_SIZE) {
|
||||
throw new IOException("Invalid data format");
|
||||
}
|
||||
|
||||
int bufferSize = baseStream.readInt();
|
||||
if ( bufferSize < 0 ) {
|
||||
throw new IOException("Invalid data format");
|
||||
}
|
||||
|
||||
if ( bufferSize == 0 ) {
|
||||
for (int hash = 0; hash < HASH_SIZE; hash++) {
|
||||
if ( storedHash[hash] != 0 ) {
|
||||
throw new IOException("Invalid data format");
|
||||
}
|
||||
}
|
||||
|
||||
atEnd = true;
|
||||
buffer = new byte[0];
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer = baseStream.readBytes(bufferSize);
|
||||
if ( buffer == null || buffer.length != bufferSize ) {
|
||||
throw new IOException("Invalid data format");
|
||||
}
|
||||
|
||||
MessageDigest md = null;
|
||||
try {
|
||||
md = MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IOException("SHA-256 not implemented here.");
|
||||
}
|
||||
|
||||
byte[] computedHash = md.digest(buffer);
|
||||
if ( computedHash == null || computedHash.length != HASH_SIZE ) {
|
||||
throw new IOException("Hash wrong size");
|
||||
}
|
||||
|
||||
if ( ! Arrays.equals(storedHash, computedHash) ) {
|
||||
throw new IOException("Hashes didn't match.");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long skip(long n) throws IOException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
throw new IOException("Not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
baseStream.close();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user