use of java.security.DigestOutputStream in project sling by apache.
the class ResourceDistributionPackageBuilder method createPackageForAdd.
@Override
protected DistributionPackage createPackageForAdd(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionRequest request) throws DistributionException {
DistributionPackage distributionPackage;
try {
FileBackedMemoryOutputStream outputStream = null;
DigestOutputStream digestStream = null;
String digestMessage = null;
try {
outputStream = new FileBackedMemoryOutputStream(fileThreshold, memoryUnit, useOffHeapMemory, tempDirectory, "distrpck-create-", "." + getType());
if (digestAlgorithm != null) {
digestStream = openDigestOutputStream(outputStream, digestAlgorithm);
export(resourceResolver, request, digestStream);
} else {
export(resourceResolver, request, outputStream);
}
outputStream.flush();
if (digestAlgorithm != null) {
digestMessage = readDigestMessage(digestStream);
}
} finally {
IOUtils.closeQuietly(outputStream);
IOUtils.closeQuietly(digestStream);
}
Resource packagesRoot = DistributionPackageUtils.getPackagesRoot(resourceResolver, packagesPath);
InputStream inputStream = null;
Resource packageResource = null;
try {
inputStream = outputStream.openWrittenDataInputStream();
packageResource = uploadStream(resourceResolver, packagesRoot, inputStream, outputStream.size());
} finally {
IOUtils.closeQuietly(inputStream);
outputStream.clean();
}
distributionPackage = new ResourceDistributionPackage(packageResource, getType(), resourceResolver, digestAlgorithm, digestMessage);
} catch (IOException e) {
throw new DistributionException(e);
}
return distributionPackage;
}
use of java.security.DigestOutputStream in project bnd by bndtools.
the class Jar method getTimelessDigest.
public byte[] getTimelessDigest() throws Exception {
check();
MessageDigest md = MessageDigest.getInstance("SHA1");
OutputStream dout = new DigestOutputStream(IO.nullStream, md);
// dout = System.out;
Manifest m = getManifest();
if (m != null) {
Manifest m2 = new Manifest(m);
Attributes main = m2.getMainAttributes();
String lastmodified = (String) main.remove(new Attributes.Name(Constants.BND_LASTMODIFIED));
String version = main.getValue(new Attributes.Name(Constants.BUNDLE_VERSION));
if (version != null && Verifier.isVersion(version)) {
Version v = new Version(version);
main.putValue(Constants.BUNDLE_VERSION, v.getWithoutQualifier().toString());
}
writeManifest(m2, dout);
for (Map.Entry<String, Resource> entry : getResources().entrySet()) {
String path = entry.getKey();
if (path.equals(manifestName))
continue;
Resource resource = entry.getValue();
dout.write(path.getBytes(UTF_8));
resource.write(dout);
}
}
return md.digest();
}
use of java.security.DigestOutputStream in project fdroidclient by f-droid.
the class JKS method engineStore.
public void engineStore(OutputStream out, char[] passwd) throws IOException, NoSuchAlgorithmException, CertificateException {
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(charsToBytes(passwd));
md.update("Mighty Aphrodite".getBytes("UTF-8"));
DataOutputStream dout = new DataOutputStream(new DigestOutputStream(out, md));
dout.writeInt(MAGIC);
dout.writeInt(2);
dout.writeInt(aliases.size());
for (Enumeration e = aliases.elements(); e.hasMoreElements(); ) {
String alias = (String) e.nextElement();
if (trustedCerts.containsKey(alias)) {
dout.writeInt(TRUSTED_CERT);
dout.writeUTF(alias);
dout.writeLong(((Date) dates.get(alias)).getTime());
writeCert(dout, (Certificate) trustedCerts.get(alias));
} else {
dout.writeInt(PRIVATE_KEY);
dout.writeUTF(alias);
dout.writeLong(((Date) dates.get(alias)).getTime());
byte[] key = (byte[]) privateKeys.get(alias);
dout.writeInt(key.length);
dout.write(key);
Certificate[] chain = (Certificate[]) certChains.get(alias);
dout.writeInt(chain.length);
for (int i = 0; i < chain.length; i++) writeCert(dout, chain[i]);
}
}
byte[] digest = md.digest();
dout.write(digest);
}
use of java.security.DigestOutputStream in project KeePassDX by Kunzisoft.
the class CryptoUtil method hashGen.
public static byte[] hashGen(String transform, byte[] data, int offset, int count) {
MessageDigest hash;
try {
hash = MessageDigest.getInstance(transform);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
NullOutputStream nos = new NullOutputStream();
DigestOutputStream dos = new DigestOutputStream(nos, hash);
try {
dos.write(data, offset, count);
dos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return hash.digest();
}
use of java.security.DigestOutputStream in project KeePassDX by Kunzisoft.
the class PwDbV3Output method outputHeader.
public PwDbHeaderV3 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);
// Content checksum
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new PwDbOutputException("SHA-256 not implemented here.");
}
// Header checksum
MessageDigest headerDigest;
try {
headerDigest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new PwDbOutputException("SHA-256 not implemented here.");
}
NullOutputStream nos;
nos = new NullOutputStream();
DigestOutputStream headerDos = new DigestOutputStream(nos, headerDigest);
// Output header for the purpose of calculating the header checksum
PwDbHeaderOutputV3 pho = new PwDbHeaderOutputV3(header, headerDos);
try {
pho.outputStart();
pho.outputEnd();
headerDos.flush();
} catch (IOException e) {
throw new PwDbOutputException(e);
}
byte[] headerHash = headerDigest.digest();
headerHashBlock = getHeaderHashBuffer(headerHash);
// Output database for the purpose of calculating the content checksum
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 for real output, containing content hash
pho = new PwDbHeaderOutputV3(header, os);
try {
pho.outputStart();
dos.on(false);
pho.outputContentHash();
dos.on(true);
pho.outputEnd();
dos.flush();
} catch (IOException e) {
throw new PwDbOutputException(e);
}
return header;
}
Aggregations