use of java.security.DigestOutputStream in project Pix-Art-Messenger by kriztan.
the class FileBackend method save.
public boolean save(Avatar avatar) {
File file;
if (isAvatarCached(avatar)) {
file = new File(getAvatarPath(avatar.getFilename()));
avatar.size = file.length();
} else {
String filename = getAvatarPath(avatar.getFilename());
file = new File(filename + ".tmp");
file.getParentFile().mkdirs();
OutputStream os = null;
try {
file.createNewFile();
os = new FileOutputStream(file);
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.reset();
DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
final byte[] bytes = avatar.getImageAsBytes();
mDigestOutputStream.write(bytes);
mDigestOutputStream.flush();
mDigestOutputStream.close();
String sha1sum = CryptoHelper.bytesToHex(digest.digest());
if (sha1sum.equals(avatar.sha1sum)) {
file.renameTo(new File(filename));
} else {
Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
file.delete();
return false;
}
avatar.size = bytes.length;
} catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
return false;
} finally {
close(os);
}
}
return true;
}
use of java.security.DigestOutputStream in project Pix-Art-Messenger by kriztan.
the class FileBackend method getPepAvatar.
private Avatar getPepAvatar(Bitmap bitmap, Bitmap.CompressFormat format, int quality) {
try {
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
MessageDigest digest = MessageDigest.getInstance("SHA-1");
DigestOutputStream mDigestOutputStream = new DigestOutputStream(mBase64OutputStream, digest);
if (!bitmap.compress(format, quality, mDigestOutputStream)) {
return null;
}
mDigestOutputStream.flush();
mDigestOutputStream.close();
long chars = mByteArrayOutputStream.size();
if (format != Bitmap.CompressFormat.PNG && quality >= 50 && chars >= Config.AVATAR_CHAR_LIMIT) {
int q = quality - 2;
Log.d(Config.LOGTAG, "avatar char length was " + chars + " reducing quality to " + q);
return getPepAvatar(bitmap, format, q);
}
Log.d(Config.LOGTAG, "settled on char length " + chars + " with quality=" + quality);
final Avatar avatar = new Avatar();
avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
avatar.image = new String(mByteArrayOutputStream.toByteArray());
if (format.equals(Bitmap.CompressFormat.WEBP)) {
avatar.type = "image/webp";
} else if (format.equals(Bitmap.CompressFormat.JPEG)) {
avatar.type = "image/jpeg";
} else if (format.equals(Bitmap.CompressFormat.PNG)) {
avatar.type = "image/png";
}
avatar.width = bitmap.getWidth();
avatar.height = bitmap.getHeight();
return avatar;
} catch (Exception e) {
return null;
}
}
use of java.security.DigestOutputStream in project keepass2android by PhilippC.
the class PwDatabaseV3 method makeFinalKey.
public void makeFinalKey(byte[] masterSeed, byte[] masterSeed2, int numRounds) throws IOException {
// Write checksum Checksum
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new IOException("SHA-256 not implemented here.");
}
NullOutputStream nos = new NullOutputStream();
DigestOutputStream dos = new DigestOutputStream(nos, md);
byte[] transformedMasterKey = transformMasterKey(masterSeed2, masterKey, numRounds);
dos.write(masterSeed);
dos.write(transformedMasterKey);
finalKey = md.digest();
}
use of java.security.DigestOutputStream in project keepass2android by PhilippC.
the class PwDbV3Output method outputHeader.
public PwDbHeader outputHeader(OutputStream os) throws PwDbOutputException {
// Build header
PwDbHeaderV3 header = new PwDbHeaderV3();
header.signature1 = PwDbHeader.PWM_DBSIG_1;
header.signature2 = PwDbHeaderV3.DBSIG_2;
header.flags = PwDbHeaderV3.FLAG_SHA2;
if (mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Rjindal) {
header.flags |= PwDbHeaderV3.FLAG_RIJNDAEL;
} else if (mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Twofish) {
header.flags |= PwDbHeaderV3.FLAG_TWOFISH;
} else {
throw new PwDbOutputException("Unsupported algorithm.");
}
header.version = PwDbHeaderV3.DBVER_DW;
header.numGroups = mPM.getGroups().size();
header.numEntries = mPM.entries.size();
header.numKeyEncRounds = mPM.getNumKeyEncRecords();
setIVs(header);
// Write checksum Checksum
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new PwDbOutputException("SHA-256 not implemented here.");
}
NullOutputStream nos;
nos = new NullOutputStream();
DigestOutputStream dos = new DigestOutputStream(nos, md);
BufferedOutputStream bos = new BufferedOutputStream(dos);
try {
outputPlanGroupAndEntries(bos);
bos.flush();
bos.close();
} catch (IOException e) {
throw new PwDbOutputException("Failed to generate checksum.");
}
header.contentsHash = md.digest();
// Output header
PwDbHeaderOutputV3 pho = new PwDbHeaderOutputV3(header, os);
try {
pho.output();
} catch (IOException e) {
throw new PwDbOutputException("Failed to output the header.");
}
return header;
}
use of java.security.DigestOutputStream in project ma-core-public by infiniteautomation.
the class JsonSerializableUtility method digestsDiffer.
private boolean digestsDiffer(Object fromValue, Object toValue) throws IOException, JsonException {
try (DigestOutputStream dos = new DigestOutputStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
// no-op, just digesting
}
}, MessageDigest.getInstance("MD5"));
OutputStreamWriter toStreamWriter = new OutputStreamWriter(dos);
OutputStreamWriter fromStreamWriter = new OutputStreamWriter(dos)) {
JsonWriter fromWriter = new JsonWriter(Common.JSON_CONTEXT, fromStreamWriter);
// We need fresh writers to avoid miscellaneous commas or whatnot
JsonWriter toWriter = new JsonWriter(Common.JSON_CONTEXT, toStreamWriter);
fromWriter.writeObject(fromValue);
fromWriter.flush();
byte[] fromDigest = dos.getMessageDigest().digest();
fromDigest = Arrays.copyOf(fromDigest, fromDigest.length);
toWriter.writeObject(toValue);
toWriter.flush();
byte[] toDigest = dos.getMessageDigest().digest();
return !Arrays.equals(fromDigest, toDigest);
} catch (NoSuchAlgorithmException e) {
// Required to implement MD5, really shouldn't happen
throw new ShouldNeverHappenException(e);
}
}
Aggregations