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)


相关推荐

  • 3种JavaScript 对象转数组的方法

    3种JavaScript 对象转数组的方法来源|https://www.fly63.com我们在项目开发的时候,有时需要将js对象转换为数组,下面小编给大家具体演示一下怎么转换,主要是介绍一些常用、简洁的转换方法。比如Java…

  • EXCEL宏编程纪念

    EXCEL宏编程纪念       不久前被叫去编写宏,这EXCEL宏真是麻烦! 可能以后不会再遇到有编写宏的经历了,故而贴出来以做个纪念: Subdirect_Price()query_dir_volume宏由颜清国编写,时间:2007-4-9定义变量DimcRowsAsInteger总行数DimcColumnsAsInteger总列数DimHEADERCOLORINDE

  • ups不间断电源介绍_ups不间断电源设备有哪几部分组成

    ups不间断电源介绍_ups不间断电源设备有哪几部分组成本文简单介绍UPS(不间断电源)的相关知识。1概述UPS(UninterruptedPowerSupply),即不间断电源,是将蓄电池与主机相连接,通过主机逆变器等模块电路,将蓄电池中直流(DC,DirectCurrent)电转换成市电交流(AC,AlternatingCurrent)电的系统设备。UPS主要用于给单台计算机、计算机网络系统或其它电力电子设备如电磁阀、压力变送器…

    2022年10月27日
  • 论SLAM技术发展趋势[通俗易懂]

    论SLAM技术发展趋势[通俗易懂]2018年7月底,深蓝学院发起并承办了“第一届全国SLAM技术论坛”。浙江大学章国锋老师、香港科技大学沈劭劼老师、上海交通大学邹丹平老师、中科院自动化所申抒含老师在“圆桌论坛:SLAM技术发展趋势”上分享了SLAM技术的趋势,现将内容整理公布,希望更多SLAMer受益。章国锋:由于我的研究方向更偏向于视觉SLAM,所以我会从我的角度来讲一讲SLAM技术的发展趋势。首先,我认为无论是视觉…

  • poetry和poet_poetry和the poetry区别

    poetry和poet_poetry和the poetry区别Poetry的基本使用准备工作如果你是在一个已有的项目里使用Poetry,你只需要执行poetryinit命令来创建一个pyproject.toml文件:poetryinit可看到

  • Java中的join方法原理详解「建议收藏」

    Java中的join方法原理详解「建议收藏」1.synchronized中的对象锁是线程的实例我们可以使用同步语句块的方式对需要同步的代码进行包裹。Objectobj=newObject();synchronized(obj){   obj.wait();    //线程在这里等待}此时线程会在obj.wait()处等待,如果想继续执行,此时需要别的线程通过notify、notifyAll唤醒或者中断。但是如果o…

发表回复

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

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