use of java.security.DigestInputStream in project fdroidclient by f-droid.
the class JKS method engineLoad.
public void engineLoad(InputStream in, char[] passwd) throws IOException, NoSuchAlgorithmException, CertificateException {
MessageDigest md = MessageDigest.getInstance("SHA");
if (passwd != null)
md.update(charsToBytes(passwd));
// HAR HAR
md.update("Mighty Aphrodite".getBytes("UTF-8"));
aliases.clear();
trustedCerts.clear();
privateKeys.clear();
certChains.clear();
dates.clear();
if (in == null)
return;
DataInputStream din = new DataInputStream(new DigestInputStream(in, md));
if (din.readInt() != MAGIC)
throw new IOException("not a JavaKeyStore");
// version no.
din.readInt();
final int n = din.readInt();
aliases.ensureCapacity(n);
if (n < 0)
throw new LoadKeystoreException("Malformed key store");
for (int i = 0; i < n; i++) {
int type = din.readInt();
String alias = din.readUTF();
aliases.add(alias);
dates.put(alias, new Date(din.readLong()));
switch(type) {
case PRIVATE_KEY:
int len = din.readInt();
byte[] encoded = new byte[len];
din.read(encoded);
privateKeys.put(alias, encoded);
int count = din.readInt();
Certificate[] chain = new Certificate[count];
for (int j = 0; j < count; j++) chain[j] = readCert(din);
certChains.put(alias, chain);
break;
case TRUSTED_CERT:
trustedCerts.put(alias, readCert(din));
break;
default:
throw new LoadKeystoreException("Malformed key store");
}
}
if (passwd != null) {
byte[] computedHash = md.digest();
byte[] storedHash = new byte[20];
din.read(storedHash);
if (!MessageDigest.isEqual(storedHash, computedHash)) {
throw new LoadKeystoreException("Incorrect password, or integrity check failed.");
}
}
}
use of java.security.DigestInputStream in project cytoscape-impl by cytoscape.
the class AppParser method getChecksum.
/**
* Obtain the SHA-512 checksum of a file, in the following format: sha512:3c1c..
* @param file The file to find the checksum
* @return The SHA-512 checksum, in format: sha512:e1..
* @throws ChecksumException If unable to obtain SHA-512 algorithm implementation,
* file does not exist, or IO error while reading
*/
public String getChecksum(File file) throws ChecksumException {
MessageDigest messageDigest;
try {
messageDigest = MessageDigest.getInstance("SHA-512");
} catch (NoSuchAlgorithmException e) {
throw new ChecksumException("Unable to obtain SHA-512 algorithm implementation");
}
InputStream inputStream;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new ChecksumException("File " + file.getAbsolutePath() + " does not exist.");
}
try {
inputStream = new DigestInputStream(inputStream, messageDigest);
byte[] byteBuffer = new byte[128];
while (inputStream.available() != 0) {
inputStream.read(byteBuffer);
}
} catch (IOException e) {
throw new ChecksumException("Error reading from file " + file + ", " + e.getMessage());
} finally {
try {
inputStream.close();
} catch (IOException e) {
// logger.warn("Failed to close input stream on file " + file.getAbsolutePath() + ", " + e.getMessage());
}
}
byte[] digest = messageDigest.digest();
String result = "";
for (int i = 0; i < digest.length; i++) {
// Convert each byte to a 2-digit hex number, adding 0x100 to obtain the 0 if the byte is 0E
result += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
}
return "sha512:" + result;
}
use of java.security.DigestInputStream in project android_packages_apps_Gallery2 by LineageOS.
the class Fingerprint method fromInputStream.
/**
* Creates a Fingerprint based on the contents of a file.
*
* Note that this will close() stream after calculating the digest.
* @param byteCount length of original data will be stored at byteCount[0] as a side product
* of the fingerprint calculation
*/
public static Fingerprint fromInputStream(InputStream stream, long[] byteCount) throws IOException {
DigestInputStream in = null;
long count = 0;
try {
in = new DigestInputStream(stream, DIGESTER);
byte[] bytes = new byte[8192];
while (true) {
// scan through file to compute a fingerprint.
int n = in.read(bytes);
if (n < 0)
break;
count += n;
}
} finally {
if (in != null)
in.close();
}
if ((byteCount != null) && (byteCount.length > 0))
byteCount[0] = count;
return new Fingerprint(in.getMessageDigest().digest());
}
use of java.security.DigestInputStream in project lzc_app_lib by httplzc.
the class StringManagerUtil method fileHash.
public static String fileHash(String inputFile, String hashType) {
int bufferSize = 256 * 1024;
FileInputStream fileInputStream = null;
DigestInputStream digestInputStream = null;
try {
MessageDigest messageDigest = MessageDigest.getInstance(hashType);
// 使用DigestInputStream
fileInputStream = new FileInputStream(inputFile);
digestInputStream = new DigestInputStream(fileInputStream, messageDigest);
byte[] buffer = new byte[bufferSize];
while (digestInputStream.read(buffer) > 0) ;
// 获取最终的MessageDigest
messageDigest = digestInputStream.getMessageDigest();
// 拿到结果,也是字节数组,包含16个元素
byte[] hash = messageDigest.digest();
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10)
hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (digestInputStream != null)
digestInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (fileInputStream != null)
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
use of java.security.DigestInputStream in project javautils by jiadongpo.
the class Md5Hasher method md5Hash.
public static byte[] md5Hash(final File file) throws IOException {
final MessageDigest digest = getMd5Digest();
final FileInputStream fStream = new FileInputStream(file);
final BufferedInputStream bStream = new BufferedInputStream(fStream);
final DigestInputStream blobStream = new DigestInputStream(bStream, digest);
final byte[] buffer = new byte[BYTE_BUFFER_SIZE];
int num = 0;
do {
num = blobStream.read(buffer);
} while (num > 0);
bStream.close();
return digest.digest();
}
Aggregations