本文整理汇总了Java中java.security.DigestOutputStream.write方法的典型用法代码示例。如果您正苦于以下问题:Java DigestOutputStream.write方法的具体用法?Java DigestOutputStream.write怎么用?Java DigestOutputStream.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.DigestOutputStream
的用法示例。
在下文中一共展示了DigestOutputStream.write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
示例2: 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);
}
}
示例3: 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");
}
示例4: testWriteint01
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* Test #1 for <code>write(int)</code> method<br>
*
* Assertion: writes the byte to the output stream<br>
* Assertion: updates associated digest<br>
*/
public final void testWriteint01()
throws IOException {
for (int k=0; k<algorithmName.length; k++) {
try {
MessageDigest md = MessageDigest.getInstance(algorithmName[k]);
ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
DigestOutputStream dos = new DigestOutputStream(bos, md);
for (int i=0; i<MY_MESSAGE_LEN; i++) {
dos.write(myMessage[i]);
}
// check that bytes have been written correctly
assertTrue("write", Arrays.equals(MDGoldenData.getMessage(), bos.toByteArray()));
// check that associated digest has been updated properly
assertTrue("update", Arrays.equals(dos.getMessageDigest().digest(),
MDGoldenData.getDigest(algorithmName[k])));
return;
} catch (NoSuchAlgorithmException e) {
// allowed failure
}
}
fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
示例5: for
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* Test #1 for <code>write(byte[],int,int)</code> method<br>
*
* Assertion: put bytes into output stream<br>
*
* Assertion: updates associated digest<br>
*/
public final void test_write$BII_1() throws IOException {
for (int k=0; k<algorithmName.length; k++) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
MessageDigest md = MessageDigest.getInstance(algorithmName[k]);
DigestOutputStream dos = new DigestOutputStream(bos, md);
// write message at once
dos.write(myMessage, 0, MY_MESSAGE_LEN);
// check write
assertTrue("write", Arrays.equals(myMessage, bos.toByteArray()));
// check that associated digest has been updated properly
assertTrue("update", Arrays.equals(dos.getMessageDigest().digest(),
MDGoldenData.getDigest(algorithmName[k])));
return;
} catch (NoSuchAlgorithmException e) {
// allowed failure
}
}
fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
示例6: Support_OutputStream
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* java.io.DigestOutputStream#write(byte[], int, int)
*/
public void test_write$BII_7()
throws IOException, NoSuchAlgorithmException {
Support_OutputStream sos = new Support_OutputStream(MY_MESSAGE_LEN);
MessageDigest md = MessageDigest.getInstance(algorithmName[0]);
DigestOutputStream dos = new DigestOutputStream(sos, md);
dos.write(myMessage, 0, MY_MESSAGE_LEN);
try {
// Support_OutputStream throws an IOException if the internal
// buffer is full, which it should be now.
dos.write(myMessage, 0, MY_MESSAGE_LEN);
fail("Test 1: IOException expected.");
} catch (IOException e) {
// Expected.
}
}
示例7: test_onZ
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* java.security.DigestOutputStream#on(boolean)
*/
public void test_onZ() throws Exception {
// Test for method void java.security.DigestOutputStream.on(boolean)
DigestOutputStream dos = new DigestOutputStream(
new ByteArrayOutputStream(), MessageDigest.getInstance("SHA"));
dos.on(false);
byte digestArray[] = { 23, 43, 44 };
dos.write(digestArray, 1, 1);
byte digestResult[] = dos.getMessageDigest().digest();
byte expected[] = { -38, 57, -93, -18, 94, 107, 75, 13, 50, 85,
-65, -17, -107, 96, 24, -112, -81, -40, 7, 9 };
assertTrue("Digest did not return expected result.",
Arrays.equals(digestResult, expected));
// now turn on processing and re-run
dos.on(true);
dos.write(digestArray, 1, 1);
digestResult = dos.getMessageDigest().digest();
byte expected1[] = { -87, 121, -17, 16, -52, 111, 106, 54, -33,
107, -118, 50, 51, 7, -18, 59, -78, -30, -37, -100 };
assertTrue("Digest did not return expected result.",
Arrays.equals(digestResult, expected1));
}
示例8: hashContent
import java.security.DigestOutputStream; //导入方法依赖的package包/类
private byte[] hashContent(ModelNode content) throws IOException {
byte[] sha1Bytes;
OutputStream os = new OutputStream() {
@Override
public void write(int b) throws IOException {
// just discard
}
};
synchronized (messageDigest) {
messageDigest.reset();
DigestOutputStream dos = new DigestOutputStream(os, messageDigest);
ByteArrayInputStream bis = new ByteArrayInputStream(content.toString().getBytes(StandardCharsets.UTF_8));
byte[] bytes = new byte[8192];
int read;
while ((read = bis.read(bytes)) > -1) {
dos.write(bytes, 0, read);
}
sha1Bytes = messageDigest.digest();
}
return sha1Bytes;
}
示例9: 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);
}
}
示例10: getHashValue
import java.security.DigestOutputStream; //导入方法依赖的package包/类
private byte[] getHashValue(File file) throws Exception {
MessageDigest sha = MessageDigest.getInstance("SHA-256");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DigestOutputStream dout = new DigestOutputStream(baos, sha);
byte[] data = new byte[1024];
BufferedInputStream is = new BufferedInputStream(file.toURL().openStream());
while (true) {
int bytesRead = is.read(data);
if (bytesRead < 0)
break;
dout.write(data, 0, bytesRead);
}
dout.flush();
byte[] result = dout.getMessageDigest().digest();
return result;
}
示例11: 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;
}
示例12: 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;
}
示例13: assertEntityLinesMD5
import java.security.DigestOutputStream; //导入方法依赖的package包/类
private static void assertEntityLinesMD5(Results results, Session session, String expectedMD5)
{
try {
DigestOutputStream out = md5OutputStream(ByteStreams.nullOutputStream());
for (List<List<String>> parentAndChildRows : results) {
out.write(formatRow(parentAndChildRows.get(0), session).getBytes(ISO_8859_1));
}
byte[] md5Digest = out.getMessageDigest().digest();
assertEquals(base16().lowerCase().encode(md5Digest), expectedMD5);
}
catch (IOException e) {
throw Throwables.propagate(e);
}
}
示例14: 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;
}
示例15: testWriteint02
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* Test #2 for <code>write(int)</code> method<br>
* Test #1 for <code>on(boolean)</code> method<br>
*
* Assertion: <code>write(int)</code> must not update digest if it is off<br>
* Assertion: <code>on(boolean)</code> turns digest functionality on
* if <code>true</code> passed as a parameter or off if <code>false</code>
* passed
*/
public final void testWriteint02()
throws IOException {
for (int k=0; k<algorithmName.length; k++) {
try {
MessageDigest md = MessageDigest.getInstance(algorithmName[k]);
ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
DigestOutputStream dos = new DigestOutputStream(bos, md);
// turn digest off
dos.on(false);
for (int i=0; i<MY_MESSAGE_LEN; i++) {
dos.write(myMessage[i]);
}
// check that bytes have been written correctly
assertTrue("write", Arrays.equals(MDGoldenData.getMessage(), bos.toByteArray()));
// check that digest value has not been updated by write()
assertTrue("update", Arrays.equals(dos.getMessageDigest().digest(),
MDGoldenData.getDigest(algorithmName[k]+"_NU")));
return;
} catch (NoSuchAlgorithmException e) {
// allowed failure
}
}
fail(getName() + ": no MessageDigest algorithms available - test not performed");
}