ru.d_shap.hash.builder.InputStreamHashWithSaltBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hash Show documentation
Show all versions of hash Show documentation
Hash library provides facilities for the hash computations
The newest version!
///////////////////////////////////////////////////////////////////////////////////////////////////
// Hash library provides facilities for the hash computations.
// Copyright (C) 2017 Dmitry Shapovalov.
//
// This file is part of hash library.
//
// Hash library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Hash library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see .
///////////////////////////////////////////////////////////////////////////////////////////////////
package ru.d_shap.hash.builder;
import java.io.InputStream;
import ru.d_shap.hash.Hash;
import ru.d_shap.hash.HashHelper;
import ru.d_shap.hash.SaltStoreType;
/**
* Hash builder for the input stream.
*
* @author Dmitry Shapovalov
*/
final class InputStreamHashWithSaltBuilder extends HashWithSaltBuilder {
private final InputStream _stream;
InputStreamHashWithSaltBuilder(final InputStream stream) {
super();
_stream = stream;
}
@Override
public byte[] getHash() {
Hash hash = addSalt(HashHelper.getHash(_stream, getAlgorithm()));
return addSaltBytes(hash);
}
@Override
public boolean isHashValid() {
if (getSaltStoreType() == SaltStoreType.DO_NOT_STORE) {
return matches(addSalt(HashHelper.getHash(_stream, getAlgorithm())));
} else {
Hash hash = HashHelper.getHash(_stream, getAlgorithm());
int storedSaltLength = getStoredSaltLength(hash.getLength());
byte[] storedHash = getHashFromStoredHash(getSaltStoreType(), storedSaltLength);
byte[] storedSalt = getSaltFromStoredHash(getSaltStoreType(), storedSaltLength);
return hash.addSalt(storedSalt).matches(storedHash);
}
}
}