本文整理汇总了Java中java.security.DigestOutputStream类的典型用法代码示例。如果您正苦于以下问题:Java DigestOutputStream类的具体用法?Java DigestOutputStream怎么用?Java DigestOutputStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DigestOutputStream类属于java.security包,在下文中一共展示了DigestOutputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: aroundWriteTo
import java.security.DigestOutputStream; //导入依赖的package包/类
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
MessageDigest digest = null;
try {
digest = getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException(e);
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DigestOutputStream digestStream = new DigestOutputStream(buffer, digest);
OutputStream old = context.getOutputStream();
context.setOutputStream(digestStream);
try {
context.proceed();
byte[] hash = digest.digest();
String encodedHash = getEncoder().encodeToString(hash);
context.getHeaders().putSingle(CONTENT_MD5_STRING, encodedHash);
byte[] content = buffer.toByteArray();
old.write(content);
} finally {
context.setOutputStream(old);
}
}
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:27,代码来源:ContentMD5Writer.java
示例2: makeFinalKey
import java.security.DigestOutputStream; //导入依赖的package包/类
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();
}
示例3: writeBinaryCheckpoints
import java.security.DigestOutputStream; //导入依赖的package包/类
private static void writeBinaryCheckpoints(TreeMap<Integer, StoredBlock> checkpoints, File file) throws Exception {
final FileOutputStream fileOutputStream = new FileOutputStream(file, false);
MessageDigest digest = Sha256Hash.newDigest();
final DigestOutputStream digestOutputStream = new DigestOutputStream(fileOutputStream, digest);
digestOutputStream.on(false);
final DataOutputStream dataOutputStream = new DataOutputStream(digestOutputStream);
dataOutputStream.writeBytes("CHECKPOINTS 1");
dataOutputStream.writeInt(0); // Number of signatures to read. Do this later.
digestOutputStream.on(true);
dataOutputStream.writeInt(checkpoints.size());
ByteBuffer buffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE);
for (StoredBlock block : checkpoints.values()) {
block.serializeCompact(buffer);
dataOutputStream.write(buffer.array());
buffer.position(0);
}
dataOutputStream.close();
Sha256Hash checkpointsHash = Sha256Hash.wrap(digest.digest());
System.out.println("Hash of checkpoints data is " + checkpointsHash);
digestOutputStream.close();
fileOutputStream.close();
System.out.println("Checkpoints written to '" + file.getCanonicalPath() + "'.");
}
示例4: calulateHash
import java.security.DigestOutputStream; //导入依赖的package包/类
static public byte[] calulateHash(JsonElement contents) {
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new InternalError("MD5 MessageDigest is not available");
}
OutputStream byteSink = new OutputStream() {
@Override
public void write(int b) throws IOException {
// ignore, since this is only used to calculate MD5
}
};
DigestOutputStream dis = new DigestOutputStream(byteSink, md);
new Gson().toJson(contents, new OutputStreamWriter(dis, Charset.forName(DEFAULT_CHARSET_NAME)));
return dis.getMessageDigest().digest();
}
示例5: NativeS3FsOutputStream
import java.security.DigestOutputStream; //导入依赖的package包/类
public NativeS3FsOutputStream(Configuration conf,
NativeFileSystemStore store, String key, Progressable progress,
int bufferSize) throws IOException {
this.conf = conf;
this.key = key;
this.backupFile = newBackupFile();
LOG.info("OutputStream for key '" + key + "' writing to tempfile '" + this.backupFile + "'");
try {
this.digest = MessageDigest.getInstance("MD5");
this.backupStream = new BufferedOutputStream(new DigestOutputStream(
new FileOutputStream(backupFile), this.digest));
} catch (NoSuchAlgorithmException e) {
LOG.warn("Cannot load MD5 digest algorithm," +
"skipping message integrity check.", e);
this.backupStream = new BufferedOutputStream(
new FileOutputStream(backupFile));
}
}
示例6: writeRenameReadCompare
import java.security.DigestOutputStream; //导入依赖的package包/类
protected void writeRenameReadCompare(Path path, long len)
throws IOException, NoSuchAlgorithmException {
// If len > fs.s3n.multipart.uploads.block.size,
// we'll use a multipart upload copy
MessageDigest digest = MessageDigest.getInstance("MD5");
OutputStream out = new BufferedOutputStream(
new DigestOutputStream(fs.create(path, false), digest));
for (long i = 0; i < len; i++) {
out.write('Q');
}
out.flush();
out.close();
assertTrue("Exists", fs.exists(path));
// Depending on if this file is over 5 GB or not,
// rename will cause a multipart upload copy
Path copyPath = path.suffix(".copy");
fs.rename(path, copyPath);
assertTrue("Copy exists", fs.exists(copyPath));
// Download file from S3 and compare the digest against the original
MessageDigest digest2 = MessageDigest.getInstance("MD5");
InputStream in = new BufferedInputStream(
new DigestInputStream(fs.open(copyPath), digest2));
long copyLen = 0;
while (in.read() != -1) {copyLen++;}
in.close();
assertEquals("Copy length matches original", len, copyLen);
assertArrayEquals("Digests match", digest.digest(), digest2.digest());
}
示例7: streamEncrypt
import java.security.DigestOutputStream; //导入依赖的package包/类
public static byte[] streamEncrypt(String message, SecretKey key,
MessageDigest digest)
throws Exception {
byte[] data;
Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
encCipher.init(Cipher.ENCRYPT_MODE, key);
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
DigestOutputStream dos = new DigestOutputStream(bos, digest);
CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
oos.writeObject(message);
}
data = bos.toByteArray();
}
if (debug) {
System.out.println(DatatypeConverter.printHexBinary(data));
}
return data;
}
示例8: generateSha1Hash
import java.security.DigestOutputStream; //导入依赖的package包/类
private byte[] generateSha1Hash(Bundle associatedBundle) throws IOException, NoSuchAlgorithmException {
MessageDigest hash = MessageDigest.getInstance("SHA1");
hash.reset();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
OutputStream output = new DigestOutputStream(byteArrayOutputStream, hash);
writeBundleArtifactFile(output, associatedBundle);
DigestOutputStream digestOutput = (DigestOutputStream) output;
byte[] digest = digestOutput.getMessageDigest().digest();
String hexStr = "";
for (int i = 0; i < digest.length; i++) {
hexStr += Integer.toString((digest[i] & 0xff) + 0x100, 16)
.substring(1);
}
return hexStr.getBytes();
}
示例9: getPrintWriter
import java.security.DigestOutputStream; //导入依赖的package包/类
/**
* Return a PrintWriter object that may be used to write to this file. Any
* necessary header information is written by this method.
*
* @return
* @throws IOException
*/
public PrintWriter getPrintWriter() throws IOException {
if (pw == null) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw (IOException) (new IOException("No MD5 implementation")
.initCause(e));
}
dos = new DigestOutputStream(new BufferedOutputStream(
new FileOutputStream(file)), digest);
pw = new TrapClosePrintWriter(dos);
// Write the headers....
String version = compatibleVersion == null ? Version.versionNumber : compatibleVersion;
pw.println("/* "
+ JavaCCGlobals.getIdString(toolName, file.getName())
+ " Version " + version + " */");
if (options != null) {
pw.println("/* JavaCCOptions:" + Options.getOptionsString(options) + " */");
}
}
return pw;
}
示例10: xmlExportDigest
import java.security.DigestOutputStream; //导入依赖的package包/类
protected String xmlExportDigest(JasperPrint print)
throws NoSuchAlgorithmException, FileNotFoundException, JRException, IOException
{
File outputFile = createXmlOutputFile();
log.debug("XML export output at " + outputFile.getAbsolutePath());
MessageDigest digest = MessageDigest.getInstance("SHA-1");
FileOutputStream output = new FileOutputStream(outputFile);
try
{
DigestOutputStream out = new DigestOutputStream(output, digest);
xmlExport(print, out);
}
finally
{
output.close();
}
return toDigestString(digest);
}
示例11: errExportDigest
import java.security.DigestOutputStream; //导入依赖的package包/类
protected String errExportDigest(Throwable t)
throws NoSuchAlgorithmException, FileNotFoundException, JRException, IOException
{
File outputFile = createXmlOutputFile();
log.debug("Error stack trace at " + outputFile.getAbsolutePath());
MessageDigest digest = MessageDigest.getInstance("SHA-1");
FileOutputStream output = new FileOutputStream(outputFile);
OutputStreamWriter osw = null;
try
{
DigestOutputStream out = new DigestOutputStream(output, digest);
//PrintStream ps = new PrintStream(out);
//t.printStackTrace(ps);
osw = new OutputStreamWriter(out, "UTF-8");
osw.write(t.getMessage());
}
finally
{
osw.close();
output.close();
}
return toDigestString(digest);
}
示例12: xmlDigest
import java.security.DigestOutputStream; //导入依赖的package包/类
protected String xmlDigest(JasperPrint print)
throws NoSuchAlgorithmException, FileNotFoundException, JRException, IOException
{
File outputFile = createXmlOutputFile();
log.debug("XML export output at " + outputFile.getAbsolutePath());
MessageDigest digest = MessageDigest.getInstance("SHA-1");
FileOutputStream output = new FileOutputStream(outputFile);
try
{
DigestOutputStream out = new DigestOutputStream(output, digest);
xmlExport(print, out);
}
finally
{
output.close();
}
return toDigestString(digest);
}
示例13: computeSHA1Hash
import java.security.DigestOutputStream; //导入依赖的package包/类
private static String computeSHA1Hash(final InputStream stream, final OutputStream os, final String providedSHA1Sum)
throws NoSuchAlgorithmException, IOException {
String sha1Hash;
// compute digest
// Exception squid:S2070 - not used for hashing sensitive
// data
@SuppressWarnings("squid:S2070")
final MessageDigest md = MessageDigest.getInstance("SHA-1");
try (final DigestOutputStream dos = new DigestOutputStream(os, md)) {
ByteStreams.copy(stream, dos);
}
sha1Hash = BaseEncoding.base16().lowerCase().encode(md.digest());
if (providedSHA1Sum != null && !providedSHA1Sum.equalsIgnoreCase(sha1Hash)) {
throw new HashNotMatchException(
"The given sha1 hash " + providedSHA1Sum + " not matching the calculated sha1 hash " + sha1Hash,
HashNotMatchException.SHA1);
}
return sha1Hash;
}
示例14: addContent
import java.security.DigestOutputStream; //导入依赖的package包/类
@Override
public byte[] addContent(InputStream stream) throws IOException {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
byte[] sha1Bytes;
Path tmp = File.createTempFile("content", ".tmp").toPath();
try (OutputStream fos = Files.newOutputStream(tmp)) {
messageDigest.reset();
DigestOutputStream dos = new DigestOutputStream(fos, messageDigest);
BufferedInputStream bis = new BufferedInputStream(stream);
byte[] bytes = new byte[8192];
int read;
while ((read = bis.read(bytes)) > -1) {
dos.write(bytes, 0, read);
}
fos.flush();
sha1Bytes = messageDigest.digest();
}
String key = toKey(sha1Bytes);
this.index.put(key, tmp.toUri());
return sha1Bytes;
} catch (NoSuchAlgorithmException e) {
throw new IOException(e);
}
}
示例15: genContent
import java.security.DigestOutputStream; //导入依赖的package包/类
/**
* Generate random contents for the image and store it together with the md5
* for later comparison.
*/
private ContentBody genContent(long txid) throws IOException {
MessageDigest digester = MD5Hash.getDigester();
// write through digester so we can roll the written image
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
DigestOutputStream ds = new DigestOutputStream(bos, digester);
// generate random bytes
new Random().nextBytes(randomBytes);
ds.write(randomBytes);
ds.flush();
// get written hash
MD5Hash hash = new MD5Hash(digester.digest());
// store contents and digest
digests.put(txid, hash);
content.put(txid, Arrays.copyOf(randomBytes, randomBytes.length));
return new ByteArrayBody(bos.toByteArray(), "filename");
}