- java.lang.Object
-
- java.security.MessageDigestSpi
-
- java.security.MessageDigest
-
public abstract class MessageDigest extends MessageDigestSpi
此MessageDigest类为应用程序提供消息摘要算法的功能,例如SHA-1或SHA-256。 消息摘要是安全的单向散列函数,它采用任意大小的数据并输出固定长度的散列值。MessageDigest对象开始初始化。 使用
update方法通过它处理数据。 在任何时候都可以调用reset来重置摘要。 一旦更新了所有要更新的数据,就应该调用其中一个digest方法来完成哈希计算。对于给定数量的更新,可以调用一次
digest方法。 调用digest后,MessageDigest对象将重置为其初始化状态。实现可以自由地实现Cloneable接口。 客户端应用程序可以通过尝试克隆和捕获CloneNotSupportedException来测试可克隆性:
MessageDigest md = MessageDigest.getInstance("SHA-256"); 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类中定义的方法; 超类中的所有方法都适用于希望提供自己的消息摘要算法实现的加密服务提供者。需要Java平台的每个实现来支持以下标准
MessageDigest算法:-
MD5 -
SHA-1 -
SHA-256
- 从以下版本开始:
- 1.1
- 另请参见:
-
DigestInputStream,DigestOutputStream
-
-
-
构造方法摘要
构造方法 变量 构造器 描述 protectedMessageDigest(String algorithm)使用指定的算法名称创建消息摘要。
-
方法摘要
所有方法 静态方法 实例方法 具体的方法 变量和类型 方法 描述 Objectclone()如果实现是可复制的,则返回克隆。byte[]digest()通过执行填充等最终操作来完成哈希计算。byte[]digest(byte[] input)使用指定的字节数组对摘要执行最终更新,然后完成摘要计算。intdigest(byte[] buf, int offset, int len)通过执行填充等最终操作来完成哈希计算。StringgetAlgorithm()返回标识算法的字符串,与实现细节无关。intgetDigestLength()以字节为单位返回摘要的长度,如果提供程序不支持此操作且实现不可复制,则返回0。static MessageDigestgetInstance(String algorithm)返回实现指定摘要算法的MessageDigest对象。static MessageDigestgetInstance(String algorithm, String provider)返回实现指定摘要算法的MessageDigest对象。static MessageDigestgetInstance(String algorithm, Provider provider)返回实现指定摘要算法的MessageDigest对象。ProvidergetProvider()返回此消息摘要对象的提供者。static booleanisEqual(byte[] digesta, byte[] digestb)比较两个摘要的相等性。voidreset()重置摘要以供进一步使用。StringtoString()返回此消息摘要对象的字符串表示形式。voidupdate(byte input)使用指定的字节更新摘要。voidupdate(byte[] input)使用指定的字节数组更新摘要。voidupdate(byte[] input, int offset, int len)使用指定的字节数组更新摘要,从指定的偏移量开始。voidupdate(ByteBuffer input)使用指定的ByteBuffer更新摘要。-
声明方法的类 java.security.MessageDigestSpi
engineDigest, engineDigest, engineGetDigestLength, engineReset, engineUpdate, engineUpdate, engineUpdate
-
-
-
-
构造方法详细信息
-
MessageDigest
protected MessageDigest(String algorithm)
使用指定的算法名称创建消息摘要。- 参数
-
algorithm- 摘要算法的标准名称。 有关标准算法名称的信息,请参阅Java Security Standard Algorithm Names Specification中的MessageDigest部分。
-
-
方法详细信息
-
getInstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
返回实现指定摘要算法的MessageDigest对象。此方法遍历已注册的安全提供程序列表,从最首选的提供程序开始。 将返回一个新的MessageDigest对象,该对象封装来自第一个支持指定算法的Provider的MessageDigestSpi实现。
请注意,可以通过
Security.getProviders()方法检索已注册提供程序的列表。- Implementation Note:
-
JDK Reference Implementation还使用
jdk.security.provider.preferredSecurity属性来确定指定算法的首选提供程序顺序。 这可能与Security.getProviders()返回的提供程序顺序不同。 - 参数
-
algorithm- 请求的算法的名称。 有关标准算法名称的信息,请参阅Java Security Standard Algorithm Names Specification中的MessageDigest部分。 - 结果
-
实现指定算法的
MessageDigest对象 - 异常
-
NoSuchAlgorithmException- 如果没有Provider支持指定算法的MessageDigestSpi实现 -
NullPointerException- 如果algorithm是null - 另请参见:
-
Provider
-
getInstance
public static MessageDigest getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
返回实现指定摘要算法的MessageDigest对象。将返回一个新的MessageDigest对象,该对象将封装来自指定提供程序的MessageDigestSpi实现。 必须在安全提供程序列表中注册指定的提供程序。
请注意,可以通过
Security.getProviders()方法检索已注册提供商的列表。- 参数
-
algorithm- 请求的算法的名称。 有关标准算法名称的信息,请参阅Java Security Standard Algorithm Names Specification中的MessageDigest部分。 -
provider- 提供者的名称。 - 结果
-
实现指定算法的
MessageDigest对象 - 异常
-
IllegalArgumentException- 如果提供程序名称为null或为空 -
NoSuchAlgorithmException- 如果指定提供程序的指定算法的MessageDigestSpi实现不可用 -
NoSuchProviderException- 如果指定的提供程序未在安全提供程序列表中注册 -
NullPointerException- 如果algorithm是null - 另请参见:
-
Provider
-
getInstance
public static MessageDigest getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
返回实现指定摘要算法的MessageDigest对象。将返回一个新的MessageDigest对象,该对象将封装来自指定Provider对象的MessageDigestSpi实现。 请注意,指定的Provider对象不必在提供程序列表中注册。
- 参数
-
algorithm- 请求的算法的名称。 有关标准算法名称的信息,请参阅Java Security Standard Algorithm Names Specification中的MessageDigest部分。 -
provider- 提供者。 - 结果
-
实现指定算法的
MessageDigest对象 - 异常
-
IllegalArgumentException- 如果指定的提供者是null -
NoSuchAlgorithmException- 如果指定的算法的MessageDigestSpi实现不可用于指定的Provider对象 -
NullPointerException- 如果algorithm是null - 从以下版本开始:
- 1.4
- 另请参见:
-
Provider
-
getProvider
public final Provider getProvider()
返回此消息摘要对象的提供者。- 结果
- 此消息摘要对象的提供者
-
update
public void update(byte input)
使用指定的字节更新摘要。- 参数
-
input- 用于更新摘要的字节。
-
update
public void update(byte[] input, int offset, int len)使用指定的字节数组更新摘要,从指定的偏移量开始。- 参数
-
input- 字节数组。 -
offset- 从字节数组开始的偏移量。 -
len- 要使用的字节数,从offset开始。
-
update
public void update(byte[] input)
使用指定的字节数组更新摘要。- 参数
-
input- 字节数组。
-
update
public final void update(ByteBuffer input)
使用指定的ByteBuffer更新摘要。 摘要使用更新input.remaining()起始字节input.position()。 返回时,缓冲区的位置将等于其限制; 它的限制不会改变。- 参数
-
input- ByteBuffer - 从以下版本开始:
- 1.5
-
digest
public byte[] digest()
通过执行填充等最终操作来完成哈希计算。 进行此调用后,将重置摘要。- 结果
- 生成的哈希值的字节数组。
-
digest
public int digest(byte[] buf, int offset, int len) throws DigestException通过执行填充等最终操作来完成哈希计算。 进行此调用后,将重置摘要。- 参数
-
buf- 计算摘要的输出缓冲区 -
offset- 偏移到输出缓冲区以开始存储摘要 -
len- 为摘要分配的buf中的字节数 - 结果
-
放入
buf的字节数 - 异常
-
DigestException- 如果发生错误。
-
digest
public byte[] digest(byte[] input)
使用指定的字节数组对摘要执行最终更新,然后完成摘要计算。 也就是说,此方法首先调用update(input),将输入数组传递给update方法,然后调用digest()。- 参数
-
input- 摘要完成前要更新的输入。 - 结果
- 生成的哈希值的字节数组。
-
isEqual
public static boolean isEqual(byte[] digesta, byte[] digestb)比较两个摘要的相等性。 如果两个摘要具有相同的长度并且相应位置的所有字节相等,则它们是相等的。- Implementation Note:
- 如果摘要长度相同,则检查所有字节以确定相等性。
- 参数
-
digesta- 要比较的摘要之一。 -
digestb- 要比较的其他摘要。 - 结果
- 如果摘要相等则为true,否则为false。
-
reset
public void reset()
重置摘要以供进一步使用。
-
getAlgorithm
public final String getAlgorithm()
返回标识算法的字符串,与实现细节无关。 该名称应为标准Java安全性名称(例如“SHA-256”)。 有关标准算法名称的信息,请参阅Java Security Standard Algorithm Names Specification中的MessageDigest部分。- 结果
- 算法的名称
-
getDigestLength
public final int getDigestLength()
以字节为单位返回摘要的长度,如果提供程序不支持此操作且实现不可复制,则返回0。- 结果
- 摘要长度(以字节为单位),如果提供程序不支持此操作且实现不可复制,则为0。
- 从以下版本开始:
- 1.2
-
clone
public Object clone() throws CloneNotSupportedException
如果实现是可复制的,则返回克隆。- 重写:
-
clonein classMessageDigestSpi - 结果
- 如果实现是可复制的,则为克隆。
- 异常
-
CloneNotSupportedException- 如果在不支持Cloneable的实现上调用此方法。 - 另请参见:
-
Cloneable
-
-