java.security.DigestInputStream类的setMessageDigest()方法用于将指定的MessageDigest分配给DigestInputStream对象。
用法:
public void setMessageDigest(MessageDigest digest)
返回值:此方法无返回值。
注意:由于不存在“名称”文件,因此本文中的所有程序都无法在在线IDE上运行。您可以在系统上的Java编译器上检查此代码。要检查此代码,请在系统上创建一个文件“名称”。
下面是说明setMessageDigest()方法的示例:
示例1:
// Java program to demonstrate
// setMessageDigest() method
import java.security.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// creating the object of MessageDigest
// and getting instance
// By usng getInstance() method
MessageDigest sr
= MessageDigest.getInstance("MD5");
// creating and intializing
// object of InputStream
InputStream is
= new FileInputStream(
"f:/java/name.txt");
// creating and intializing
// object of DigestInputStream
DigestInputStream di
= new DigestInputStream(is, sr);
// display the result
System.out.println(
"MessageDigest before assigning : "
+ di.getMessageDigest());
// assigning the MessageDigest to this stream
// using setMessageDigest() method
di.setMessageDigest(
MessageDigest
.getInstance("SHA-1"));
// display the result
System.out.println(
"MessageDigest after assigning : "
+ di.getMessageDigest());
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
catch (FileNotFoundException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
MessageDigest before assigning : MD5 Message Digest from SUN, MessageDigest after assigning : SHA-1 Message Digest from SUN,
示例2:
// Java program to demonstrate
// setMessageDigest() method
import java.security.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// creating the object of MessageDigest
// and getting instance
// By usng getInstance() method
MessageDigest sr
= MessageDigest.getInstance("SHA-1");
// creating and intializing
// object of InputStream
InputStream is
= new FileInputStream(
"f:/java/name.txt");
// creating and intializing
// object of DigestInputStream
DigestInputStream di
= new DigestInputStream(is, sr);
// display the result
System.out.println(
"MessageDigest before assigning : "
+ di.getMessageDigest());
// assigning the MessageDigest to this stream
// using setMessageDigest() method
di.setMessageDigest(
MessageDigest.getInstance("MD5"));
// display the result
System.out.println(
"MessageDigest after assigning : "
+ di.getMessageDigest());
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
catch (FileNotFoundException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
MessageDigest before assigning : SHA-1 Message Digest from SUN, MessageDigest after assigning : MD5 Message Digest from SUN,
相关用法
- Java Java lang.Long.highestOneBit()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Java lang.Long.builtcount()用法及代码示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
- Java Clock withZone()用法及代码示例
- Java Clock tickMinutes()用法及代码示例
- Java Map get()用法及代码示例
- Java Set contains()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 DigestInputStream.setMessageDigest() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。