专业制作网站费用,成都专业网站建设,深圳英文网站开发公司,没备案网站如何通过百度联盟审核MessageDigest类实现了消息摘要算法#xff0c;它继承于MessageDigestSpi类#xff0c;是Java安全提供者体系结构中最简单的一个引擎类。
在Java API的列表中#xff0c;总能看到后缀名带有SPI#xff08;Service Provider Interface#xff09;的类。如果要实现自定义的…MessageDigest类实现了消息摘要算法它继承于MessageDigestSpi类是Java安全提供者体系结构中最简单的一个引擎类。
在Java API的列表中总能看到后缀名带有SPIService Provider Interface的类。如果要实现自定义的消息摘要算法则需要提供MessageDigestSpi类的子类实现
使用示例
import java.security.MessageDigest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;/*** TODO 在此写上类的相关说明.br* author gqlttbr* version 1.0.0 2021年11月20日br* see * since JDK 1.5.0*/
public class MessageDigestDemo {/*** param args*/public static void main(String[] args) {String text 原始文本待生成摘要;String digest1 useUpdate(text);String digest2 digest(text);String digest3 useProvider(text);System.out.println(digest1);System.out.println(digest1.equals(digest2));System.out.println(digest1.equals(digest3));}/*** 使用update.* param text* return*/static String useUpdate(final String text) {try {MessageDigest msgDigest MessageDigest.getInstance(MD5);msgDigest.update(text.getBytes());return HexUtil.byteArrToHex(msgDigest.digest());} catch (Exception e) {throw new RuntimeException(e);}}/*** 直接digest.* param text* return*/static String digest(final String text) {try {MessageDigest msgDigest MessageDigest.getInstance(MD5);return HexUtil.byteArrToHex(msgDigest.digest(text.getBytes()));} catch (Exception e) {throw new RuntimeException(e);}}/*** 使用指定Provider.* param text* return*/static String useProvider(final String text) {try {MessageDigest msgDigest MessageDigest.getInstance(MD5, new BouncyCastleProvider());return HexUtil.byteArrToHex(msgDigest.digest(text.getBytes()));} catch (Exception e) {throw new RuntimeException(e);}}
}