本文整理汇总了Java中java.security.DigestOutputStream.close方法的典型用法代码示例。如果您正苦于以下问题:Java DigestOutputStream.close方法的具体用法?Java DigestOutputStream.close怎么用?Java DigestOutputStream.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.DigestOutputStream
的用法示例。
在下文中一共展示了DigestOutputStream.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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() + "'.");
}
示例2: 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 = MessageDigest.getInstance("SHA-256");
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 = new Sha256Hash(digest.digest());
System.out.println("Hash of checkpoints data is " + checkpointsHash);
digestOutputStream.close();
fileOutputStream.close();
System.out.println("Checkpoints written to '" + file.getCanonicalPath() + "'.");
}
示例3: writeRandomFile
import java.security.DigestOutputStream; //导入方法依赖的package包/类
public static byte [] writeRandomFile(int bytes, OutputStream out) throws IOException {
try {
DigestOutputStream dos = new DigestOutputStream(out, MessageDigest.getInstance(CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM));
byte [] buf = new byte[BUF_SIZE];
int count = 0;
int towrite = 0;
while (count < bytes) {
random.nextBytes(buf);
towrite = ((bytes - count) > buf.length) ? buf.length : (bytes - count);
dos.write(buf, 0, towrite);
count += towrite;
}
dos.flush();
dos.close();
return dos.getMessageDigest().digest();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Cannot find digest algorithm: " + CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM);
}
}
示例4: digestContent
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* Encode and digest the object's content in order to detect changes made
* outside of the object's own interface (for example, if the data is accessed
* using data() and then modified).
* @return
* @throws ContentEncodingException if there is a problem encoding the content
* @throws IOException if there is a problem writing the object to the stream.
*/
protected byte [] digestContent() throws ContentEncodingException, IOException {
try {
// Otherwise, might have been written when we weren't looking (someone accessed
// data and then changed it).
DigestOutputStream dos = new DigestOutputStream(new NullOutputStream(), MessageDigest.getInstance(DEFAULT_CHECKSUM_ALGORITHM));
writeObjectImpl(dos);
dos.flush();
dos.close();
byte [] currentValue = dos.getMessageDigest().digest();
return currentValue;
} catch (NoSuchAlgorithmException e) {
Log.warning("No pre-configured algorithm {0} available -- configuration error!", DEFAULT_CHECKSUM_ALGORITHM);
throw new RuntimeException("No pre-configured algorithm " + DEFAULT_CHECKSUM_ALGORITHM + " available -- configuration error!");
}
}
示例5: writeToFile
import java.security.DigestOutputStream; //导入方法依赖的package包/类
@Override
protected void writeToFile(List<ExportEntry> csvEntries) throws IOException {
// Setup compression stuff
ZipOutputStream zos = new ZipOutputStream(fileOutpuStream);
zos.setMethod(ZipOutputStream.DEFLATED);
zos.setLevel(9);
// Setup signature stuff
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-512");
} catch (NoSuchAlgorithmException ex) {
LOG.log(Level.SEVERE, "Missing hash algorithm for signature. No file exported!", ex);
throw new IOException("Missing hash algorithm for signature.");
}
DigestOutputStream dos = new DigestOutputStream(zos, md);
// write data
zos.putNextEntry(new ZipEntry(DATA_ZIP_ENTRY));
CsvWriter cwriter = new CsvWriter(dos, VaultCsvEntry.CSV_DELIMITER,
Charset.forName("UTF-8"));
cwriter.writeRecord(((CsvEntry) csvEntries.get(0)).getCsvHeaderRecord());
for (ExportEntry item : csvEntries) {
cwriter.writeRecord(((CsvEntry) item).toCsvRecord());
}
cwriter.flush();
// add signature file
zos.putNextEntry(new ZipEntry(SIGNATURE_ZIP_ENTRY));
String sigString = (new HexBinaryAdapter()).marshal(md.digest());
zos.write(sigString.getBytes(), 0, sigString.getBytes().length);
// close everything
cwriter.close();
dos.close();
zos.close();
fileOutpuStream.close();
}
示例6: getCheckpoints
import java.security.DigestOutputStream; //导入方法依赖的package包/类
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");
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 (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);
}
}
示例7: save
import java.security.DigestOutputStream; //导入方法依赖的package包/类
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;
}
示例8: save
import java.security.DigestOutputStream; //导入方法依赖的package包/类
public boolean save(Avatar avatar) {
File file;
if (isAvatarCached(avatar)) {
file = new File(getAvatarPath(avatar.getFilename()));
} 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);
mDigestOutputStream.write(avatar.getImageAsBytes());
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;
}
} catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
return false;
} finally {
close(os);
}
}
avatar.size = file.length();
return true;
}
示例9: getPepAvatar
import java.security.DigestOutputStream; //导入方法依赖的package包/类
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;
}
}
示例10: save
import java.security.DigestOutputStream; //导入方法依赖的package包/类
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;
}
示例11: getPepAvatar
import java.security.DigestOutputStream; //导入方法依赖的package包/类
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;
}
}
示例12: writeFile
import java.security.DigestOutputStream; //导入方法依赖的package包/类
public static int writeFile(ContentName name, boolean random, int size, CCNHandle handle) throws Exception {
int segmentsToWrite = 0;
RepositoryFileOutputStream rfos = new RepositoryFileOutputStream(name.append("randomFile"), handle);
DigestOutputStream dos = new DigestOutputStream(rfos, MessageDigest.getInstance(CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM));
byte [] buf = new byte[BUF_SIZE];
int count = 0;
int towrite = 0;
Random rand = new Random();
int bytes = 0;
if (random) {
bytes = rand.nextInt(maxBytes) + 1;
} else
bytes = size;
double block = (double)bytes/(double)SystemConfiguration.BLOCK_SIZE;
segmentsToWrite = (int) (Math.ceil(block) + 1);
Log.fine(Log.FAC_TEST, "bytes: {0} block size: {1} div: {2} ceil: {3}", bytes, SystemConfiguration.BLOCK_SIZE, block, (int)Math.ceil(block));
Log.fine(Log.FAC_TEST, "will write out a {0} byte file, will have {1} segments (1 is a header)", bytes, segmentsToWrite);
while (count < bytes) {
rand.nextBytes(buf);
towrite = ((bytes - count) > buf.length) ? buf.length : (bytes - count);
dos.write(buf, 0, towrite);
count += towrite;
}
dos.flush();
dos.close();
Log.info(Log.FAC_TEST, "Wrote file to repository: {0} with {1} segments", rfos.getBaseName(), segmentsToWrite);
return segmentsToWrite;
}
示例13: writeFile
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* @param completeName
* @param fileLength
* @param randBytes
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
*/
public static byte [] writeFile(Flosser flosser, ContentName completeName, int fileLength, Random randBytes) throws IOException, NoSuchAlgorithmException {
flosser.handleNamespace(completeName);
CCNOutputStream stockOutputStream = new CCNOutputStream(completeName, outputHandle);
DigestOutputStream digestStreamWrapper = new DigestOutputStream(stockOutputStream, MessageDigest.getInstance("SHA1"));
byte [] bytes = new byte[BUF_SIZE];
int elapsed = 0;
int nextBufSize = 0;
Log.info(Log.FAC_TEST, "Writing file: " + completeName + " bytes: " + fileLength);
final double probFlush = .3;
while (elapsed < fileLength) {
nextBufSize = ((fileLength - elapsed) > BUF_SIZE) ? BUF_SIZE : (fileLength - elapsed);
randBytes.nextBytes(bytes);
digestStreamWrapper.write(bytes, 0, nextBufSize);
elapsed += nextBufSize;
Log.info(completeName + " wrote " + elapsed + " out of " + fileLength + " bytes.");
if (randBytes.nextDouble() < probFlush) {
Log.info(Log.FAC_TEST, "Flushing buffers.");
digestStreamWrapper.flush();
}
}
digestStreamWrapper.close();
Log.info(Log.FAC_TEST, "Finished writing file " + completeName);
return digestStreamWrapper.getMessageDigest().digest();
}
示例14: writeToFile
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* Writes the data to a CSV file and generates a signature hash.
* Then it puts both of this into a ZIP archive file.
*
* @param csvEntries The {@link ExportEntry} to be exported.
* @throws IOException Thrown if the SHA-512 hash algorithm is missing.
*/
@Override
protected void writeToFile(final List<ExportEntry> csvEntries) throws IOException {
// Setup compression stuff
FileOutputStream fileOutputStream = getFileOutputStream();
final int zipCompressionLevel = 9;
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
zipOutputStream.setMethod(ZipOutputStream.DEFLATED);
zipOutputStream.setLevel(zipCompressionLevel);
// Setup signature
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-512");
} catch (NoSuchAlgorithmException exception) {
LOG.log(Level.SEVERE, "Missing hash algorithm for signature. No file exported!", exception);
throw new IOException("Missing hash algorithm for signature.");
}
DigestOutputStream digestOutputStream = new DigestOutputStream(zipOutputStream, digest);
// write data
zipOutputStream.putNextEntry(new ZipEntry(DATA_ZIP_ENTRY));
CsvWriter cwriter = new CsvWriter(digestOutputStream, VaultCsvEntry.CSV_DELIMITER,
Charset.forName("UTF-8"));
cwriter.writeRecord(((CsvEntry) csvEntries.get(0)).getCsvHeaderRecord());
for (ExportEntry item : csvEntries) {
cwriter.writeRecord(((CsvEntry) item).toCsvRecord());
}
cwriter.flush();
// add signature file
zipOutputStream.putNextEntry(new ZipEntry(SIGNATURE_ZIP_ENTRY));
String sigString = (new HexBinaryAdapter()).marshal(digest.digest());
zipOutputStream.write(sigString.getBytes("UTF-8"), 0, sigString.getBytes("UTF-8").length);
// Closing the streams
cwriter.close();
digestOutputStream.close();
zipOutputStream.close();
fileOutputStream.close();
}
示例15: copyTableAsJsonByte
import java.security.DigestOutputStream; //导入方法依赖的package包/类
public ByteArrayOutputStream copyTableAsJsonByte(Cursor cursor) {
ByteArrayOutputStream bufStream = null;
// Do copy Table
try {
if (cursor.getCount() > 0) {
// Config
MessageDigest md = getMessageDigest();
List<String> ignoreFields = Arrays.asList(new String[] { PersonColumns.COL_ID });
// Init Is
bufStream = new ByteArrayOutputStream(IS_BUFFER_SIZE);
GZIPOutputStream gzipOut = new GZIPOutputStream(bufStream, IS_BUFFER_SIZE);
// Prepare Disgester
DigestOutputStream digesterIs = new DigestOutputStream(gzipOut, md);
try {
// Json writer
JsonFactory f = new JsonFactory();
JsonGenerator g = f.createGenerator(digesterIs, JsonEncoding.UTF16_BE);
try {
g.writeStartArray();
while (cursor.moveToNext()) {
g.writeStartObject();
// Write Datas
ContentValues values = getCursorAsContentValues(cursor);
UpgradeDbHelper.writeLineToJson(g, values, ignoreFields);
// End Lines
g.writeEndObject();
}
g.writeEndArray();
// important: will force flushing of output, close
// underlying output stream
} finally {
g.close();
}
} finally {
// Close Streams
digesterIs.close();
gzipOut.close();
bufStream.close();
}
// Digest
// String digestString = new String( md.digest(), Charset.forName("UTF-8"));
// BigInteger digesterInt = new BigInteger(md.digest());
// String digestString = digesterInt.toString(16);
String digestString = new String ( Hex.encode(md.digest()));
Log.d(TAG, "Backup Md5 : " + digestString);
}
} catch (IOException e) {
Log.e(TAG, "Backup IOException : " + e.getMessage(), e);
}
return bufStream;
}