大家好,又见面了,我是你们的朋友全栈君。
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 更新摘要。 |
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>
MessageDigest对象。因此,它不需要进一步的初始化。
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>
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>
<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>
三、例子演示
MessageDigest类提供了计算消息摘要
(
即生成 散列码
)的方法,首先生成对象,执行其
update( )方法可
digest( )方法即可得到消息摘要。具体步骤如下:
<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>
getInstance( )生成
MessageDigest对象。
MD5“,”
SHA“等。
<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>
<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>
<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="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>
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账号...