use of java.security.DigestOutputStream in project gocd by gocd.
the class GoDashboardPipelineGroup method etag.
public String etag() {
try {
MessageDigest digest = DigestUtils.getSha256Digest();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new DigestOutputStream(new NullOutputStream(), digest));
outputStreamWriter.write(name);
outputStreamWriter.write("/");
outputStreamWriter.write(Integer.toString(permissions.hashCode()));
outputStreamWriter.write("[");
for (Map.Entry<String, GoDashboardPipeline> entry : pipelines.entrySet()) {
long lastUpdatedTimeStamp = entry.getValue().getLastUpdatedTimeStamp();
outputStreamWriter.write(entry.getKey());
outputStreamWriter.write(":");
outputStreamWriter.write(Long.toString(lastUpdatedTimeStamp));
}
outputStreamWriter.write("]");
outputStreamWriter.flush();
return Hex.encodeHexString(digest.digest());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of java.security.DigestOutputStream in project rskj by rsksmart.
the class BridgeSupportTest method getCheckpoints.
private InputStream getCheckpoints(NetworkParameters _networkParameters, List<BtcBlock> checkpoints) {
try {
ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
MessageDigest digest = Sha256Hash.newDigest();
final DigestOutputStream digestOutputStream = new DigestOutputStream(baOutputStream, digest);
digestOutputStream.on(false);
final DataOutputStream dataOutputStream = new DataOutputStream(digestOutputStream);
StoredBlock storedBlock = new StoredBlock(_networkParameters.getGenesisBlock(), _networkParameters.getGenesisBlock().getWork(), 0);
try {
dataOutputStream.writeBytes("CHECKPOINTS 1");
// Number of signatures to read. Do this later.
dataOutputStream.writeInt(0);
digestOutputStream.on(true);
dataOutputStream.writeInt(checkpoints.size());
ByteBuffer buffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE);
for (BtcBlock block : checkpoints) {
storedBlock = storedBlock.build(block);
storedBlock.serializeCompact(buffer);
dataOutputStream.write(buffer.array());
buffer.position(0);
}
} finally {
dataOutputStream.close();
digestOutputStream.close();
baOutputStream.close();
}
return new ByteArrayInputStream(baOutputStream.toByteArray());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of java.security.DigestOutputStream in project jdk8u_jdk by JetBrains.
the class Util method computeMethodHash.
/**
* Compute the "method hash" of a remote method. The method hash
* is a long containing the first 64 bits of the SHA digest from
* the UTF encoded string of the method name and descriptor.
*/
public static long computeMethodHash(Method m) {
long hash = 0;
ByteArrayOutputStream sink = new ByteArrayOutputStream(127);
try {
MessageDigest md = MessageDigest.getInstance("SHA");
DataOutputStream out = new DataOutputStream(new DigestOutputStream(sink, md));
String s = getMethodNameAndDescriptor(m);
if (serverRefLog.isLoggable(Log.VERBOSE)) {
serverRefLog.log(Log.VERBOSE, "string used for method hash: \"" + s + "\"");
}
out.writeUTF(s);
// use only the first 64 bits of the digest for the hash
out.flush();
byte[] hasharray = md.digest();
for (int i = 0; i < Math.min(8, hasharray.length); i++) {
hash += ((long) (hasharray[i] & 0xFF)) << (i * 8);
}
} catch (IOException ignore) {
/* can't happen, but be deterministic anyway. */
hash = -1;
} catch (NoSuchAlgorithmException complain) {
throw new SecurityException(complain.getMessage());
}
return hash;
}
use of java.security.DigestOutputStream in project jdk8u_jdk by JetBrains.
the class JceKeyStore method engineStore.
/**
* Stores this keystore to the given output stream, and protects its
* integrity with the given password.
*
* @param stream the output stream to which this keystore is written.
* @param password the password to generate the keystore integrity check
*
* @exception IOException if there was an I/O problem with data
* @exception NoSuchAlgorithmException if the appropriate data integrity
* algorithm could not be found
* @exception CertificateException if any of the certificates included in
* the keystore data could not be stored
*/
public void engineStore(OutputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException {
synchronized (entries) {
// password is mandatory when storing
if (password == null) {
throw new IllegalArgumentException("password can't be null");
}
// the certificate encoding
byte[] encoded;
MessageDigest md = getPreKeyedHash(password);
DataOutputStream dos = new DataOutputStream(new DigestOutputStream(stream, md));
// NOTE: don't pass dos to oos at this point or it'll corrupt
// the keystore!!!
ObjectOutputStream oos = null;
try {
dos.writeInt(JCEKS_MAGIC);
// always write the latest version
dos.writeInt(VERSION_2);
dos.writeInt(entries.size());
Enumeration<String> e = entries.keys();
while (e.hasMoreElements()) {
String alias = e.nextElement();
Object entry = entries.get(alias);
if (entry instanceof PrivateKeyEntry) {
PrivateKeyEntry pentry = (PrivateKeyEntry) entry;
// write PrivateKeyEntry tag
dos.writeInt(1);
// write the alias
dos.writeUTF(alias);
// write the (entry creation) date
dos.writeLong(pentry.date.getTime());
// write the protected private key
dos.writeInt(pentry.protectedKey.length);
dos.write(pentry.protectedKey);
// write the certificate chain
int chainLen;
if (pentry.chain == null) {
chainLen = 0;
} else {
chainLen = pentry.chain.length;
}
dos.writeInt(chainLen);
for (int i = 0; i < chainLen; i++) {
encoded = pentry.chain[i].getEncoded();
dos.writeUTF(pentry.chain[i].getType());
dos.writeInt(encoded.length);
dos.write(encoded);
}
} else if (entry instanceof TrustedCertEntry) {
// write TrustedCertEntry tag
dos.writeInt(2);
// write the alias
dos.writeUTF(alias);
// write the (entry creation) date
dos.writeLong(((TrustedCertEntry) entry).date.getTime());
// write the trusted certificate
encoded = ((TrustedCertEntry) entry).cert.getEncoded();
dos.writeUTF(((TrustedCertEntry) entry).cert.getType());
dos.writeInt(encoded.length);
dos.write(encoded);
} else {
// write SecretKeyEntry tag
dos.writeInt(3);
// write the alias
dos.writeUTF(alias);
// write the (entry creation) date
dos.writeLong(((SecretKeyEntry) entry).date.getTime());
// write the sealed key
oos = new ObjectOutputStream(dos);
oos.writeObject(((SecretKeyEntry) entry).sealedKey);
// NOTE: don't close oos here since we are still
// using dos!!!
}
}
/*
* Write the keyed hash which is used to detect tampering with
* the keystore (such as deleting or modifying key or
* certificate entries).
*/
byte[] digest = md.digest();
dos.write(digest);
dos.flush();
} finally {
if (oos != null) {
oos.close();
} else {
dos.close();
}
}
}
}
use of java.security.DigestOutputStream in project sling by apache.
the class FileDistributionPackageBuilder method readPackageInternal.
@Override
protected DistributionPackage readPackageInternal(@Nonnull ResourceResolver resourceResolver, @Nonnull InputStream stream) throws DistributionException {
DistributionPackage distributionPackage;
final File file;
DigestOutputStream outputStream = null;
try {
String name;
// stable id
Map<String, Object> info = new HashMap<String, Object>();
DistributionPackageUtils.readInfo(stream, info);
Object remoteId = info.get(DistributionPackageUtils.PROPERTY_REMOTE_PACKAGE_ID);
if (remoteId != null) {
name = remoteId.toString();
log.debug("preserving remote id {}", name);
} else {
name = "distrpck-read-" + System.nanoTime();
log.debug("generating a new id {}", name);
}
file = File.createTempFile(name, "." + getType(), tempDirectory);
outputStream = openDigestOutputStream(new FileOutputStream(file), digestAlgorithm);
IOUtils.copy(stream, outputStream);
outputStream.flush();
String digestMessage = readDigestMessage(outputStream);
distributionPackage = new FileDistributionPackage(file, getType(), digestAlgorithm, digestMessage);
} catch (Exception e) {
throw new DistributionException(e);
} finally {
IOUtils.closeQuietly(outputStream);
}
return distributionPackage;
}
Aggregations