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
类中定义的方法;超类中的所有方法是供希望提供自己的信息摘要算法实现的加密服务提供者使用的。
注意3:MessageDigest并不是单实例的。如下代码所示:
<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>
构造方法摘要 | |
---|---|
protected | MessageDigest(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>
注意:算法名不区分大小写。例如,以下所有调用都是相等的:
<p><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><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><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="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(更新)方法来完成:
<p><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></p><p><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><p><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></p>
2.3、计算摘要
通过调用 update 方法向MessageDigest对象提传送要计算的数据后,你就可以调用以下某个 digest(摘要)方法来计算摘要(即生成散列码):
<p><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></p><p><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></p><p><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></p>
前两个方法返回计算出的摘要。后一个方法把计算出的摘要储存在所提供的 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数组)转换为字符串。
<p><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></p><p><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></p><p><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></p><p><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></p><p><span class="pln"> </span><span class="pun" style="color:#66660;">}</span></p><p><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></p><p><span class="pln"> </span><span class="pun" style="color:#66660;">}</span></p>
3.2、示例一
★完整程序如下:
<p><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></p><p><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></p><p><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></p><p><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></p><p><span class="pln"> </span><span class="typ" style="color:#66066;">MessageDigest</span><span class="pln"> m</span><span class="pun" style="color:#66660;">;</span></p><p><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><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></p><p><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></p><p><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></p><p><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></p><p><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></p><p><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></p><p><span class="pln"> </span><span class="com" style="color:#8800;">// TODO Auto-generated catch block</span></p><p><span class="com" style="color:#8800;"> </span><span class="com" style="color:#8800;">e.printStackTrace();</span></p><p><span class="com" style="color:#8800;"> </span><span class="com" style="color:#8800;">} catch (UnsupportedEncodingException e) {</span></p><p><span class="com" style="color:#8800;"> </span><span class="com" style="color:#8800;">// TODO Auto-generated catch block</span></p><p><span class="com" style="color:#8800;"> </span><span class="com" style="color:#8800;">e.printStackTrace();</span></p><p><span class="com" style="color:#8800;"> </span><span class="com" style="color:#8800;">}</span></p><p> </p><p><span class="pln"> </span><span class="pun" style="color:#66660;">}</span></p><p> </p><p><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></p><p><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></p><p><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></p><p><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></p><p><span class="pln"> </span><span class="pun" style="color:#66660;">}</span></p><p><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></p><p><span class="pln"> </span><span class="pun" style="color:#66660;">}</span></p><p><span class="pun" style="color:#66660;">}</span></p>
★运行结果
<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进行简单的加密。
<p><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></p><p><span class="typ" style="color:#66066;">UnsupportedEncodingException</span><span class="pun" style="color:#66660;">{</span></p><p><span class="pln"> </span><span class="com" style="color:#8800;">/*确定计算方法*/</span></p><p><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></p><p><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></p><p><span class="pln"> </span><span class="com" style="color:#8800;">/*加密后的散列码字符串*/</span></p><p><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></p><p><span class="pln"> </span><span class="kwd" style="color:#0088;">return</span><span class="pln"> strMd5</span><span class="pun" style="color:#66660;">;</span></p><p><span class="pln"> </span><span class="pun" style="color:#66660;">}</span></p>
调用函数
<p><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></p><p><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></p>
输出