本文整理汇总了Java中java.security.DigestOutputStream.on方法的典型用法代码示例。如果您正苦于以下问题:Java DigestOutputStream.on方法的具体用法?Java DigestOutputStream.on怎么用?Java DigestOutputStream.on使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.DigestOutputStream
的用法示例。
在下文中一共展示了DigestOutputStream.on方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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));
}
示例4: 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);
}
}
示例5: 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");
}
示例6: testWriteint05
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* Test #5 for <code>write(int)</code> method<br>
* Test #2 for <code>on(boolean)</code> method<br>
*
* Assertion: broken <code>DigestOutputStream</code>instance:
* associated <code>MessageDigest</code> not set.
* <code>write(int)</code> must work when digest
* functionality is off
*/
public final void testWriteint05() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
DigestOutputStream dos = new DigestOutputStream(bos, null);
// set digest functionality to off
dos.on(false);
// the following must pass without any exception
for (int i=0; i<MY_MESSAGE_LEN; i++) {
dos.write(myMessage[i]);
}
// check that bytes have been written correctly
assertTrue(Arrays.equals(MDGoldenData.getMessage(), bos.toByteArray()));
}
示例7: assertEquals
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* Test #4 for <code>write(byte[],int,int)</code> method<br>
*
* Assertion: put bytes into output stream<br>
*
* Assertion: does not update associated digest if digest
* functionality is off<br>
*/
public final void test_write$BII_4()
throws NoSuchAlgorithmException,
IOException {
// check precondition
assertEquals(0, MY_MESSAGE_LEN % CHUNK_SIZE);
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);
// set digest functionality off
dos.on(false);
// write message by chunks
for (int i=0; i<MY_MESSAGE_LEN/CHUNK_SIZE; i++) {
dos.write(myMessage, i*CHUNK_SIZE, CHUNK_SIZE);
}
// check write
assertTrue("write", Arrays.equals(myMessage, bos.toByteArray()));
// check that associated digest has not been updated
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");
}
示例8: testOn
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* Test for <code>on()</code> method<br>
* Assertion: turns digest functionality on or off
*/
public final void testOn() 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);
// turn digest off
dos.on(false);
for (int i=0; i<MY_MESSAGE_LEN-1; i++) {
dos.write(myMessage[i]);
}
// turn digest on
dos.on(true);
// read remaining byte
dos.write(myMessage[MY_MESSAGE_LEN-1]);
byte[] digest = dos.getMessageDigest().digest();
// check that digest value has been
// updated by the last write(int) call
assertFalse(Arrays.equals(digest,MDGoldenData.getDigest(algorithmName[k])));
assertFalse(Arrays.equals(digest,MDGoldenData.getDigest(algorithmName[k]+"_NU")));
return;
} catch (NoSuchAlgorithmException e) {
// allowed failure
}
}
fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
示例9: copyResourceAscii
import java.security.DigestOutputStream; //导入方法依赖的package包/类
public static void copyResourceAscii(final String resourceName, final File file) throws IOException
{
final InputStreamReader reader = new InputStreamReader(ManagedFile.class.getResourceAsStream(resourceName));
try
{
final MessageDigest digest = getDigest();
final DigestOutputStream digestStream = new DigestOutputStream(new FileOutputStream(file), digest);
try
{
final OutputStreamWriter writer = new OutputStreamWriter(digestStream);
try
{
final char[] buffer = new char[BUFFERSIZE];
do
{
final int length = reader.read(buffer);
if (length >= 0)
{
writer.write(buffer, 0, length);
}
else
{
break;
}
}
while (true);
writer.write("\n");
writer.flush();
final BigInteger hashInt = new BigInteger(1, digest.digest());
digestStream.on(false);
digestStream.write('#');
digestStream.write(hashInt.toString(16).getBytes());
}
finally
{
writer.close();
}
}
finally
{
digestStream.close();
}
}
finally
{
reader.close();
}
}