public abstract class MessageDigest 
  extends MessageDigestSpi 
| java.lang.Object | ||
| java.security.MessageDigestSpi | ||
| java.security.MessageDigest | ||
这个MessageDigest类为应用程序提供了消息摘要算法的功能,例如SHA-1或SHA-256。 消息摘要是安全的单向散列函数,可以采用任意大小的数据并输出固定长度的散列值。
MessageDigest对象从初始化开始。 数据通过使用update方法进行处理。 任何时候都可以调用reset来重置摘要。 一旦所有要更新的数据都被更新,应调用其中一个digest方法来完成哈希计算。
对于给定数量的更新,可以调用digest方法一次。 在digest之后,MessageDigest对象将重置为初始化状态。
实现可以自由实现Cloneable接口。 客户端应用程序可以通过尝试克隆并捕获CloneNotSupportedException来测试可克隆性:
 MessageDigest md = MessageDigest.getInstance("SHA");
 try {
     md.update(toChapter1);
     MessageDigest tc1 = md.clone();
     byte[] toChapter1Digest = tc1.digest();
     md.update(toChapter2);
     ...etc.
 } catch (CloneNotSupportedException cnse) {
     throw new DigestException("couldn't make digest of partial content");
 }
  
    请注意,如果给定的实现不可复制,如果提前知道摘要的数量,则仍然可以通过实例化几个实例来计算中间摘要。
请注意,由于历史原因,这个类是抽象的,并且从MessageDigestSpi延伸。 应用程序开发人员只应注意本MessageDigest类中定义的方法; 超类中的所有方法都用于希望提供自己的消息摘要算法实现的加密服务提供者。
Android提供了以下 MessageDigest算法:
| Name | Supported (API Levels) | 
|---|---|
| MD5 | 1+ | 
| SHA-1 | 1+ | 
| SHA-224 | 1–8,22+ | 
| SHA-256 | 1+ | 
| SHA-384 | 1+ | 
| SHA-512 | 1+ | 
Protected constructors |  
      |
|---|---|
  MessageDigest(String algorithm)  使用指定的算法名称创建消息摘要。  |  
      |
公共方法(Public methods) |  
      |
|---|---|
 Object  |  
         clone()  如果实现可复制,则返回一个克隆。  |  
      
 int  |  
         digest(byte[] buf, int offset, int len)  通过执行最终操作(如填充)完成散列计算。  |  
      
 byte[]  |  
         digest()  通过执行最终操作(如填充)完成散列计算。  |  
      
 byte[]  |  
         digest(byte[] input)  使用指定的字节数组对摘要执行最终更新,然后完成摘要计算。  |  
      
 final String  |  
         getAlgorithm()  返回标识算法的字符串,与实现细节无关。  |  
      
 final int  |  
         getDigestLength()  以字节为单位返回摘要的长度,如果提供程序不支持此操作且实现不可复制,则返回0。  |  
      
 static MessageDigest  |  
         getInstance(String algorithm)  返回实现指定摘要算法的MessageDigest对象。  |  
      
 static MessageDigest  |  
         getInstance(String algorithm, String provider)  返回实现指定摘要算法的MessageDigest对象。  |  
      
 static MessageDigest  |  
         getInstance(String algorithm, Provider provider)  返回实现指定摘要算法的MessageDigest对象。  |  
      
 final Provider  |  
         getProvider()  返回此消息摘要对象的提供者。  |  
      
 static boolean  |  
         isEqual(byte[] digesta, byte[] digestb)  比较两个摘要的平等。  |  
      
 void  |  
         reset()  重置摘要以供进一步使用。  |  
      
 String  |  
         toString()  返回此消息摘要对象的字符串表示形式。  |  
      
 void  |  
         update(byte[] input)  使用指定的字节数组更新摘要。  |  
      
 void  |  
         update(byte[] input, int offset, int len)  使用指定的字节数组从指定的偏移量开始更新摘要。  |  
      
 final void  |  
         update(ByteBuffer input)  使用指定的ByteBuffer更新摘要。  |  
      
 void  |  
         update(byte input)  使用指定的字节更新摘要。  |  
      
继承方法(Inherited methods) |  
      |
|---|---|
  java.security.MessageDigestSpi  
         |  
      |
  java.lang.Object  
         |  
      |
MessageDigest (String algorithm)
使用指定的算法名称创建消息摘要。
| 参数(Parameters) | |
|---|---|
algorithm |  
         String: the standard name of the digest algorithm. See the MessageDigest section in the  Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names.  |  
       
Object clone ()
如果实现可复制,则返回一个克隆。
| 返回(Returns) | |
|---|---|
Object |  
        a clone if the implementation is cloneable. | 
| 抛出异常(Throws) | |
|---|---|
CloneNotSupportedException |  
        if this is called on an implementation that does not support Cloneable.  |  
       
int digest (byte[] buf, 
                int offset, 
                int len) 
     通过执行最终操作(如填充)完成散列计算。 此呼叫完成后,摘要将被重置。
| 参数(Parameters) | |
|---|---|
buf |  
         byte: output buffer for the computed digest |  
       
offset |  
         int: offset into the output buffer to begin storing the digest |  
       
len |  
         int: number of bytes within buf allotted for the digest |  
       
| 返回(Returns) | |
|---|---|
int |  
        the number of bytes placed into buf |  
       
| 抛出异常(Throws) | |
|---|---|
DigestException |  
        if an error occurs. | 
byte[] digest ()
通过执行最终操作(如填充)完成散列计算。 此呼叫完成后,摘要将被重置。
| 返回(Returns) | |
|---|---|
byte[] |  
        the array of bytes for the resulting hash value. | 
byte[] digest (byte[] input)
使用指定的字节数组对摘要执行最终更新,然后完成摘要计算。 也就是说,此方法首先调用update(input) ,将输入数组传递给update方法,然后调用digest() 。
| 参数(Parameters) | |
|---|---|
input |  
         byte: the input to be updated before the digest is completed. |  
       
| 返回(Returns) | |
|---|---|
byte[] |  
        the array of bytes for the resulting hash value. | 
String getAlgorithm ()
返回标识算法的字符串,与实现细节无关。 该名称应该是标准的Java安全名称(例如“SHA”,“MD5”等)。 有关标准算法名称的信息,请参阅Java Cryptography Architecture Standard Algorithm Name Documentation中的MessageDigest部分。
| 返回(Returns) | |
|---|---|
String |  
        the name of the algorithm | 
int getDigestLength ()
以字节为单位返回摘要的长度,如果提供程序不支持此操作且实现不可复制,则返回0。
| 返回(Returns) | |
|---|---|
int |  
        the digest length in bytes, or 0 if this operation is not supported by the provider and the implementation is not cloneable. | 
MessageDigest getInstance (String algorithm)
返回实现指定摘要算法的MessageDigest对象。
该方法遍历注册安全提供程序的列表,从最优先的提供程序开始。 将返回一个新的MessageDigest对象,它封装来自支持指定算法的第一个Provider的MessageDigestSpi实现。
请注意,注册供应商列表可以通过 Security.getProviders()方法检索。
| 参数(Parameters) | |
|---|---|
algorithm |  
         String: the name of the algorithm requested. See the MessageDigest section in the  Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names. |  
       
| 返回(Returns) | |
|---|---|
MessageDigest |  
        a Message Digest object that implements the specified algorithm. | 
| 抛出异常(Throws) | |
|---|---|
NoSuchAlgorithmException |  
        if no Provider supports a MessageDigestSpi implementation for the specified algorithm. | 
也可以看看:
MessageDigest getInstance (String algorithm, String provider)
返回实现指定摘要算法的MessageDigest对象。
返回一个新的MessageDigest对象,封装来自指定提供者的MessageDigestSpi实现。 指定的提供者必须在安全提供者列表中注册。
请注意,注册供应商列表可以通过 Security.getProviders()方法检索。
| 参数(Parameters) | |
|---|---|
algorithm |  
         String: the name of the algorithm requested. See the MessageDigest section in the  Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names. |  
       
provider |  
         String: the name of the provider. |  
       
| 返回(Returns) | |
|---|---|
MessageDigest |  
        a MessageDigest object that implements the specified algorithm. | 
| 抛出异常(Throws) | |
|---|---|
NoSuchAlgorithmException |  
        if a MessageDigestSpi implementation for the specified algorithm is not available from the specified provider. | 
NoSuchProviderException |  
        if the specified provider is not registered in the security provider list. | 
IllegalArgumentException |  
        if the provider name is null or empty. | 
也可以看看:
MessageDigest getInstance (String algorithm, Provider provider)
返回实现指定摘要算法的MessageDigest对象。
返回封装指定Provider对象的MessageDigestSpi实现的新MessageDigest对象。 请注意,指定的Provider对象不必在提供程序列表中注册。
| 参数(Parameters) | |
|---|---|
algorithm |  
         String: the name of the algorithm requested. See the MessageDigest section in the  Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names. |  
       
provider |  
         Provider: the provider. |  
       
| 返回(Returns) | |
|---|---|
MessageDigest |  
        a MessageDigest object that implements the specified algorithm. | 
| 抛出异常(Throws) | |
|---|---|
NoSuchAlgorithmException |  
        if a MessageDigestSpi implementation for the specified algorithm is not available from the specified Provider object. | 
IllegalArgumentException |  
        if the specified provider is null. | 
也可以看看:
Provider getProvider ()
返回此消息摘要对象的提供者。
| 返回(Returns) | |
|---|---|
Provider |  
        the provider of this message digest object | 
boolean isEqual (byte[] digesta, 
                byte[] digestb) 
     比较两个摘要的平等。 一个简单的字节进行比较。
| 参数(Parameters) | |
|---|---|
digesta |  
         byte: one of the digests to compare. |  
       
digestb |  
         byte: the other digest to compare. |  
       
| 返回(Returns) | |
|---|---|
boolean |  
        true if the digests are equal, false otherwise. | 
String toString ()
返回此消息摘要对象的字符串表示形式。
| 返回(Returns) | |
|---|---|
String |  
        a string representation of the object. | 
void update (byte[] input)
使用指定的字节数组更新摘要。
| 参数(Parameters) | |
|---|---|
input |  
         byte: the array of bytes.  |  
       
void update (byte[] input, 
                int offset, 
                int len) 
     使用指定的字节数组从指定的偏移量开始更新摘要。
| 参数(Parameters) | |
|---|---|
input |  
         byte: the array of bytes. |  
       
offset |  
         int: the offset to start from in the array of bytes. |  
       
len |  
         int: the number of bytes to use, starting at offset.  |  
       
void update (ByteBuffer input)
使用指定的ByteBuffer更新摘要。 摘要采用更新的input.remaining()起始字节input.position() 。 返回时,缓冲区的位置将等于它的极限; 其限制不会改变。
| 参数(Parameters) | |
|---|---|
input |  
         ByteBuffer: the ByteBuffer |  
       
void update (byte input)
使用指定的字节更新摘要。
| 参数(Parameters) | |
|---|---|
input |  
         byte: the byte with which to update the digest.  |