MessageDigest详解

MessageDigest详解一、概述java.security.MessageDigest类用于为应用程序提供信息摘要算法的功能,如MD5或SHA算法。简单点说就是用于生成散列码。信息摘要是安全的单向哈希函数,它接收任意大小的数据,输出固定长度的哈希值。关于信息摘要和散列码请参照《数字证书简介》MessageDigest 通过其getInstance系列静态函数来进行实例化和初始化。MessageDigest对象通…

大家好,又见面了,我是你们的朋友全栈君。

一、概述
java.security.
MessageDigest
类用于为应用程序提供信息摘要算法的功能,如 MD5 或 SHA 算法。简单点说就是用于生成 散列码。
信息摘要是安全的单向哈希函数,它接收任意大小的数据,输出固定长度的哈希值。关于
信息摘要 和 散列码 请参照《
数字证书简介

MessageDigest 通过其getInstance系列静态函数来进行实例化和初始化。MessageDigest 对象通过使用 update 方法处理数据。任何时候都可以调用 reset 方法重置摘要。一旦所有需要更新的数据都已经被更新了,应该调用 digest 方法之一完成哈希计算并返回结果。

对于给定数量的更新数据,digest 方法只能被调用一次。digest 方法被调用后,MessageDigest  对象被重新设置成其初始状态。

MessageDigest 的实现可随意选择是否实现 Cloneable 接口。客户端应用程可以通过尝试复制和捕获 CloneNotSupportedException 测试可复制性:

<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln"> </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pln"> md </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pun" style="color:#66660;">.</span><span class="pln">getInstance</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"SHA"</span><span class="pun" style="color:#66660;">);</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln"> </span><span class="kwd" style="color:#0088;">try</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">     md</span><span class="pun" style="color:#66660;">.</span><span class="pln">update</span><span class="pun" style="color:#66660;">(</span><span class="pln">toChapter1</span><span class="pun" style="color:#66660;">);</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">     </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pln"> tc1 </span><span class="pun" style="color:#66660;">=</span><span class="pln"> md</span><span class="pun" style="color:#66660;">.</span><span class="pln">clone</span><span class="pun" style="color:#66660;">();</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">     </span><span class="kwd" style="color:#0088;">byte</span><span class="pun" style="color:#66660;">[]</span><span class="pln"> toChapter1Digest </span><span class="pun" style="color:#66660;">=</span><span class="pln"> tc1</span><span class="pun" style="color:#66660;">.</span><span class="pln">digest</span><span class="pun" style="color:#66660;">();</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">     md</span><span class="pun" style="color:#66660;">.</span><span class="pln">update</span><span class="pun" style="color:#66660;">(</span><span class="pln">toChapter2</span><span class="pun" style="color:#66660;">);</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">     </span><span class="pun" style="color:#66660;">...</span><span class="pln">etc</span><span class="pun" style="color:#66660;">.</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln"> </span><span class="pun" style="color:#66660;">}</span><span class="pln"> </span><span class="kwd" style="color:#0088;">catch</span><span class="pln"> </span><span class="pun" style="color:#66660;">(</span><span class="typ" style="color:#66066;">CloneNotSupportedException</span><span class="pln"> cnse</span><span class="pun" style="color:#66660;">)</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">     </span><span class="kwd" style="color:#0088;">throw</span><span class="pln"> </span><span class="kwd" style="color:#0088;">new</span><span class="pln"> </span><span class="typ" style="color:#66066;">DigestException</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"couldn't make digest of partial content"</span><span class="pun" style="color:#66660;">);</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln"> </span><span class="pun" style="color:#66660;">}</span></p>

注意1:即时给定MessageDigest的实现是不可复制的,则仍然能够通过getInstance方法实例化几个实例计算来同时进行摘要信息的计算。

注意2:由于历史原因,此类是抽象的,是从 MessageDigestSpi 扩展的。应用程序开发人员只应该注意在此 MessageDigest 类中定义的方法;超类中的所有方法是供希望提供自己的信息摘要算法实现的加密服务提供者使用的。 

注意3MessageDigest并不是单实例的。如下代码所示:

<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln"> </span><span class="pln">        </span><span class="kwd" style="color:#0088;">try</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">            </span><span class="pun" style="color:#66660;">{</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">                </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pln"> mdTemp1 </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pun" style="color:#66660;">.</span><span class="pln">getInstance</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"MD5"</span><span class="pun" style="color:#66660;">);</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">                </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pln"> mdTemp2</span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pun" style="color:#66660;">.</span><span class="pln">getInstance</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"MD5"</span><span class="pun" style="color:#66660;">);</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">                </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pln"> mdTemp3</span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pun" style="color:#66660;">.</span><span class="pln">getInstance</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"MD5"</span><span class="pun" style="color:#66660;">);</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">                </span><span class="typ" style="color:#66066;">System</span><span class="pun" style="color:#66660;">.</span><span class="kwd" style="color:#0088;">out</span><span class="pun" style="color:#66660;">.</span><span class="pln">println</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"mdTemp1==mdTemp2?:"</span><span class="pun" style="color:#66660;">+(</span><span class="pln">mdTemp1</span><span class="pun" style="color:#66660;">==</span><span class="pln">mdTemp2</span><span class="pun" style="color:#66660;">));</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">                </span><span class="typ" style="color:#66066;">System</span><span class="pun" style="color:#66660;">.</span><span class="kwd" style="color:#0088;">out</span><span class="pun" style="color:#66660;">.</span><span class="pln">println</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"mdTemp2==mdTemp3?:"</span><span class="pun" style="color:#66660;">+(</span><span class="pln">mdTemp2</span><span class="pun" style="color:#66660;">==</span><span class="pln">mdTemp3</span><span class="pun" style="color:#66660;">));</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">            </span><span class="pun" style="color:#66660;">}</span><span class="pln"> </span><span class="kwd" style="color:#0088;">catch</span><span class="pln"> </span><span class="pun" style="color:#66660;">(</span><span class="typ" style="color:#66066;">NoSuchAlgorithmException</span><span class="pln"> e</span><span class="pun" style="color:#66660;">)</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">            </span><span class="pun" style="color:#66660;">{</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">                </span><span class="com" style="color:#8800;">// TODO Auto-generated catch block</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="com" style="color:#8800;">                e.printStackTrace();</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="com" style="color:#8800;">            }</span></p>

运行结果

<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">mdTemp1</span><span class="pun" style="color:#66660;">==</span><span class="pln">mdTemp2</span><span class="pun" style="color:#66660;">?:</span><span class="pln">false</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">mdTemp2</span><span class="pun" style="color:#66660;">==</span><span class="pln">mdTemp3</span><span class="pun" style="color:#66660;">?:</span><span class="kwd" style="color:#0088;">false</span></p>
构造方法摘要
protectedMessageDigest(String algorithm) 
          创建具有指定算法名称的MessageDigest 实例对象

 

方法摘要
 Object clone() 
          如果实现是可复制的,则返回一个副本。
 byte[] digest() 
          通过执行诸如填充之类的最终操作完成哈希计算。
 byte[] digest(byte[] input) 
          使用指定的字节数组对摘要进行最后更新,然后完成摘要计算。
 int digest(byte[] buf, int offset, int len) 
          通过执行诸如填充之类的最终操作完成哈希计算。
 String getAlgorithm() 
          返回标识算法的独立于实现细节的字符串。
 int getDigestLength() 
          返回以字节为单位的摘要长度,如果提供程序不支持此操作并且实现是不可复制的,则返回 0。
static MessageDigest getInstance(String algorithm) 
          生成实现指定摘要算法的 MessageDigest 对象。
static MessageDigest getInstance(String algorithm, Provider provider) 
          生成实现指定提供程序提供的指定算法的 MessageDigest 对象,如果该算法可从指定的提供程序得到的话。
static MessageDigest getInstance(String algorithm, String provider) 
          生成实现指定提供程序提供的指定算法的 MessageDigest 对象,如果该算法可从指定的提供程序得到的话。
 Provider getProvider() 
          返回此信息摘要对象的提供程序。
static boolean isEqual(byte[] digesta, byte[] digestb) 
          比较两个摘要的相等性。
 void reset() 
          重置摘要以供再次使用。
 String toString() 
          返回此信息摘要对象的字符串表示形式。
 void update(byte input) 
          使用指定的字节更新摘要。
 void update(byte[] input) 
          使用指定的字节数组更新摘要。
 void update(byte[] input, int offset, int len) 
          使用指定的字节数组,从指定的偏移量开始更新摘要。
 void update(ByteBuffer input) 
          使用指定的 ByteBuffer 更新摘要。
二、实际实践

2.1、创建 
MessageDigest 
对象
计算信息摘(即 散列码 )要做的第一步是创建 
MessageDigest
对象  实例。像所有的引擎类一样,获取某类报文摘要算法(即
散列算法 ,比如
MD5 )的  
MessageDigest  对象的途径是调用 
MessageDigest  类中的 
getInstance  静态 
factory  方法:
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">    </span><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">static</span><span class="pln"> </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pln"> getInstance</span><span class="pun" style="color:#66660;">(</span><span class="typ" style="color:#66066;">String</span><span class="pln"> algorithm</span><span class="pun" style="color:#66660;">)</span></p>

注意:算法名不区分大小写。例如,以下所有调用都是相等的:
<div><span class="typ" style="color:#66066;">MessageDigest</span><span class="pun" style="color:#66660;">.</span><span class="pln">getInstance</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"SHA"</span><span class="pun" style="color:#66660;">);</span></div><div><span class="typ" style="color:#66066;">MessageDigest</span><span class="pun" style="color:#66660;">.</span><span class="pln">getInstance</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"sha"</span><span class="pun" style="color:#66660;">);</span></div><div><span class="typ" style="color:#66066;">MessageDigest</span><span class="pun" style="color:#66660;">.</span><span class="pln">getInstance</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"sHa"</span><span class="pun" style="color:#66660;">);</span></div>

调用程序可选择指定提供者名称,以保证所要求的算法是由已命名提供者实现的:
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">static</span><span class="pln"> </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pln"> getInstance</span><span class="pun" style="color:#66660;">(</span><span class="typ" style="color:#66066;">String</span><span class="pln"> algorithm</span><span class="pun" style="color:#66660;">,</span><span class="pln"> </span><span class="typ" style="color:#66066;">String</span><span class="pln"> provider</span><span class="pun" style="color:#66660;">);</span></p>

调用 getInstance 将返回已初始化过的
MessageDigest对象。因此,它不需要进一步的初始化。
2.2、向
MessageDigest
传送要计算的数据
计算数据的摘要的第二步是向已初始化的
MessageDigest对象提供传送要计算的数据。这将通过一次或多次调用以下某个 
update(更新)方法来完成:
<div><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">void</span><span class="pln"> update</span><span class="pun" style="color:#66660;">(</span><span class="kwd" style="color:#0088;">byte</span><span class="pln"> input</span><span class="pun" style="color:#66660;">);</span></div><div><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">void</span><span class="pln"> update</span><span class="pun" style="color:#66660;">(</span><span class="kwd" style="color:#0088;">byte</span><span class="pun" style="color:#66660;">[]</span><span class="pln"> input</span><span class="pun" style="color:#66660;">);</span></div><div><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">void</span><span class="pln"> update</span><span class="pun" style="color:#66660;">(</span><span class="kwd" style="color:#0088;">byte</span><span class="pun" style="color:#66660;">[]</span><span class="pln"> input</span><span class="pun" style="color:#66660;">,</span><span class="pln"> </span><span class="kwd" style="color:#0088;">int</span><span class="pln"> offset</span><span class="pun" style="color:#66660;">,</span><span class="pln"> </span><span class="kwd" style="color:#0088;">int</span><span class="pln"> len</span><span class="pun" style="color:#66660;">);</span></div>

2.3、计算摘要
通过调用 update 方法向
MessageDigest对象
传送要计算的数据后,你就可以调用以下某个 digest(摘要)方法来计算摘要(即
生成 散列码):
<div><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">byte</span><span class="pun" style="color:#66660;">[]</span><span class="pln"> digest</span><span class="pun" style="color:#66660;">();</span></div><div><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">byte</span><span class="pun" style="color:#66660;">[]</span><span class="pln"> digest</span><span class="pun" style="color:#66660;">(</span><span class="kwd" style="color:#0088;">byte</span><span class="pun" style="color:#66660;">[]</span><span class="pln"> input</span><span class="pun" style="color:#66660;">);</span></div><div><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">int</span><span class="pln"> digest</span><span class="pun" style="color:#66660;">(</span><span class="kwd" style="color:#0088;">byte</span><span class="pun" style="color:#66660;">[]</span><span class="pln"> buf</span><span class="pun" style="color:#66660;">,</span><span class="pln"> </span><span class="kwd" style="color:#0088;">int</span><span class="pln"> offset</span><span class="pun" style="color:#66660;">,</span><span class="pln"> </span><span class="kwd" style="color:#0088;">int</span><span class="pln"> len</span><span class="pun" style="color:#66660;">);</span></div>

前两个方法返回计算出的摘要。后一个方法把计算出的摘要储存在所提供的 buf 缓冲区中,起点是 offset。len 是 buf 中分配给该摘要的字节数。该方法返回实际存储在 buf 中的字节数。
对第二个接受输入字节数组变量的 digest 方法的调用等价于用指定的输入调用:
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">    </span><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">void</span><span class="pln"> update</span><span class="pun" style="color:#66660;">(</span><span class="kwd" style="color:#0088;">byte</span><span class="pun" style="color:#66660;">[]</span><span class="pln"> input</span><span class="pun" style="color:#66660;">)</span></p>

,接着调用不带参数的 digest 方法.

三、例子演示

3.1、★ 编程思路:
java.security包中的
MessageDigest类提供了计算消息摘要

即生成 散列码
的方法,首先生成对象,执行其
update( )方法可
以将原始数据传递给该对象,然后执行其
digest( )方法即可得到消息摘要。具体步骤如下:
(1)生成MessageDigest对象
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="typ" style="color:#66066;">MessageDigest</span><span class="pln"> m</span><span class="pun" style="color:#66660;">=</span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pun" style="color:#66660;">.</span><span class="pln">getInstance</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"MD5"</span><span class="pun" style="color:#66660;">);</span></p>

MessageDigest类也是一个工厂类,其构造器是受保护的,不允许
直接使用new MessageDigist( )来创建对象,而必须通过其静态方法
getInstance( )生成
MessageDigest对象
其中传入的参数指定计算消息摘要所使用的算法,常用的有”
MD5“,”
SHA“等。
(2)传入需要计算的字符串
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">m</span><span class="pun" style="color:#66660;">.</span><span class="pln">update</span><span class="pun" style="color:#66660;">(</span><span class="pln">x</span><span class="pun" style="color:#66660;">.</span><span class="pln">getBytes</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"UTF8"</span><span class="pln"> </span><span class="pun" style="color:#66660;">));</span></p>

分析:x为需要计算的字符串,update传入的参数是字节类型或字节类型数组,对于字符串,需要先使用getBytes( )方法生成字符串数组。
(3)计算消息摘要
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="kwd" style="color:#0088;">byte</span><span class="pln"> s</span><span class="pun" style="color:#66660;">[</span><span class="pln"> </span><span class="pun" style="color:#66660;">]=</span><span class="pln">m</span><span class="pun" style="color:#66660;">.</span><span class="pln">digest</span><span class="pun" style="color:#66660;">(</span><span class="pln"> </span><span class="pun" style="color:#66660;">);</span></p>

分析:执行MessageDigest对象的digest( )方法完成计算,计算的结果通过字节类型的数组返回。
(4)处理计算结果
必要的话可以使用如下代码将计算结果(byte数组)转换为字符串。
<div><span class="pln"> </span><span class="kwd" style="color:#0088;">static</span><span class="pln"> </span><span class="typ" style="color:#66066;">String</span><span class="pln"> convertToHexString</span><span class="pun" style="color:#66660;">(</span><span class="kwd" style="color:#0088;">byte</span><span class="pln"> data</span><span class="pun" style="color:#66660;">[])</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span></div><div><span class="pln">  </span><span class="typ" style="color:#66066;">StringBuffer</span><span class="pln"> strBuffer </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="kwd" style="color:#0088;">new</span><span class="pln"> </span><span class="typ" style="color:#66066;">StringBuffer</span><span class="pun" style="color:#66660;">();</span></div><div><span class="pln">  </span><span class="kwd" style="color:#0088;">for</span><span class="pln"> </span><span class="pun" style="color:#66660;">(</span><span class="kwd" style="color:#0088;">int</span><span class="pln"> i </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="lit" style="color:#06666;">0</span><span class="pun" style="color:#66660;">;</span><span class="pln"> i </span><span class="pun" style="color:#66660;"><</span><span class="pln"> data</span><span class="pun" style="color:#66660;">.</span><span class="pln">length</span><span class="pun" style="color:#66660;">;</span><span class="pln"> i</span><span class="pun" style="color:#66660;">++)</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span></div><div><span class="pln">   </span><span class="pln">strBuffer</span><span class="pun" style="color:#66660;">.</span><span class="pln">append</span><span class="pun" style="color:#66660;">(</span><span class="typ" style="color:#66066;">Integer</span><span class="pun" style="color:#66660;">.</span><span class="pln">toHexString</span><span class="pun" style="color:#66660;">(</span><span class="lit" style="color:#06666;">0xff</span><span class="pln"> </span><span class="pun" style="color:#66660;">&</span><span class="pln"> data</span><span class="pun" style="color:#66660;">[</span><span class="pln">i</span><span class="pun" style="color:#66660;">]));</span></div><div><span class="pln">  </span><span class="pun" style="color:#66660;">}</span></div><div><span class="pln">  </span><span class="kwd" style="color:#0088;">return</span><span class="pln"> strBuffer</span><span class="pun" style="color:#66660;">.</span><span class="pln">toString</span><span class="pun" style="color:#66660;">();</span></div><div><span class="pln"> </span><span class="pun" style="color:#66660;">}</span></div>

3.2、示例一
完整程序如下:
<div><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">class</span><span class="pln"> </span><span class="typ" style="color:#66066;">MessageDigestDemo</span><span class="pln"> </span><span class="kwd" style="color:#0088;">extends</span><span class="pln"> </span><span class="typ" style="color:#66066;">Thread</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span></div><div><span class="pln"> </span><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">void</span><span class="pln"> run</span><span class="pun" style="color:#66660;">()</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span></div><div><span class="pln"> </span><span class="typ" style="color:#66066;">String</span><span class="pln"> text </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="str" style="color:#0880;">"abc"</span><span class="pun" style="color:#66660;">;</span></div><div><span class="pln"> </span><span class="kwd" style="color:#0088;">byte</span><span class="pln"> data</span><span class="pun" style="color:#66660;">[]</span><span class="pln"> </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="kwd" style="color:#0088;">null</span><span class="pun" style="color:#66660;">;</span></div><div><span class="pln"> </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pln"> m</span><span class="pun" style="color:#66660;">;</span></div><div><span class="pln"> </span><span class="kwd" style="color:#0088;">try</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span></div><div><span class="pln"> </span><span class="pln">data </span><span class="pun" style="color:#66660;">=</span><span class="pln"> text</span><span class="pun" style="color:#66660;">.</span><span class="pln">getBytes</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"UTF8"</span><span class="pun" style="color:#66660;">);</span></div><div><span class="pln"> </span><span class="pln">m </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pun" style="color:#66660;">.</span><span class="pln">getInstance</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"MD5"</span><span class="pun" style="color:#66660;">);</span></div><div><span class="pln"> </span><span class="pln">m</span><span class="pun" style="color:#66660;">.</span><span class="pln">update</span><span class="pun" style="color:#66660;">(</span><span class="pln">data</span><span class="pun" style="color:#66660;">);</span></div><div><span class="pln"> </span><span class="kwd" style="color:#0088;">byte</span><span class="pln"> resultData</span><span class="pun" style="color:#66660;">[]</span><span class="pln"> </span><span class="pun" style="color:#66660;">=</span><span class="pln"> m</span><span class="pun" style="color:#66660;">.</span><span class="pln">digest</span><span class="pun" style="color:#66660;">();</span></div><div><span class="pln"> </span><span class="typ" style="color:#66066;">System</span><span class="pun" style="color:#66660;">.</span><span class="kwd" style="color:#0088;">out</span><span class="pun" style="color:#66660;">.</span><span class="pln">println</span><span class="pun" style="color:#66660;">(</span><span class="pln">convertToHexString</span><span class="pun" style="color:#66660;">(</span><span class="pln">resultData</span><span class="pun" style="color:#66660;">));</span></div><div><span class="pln"> </span><span class="pun" style="color:#66660;">}</span><span class="pln"> </span><span class="kwd" style="color:#0088;">catch</span><span class="pln"> </span><span class="pun" style="color:#66660;">(</span><span class="typ" style="color:#66066;">NoSuchAlgorithmException</span><span class="pln"> e</span><span class="pun" style="color:#66660;">)</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span></div><div><span class="pln"> </span><span class="com" style="color:#8800;">// TODO Auto-generated catch block</span></div><div><span class="com" style="color:#8800;"> </span><span class="com" style="color:#8800;">e.printStackTrace();</span></div><div><span class="com" style="color:#8800;"> </span><span class="com" style="color:#8800;">} catch (UnsupportedEncodingException e) {</span></div><div><span class="com" style="color:#8800;"> </span><span class="com" style="color:#8800;">// TODO Auto-generated catch block</span></div><div><span class="com" style="color:#8800;"> </span><span class="com" style="color:#8800;">e.printStackTrace();</span></div><div><span class="com" style="color:#8800;"> </span><span class="com" style="color:#8800;">}</span></div><div> </div><div><span class="pln"> </span><span class="pun" style="color:#66660;">}</span></div><div> </div><div><span class="pln"> </span><span class="kwd" style="color:#0088;">static</span><span class="pln"> </span><span class="typ" style="color:#66066;">String</span><span class="pln"> convertToHexString</span><span class="pun" style="color:#66660;">(</span><span class="kwd" style="color:#0088;">byte</span><span class="pln"> data</span><span class="pun" style="color:#66660;">[])</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span></div><div><span class="pln"> </span><span class="typ" style="color:#66066;">StringBuffer</span><span class="pln"> strBuffer </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="kwd" style="color:#0088;">new</span><span class="pln"> </span><span class="typ" style="color:#66066;">StringBuffer</span><span class="pun" style="color:#66660;">();</span></div><div><span class="pln"> </span><span class="kwd" style="color:#0088;">for</span><span class="pln"> </span><span class="pun" style="color:#66660;">(</span><span class="kwd" style="color:#0088;">int</span><span class="pln"> i </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="lit" style="color:#06666;">0</span><span class="pun" style="color:#66660;">;</span><span class="pln"> i </span><span class="pun" style="color:#66660;"><</span><span class="pln"> data</span><span class="pun" style="color:#66660;">.</span><span class="pln">length</span><span class="pun" style="color:#66660;">;</span><span class="pln"> i</span><span class="pun" style="color:#66660;">++)</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span></div><div><span class="pln"> </span><span class="pln">strBuffer</span><span class="pun" style="color:#66660;">.</span><span class="pln">append</span><span class="pun" style="color:#66660;">(</span><span class="typ" style="color:#66066;">Integer</span><span class="pun" style="color:#66660;">.</span><span class="pln">toHexString</span><span class="pun" style="color:#66660;">(</span><span class="lit" style="color:#06666;">0xff</span><span class="pln"> </span><span class="pun" style="color:#66660;">&</span><span class="pln"> data</span><span class="pun" style="color:#66660;">[</span><span class="pln">i</span><span class="pun" style="color:#66660;">]));</span></div><div><span class="pln"> </span><span class="pun" style="color:#66660;">}</span></div><div><span class="pln"> </span><span class="kwd" style="color:#0088;">return</span><span class="pln"> strBuffer</span><span class="pun" style="color:#66660;">.</span><span class="pln">toString</span><span class="pun" style="color:#66660;">();</span></div><div><span class="pln"> </span><span class="pun" style="color:#66660;">}</span></div><div><span class="pun" style="color:#66660;">}</span></div>

★运行结果
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="lit" style="color:#06666;">900150983cd24fb0d6963f7d28e17f72</span></p>

 3.3、示例二
在这里我们将对计算生成的md5使用
 sun.misc.BASE64Encoder进行简单的加密。
<div><span class="pln">    </span><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="typ" style="color:#66066;">String</span><span class="pln"> md5sumWithEncoder</span><span class="pun" style="color:#66660;">(</span><span class="typ" style="color:#66066;">String</span><span class="pln"> text</span><span class="pun" style="color:#66660;">)</span><span class="pln"> </span><span class="kwd" style="color:#0088;">throws</span><span class="pln"> </span><span class="typ" style="color:#66066;">NoSuchAlgorithmException</span><span class="pun" style="color:#66660;">,</span><span class="pln"> </span></div><div><span class="typ" style="color:#66066;">UnsupportedEncodingException</span><span class="pun" style="color:#66660;">{</span></div><div><span class="pln">        </span><span class="com" style="color:#8800;">/*确定计算方法*/</span></div><div><span class="pln">        </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pln"> md5</span><span class="pun" style="color:#66660;">=</span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pun" style="color:#66660;">.</span><span class="pln">getInstance</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"MD5"</span><span class="pun" style="color:#66660;">);</span></div><div><span class="pln">        BASE64Encoder base64en </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="kwd" style="color:#0088;">new</span><span class="pln"> BASE64Encoder</span><span class="pun" style="color:#66660;">();</span></div><div><span class="pln">        </span><span class="com" style="color:#8800;">/*加密后的散列码字符串*/</span></div><div><span class="pln">        </span><span class="typ" style="color:#66066;">String</span><span class="pln"> strMd5</span><span class="pun" style="color:#66660;">=</span><span class="pln">base64en</span><span class="pun" style="color:#66660;">.</span><span class="pln">encode</span><span class="pun" style="color:#66660;">(</span><span class="pln">md5</span><span class="pun" style="color:#66660;">.</span><span class="pln">digest</span><span class="pun" style="color:#66660;">(</span><span class="pln">text</span><span class="pun" style="color:#66660;">.</span><span class="pln">getBytes</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"utf-8"</span><span class="pun" style="color:#66660;">)));</span></div><div><span class="pln">        </span><span class="kwd" style="color:#0088;">return</span><span class="pln"> strMd5</span><span class="pun" style="color:#66660;">;</span></div><div><span class="pln">    </span><span class="pun" style="color:#66660;">}</span></div>

调用函数
<div><span class="typ" style="color:#66066;">String</span><span class="pln"> str</span><span class="pun" style="color:#66660;">=</span><span class="str" style="color:#0880;">"0123456789"</span></div><div><span class="pln"> </span><span class="typ" style="color:#66066;">System</span><span class="pun" style="color:#66660;">.</span><span class="kwd" style="color:#0088;">out</span><span class="pun" style="color:#66660;">.</span><span class="pln">println</span><span class="pun" style="color:#66660;">(</span><span style="line-height:22px"><span class="pln" style="line-height:25px">md5sumWithEncoder</span></span><span class="pun" style="color:#66660;">(</span><span class="pln">str</span><span class="pun" style="color:#66660;">));</span></div>

 输出
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/147835.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Pytroch入坑 3. 自己的人脸数据+迁移学习(resnet18)

    Pytroch入坑 3. 自己的人脸数据+迁移学习(resnet18)本文转载自:http://www.zhongruitech.com/856941441.html0.前言之前是使用了mnist数据,且网络结构比较简单,针对自己的数据,如何使用更复杂、经典的网络呢?1.数据集目标是人脸识别,可以看做一个多分类问题,本次实验的数据集为ferest,共200个人,1400张38080图片,比较小。分为train和val两个目录,每个目录下都有200个…

  • java bi报表工具_7款顶级开源BI(商务智能)软件和报表工具

    java bi报表工具_7款顶级开源BI(商务智能)软件和报表工具在这个信息化时代,每分每秒都产生海量数据。在海量数据中,挖掘出有用的数据,并且能以较人性化、直观的方式展示这些数据,变得尤为重要。本文将介绍7款顶级开源BI(商务智能)软件和报表工具,用于商业数据的分析处理,希望对您有所帮助。BIRT(BusinessIntelligenceandReportingTools)是由IBM在2004年开源的基于Eclipse的报表系统,它主要是…

    2022年10月29日
  • 微信公众号发布提醒(微信公众号模板消息接口)

    1.项目简介灵感来源于学校的每日健康日报,要求使用微信小程序进行每日健康打卡。所以此项目的功能类似于QQ群机器人,或者是每日闹钟。功能描述:用户访问给定的页面,进行授权登录,然后确认自己的身份(本科生或研究生),并开启通知,即可每天在约定时间收到微信服务号的消息提醒。当然,用户可以自定义每日通知的时间,也可以随时开启或关闭每日通知。

  • VC++消息钩子编程「建议收藏」

    VC++消息钩子编程「建议收藏」一、消息钩子的概念1、基本概念Windows应用程序是基于消息驱动的,任何线程只要注册窗口类都会有一个消息队列用于接收用户输入的消息和系统消息。为了拦截消息,Windows提出了钩子的概念。钩子(H

  • 51单片机:LED流水灯(仿真+代码)

    51单片机:LED流水灯(仿真+代码)这次用单片机做个简单的流水灯。先给大家看一下仿真软件的电路(软件为Proteus)上图就是用仿真软件制作的线路原理图AT89C51RC2:单片机BUTTON:按键CAP:电容CRYSTAL:晶振LED-GERRN:LED灯(绿色)RES:电阻接下来是程序部分(软件为keil)#include”reg51.h” //此文件中定义了单片机的一些特殊功能寄存器#include…

  • linux中rar解压命令_tar解压zip文件

    linux中rar解压命令_tar解压zip文件例1:添加文件或目录到压缩档案中,使用a命令。例如把文件files1添加到abc.rar中,使用a或m命令,a命令把file1文件添加到abc.rar档案中保持原有的file1文件不变,m命令移动file1文件到file1.rar档案中(压缩完成后会删除原有的file1文件,注意:m命令只针对文件进行操作)$raraabc.rarfile1说明:如果此时abc.rar档案不存在,会自行创建a…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号