- 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"); 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
-
-
-
构造方法摘要
构造方法 Modifier Constructor 描述 protectedMessageDigest(String algorithm)使用指定的算法名称创建消息摘要。
-
方法摘要
所有方法 静态方法 接口方法 具体的方法 Modifier and Type 方法 描述 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更新摘要。-
Methods inherited from class 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对象。此方法遍历已注册的安全提供程序列表,从最优选的提供程序开始。 返回从支持指定算法的第一个Provider中封装MessageDigestSpi实现的新MessageDigest对象。
请注意,注册提供商的列表可以通过
Security.getProviders()方法检索。- Implementation Note:
-
JDK参考实现另外使用
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对象。返回从指定提供程序封装MessageDigestSpi实现的新MessageDigest对象。 指定的提供者必须在安全提供程序列表中注册。
注意,可以通过
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对象。返回从指定的Provider对象封装MessageDigestSpi实现的新MessageDigest对象。 请注意,指定的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”,“MD5”等)。 有关标准算法名称的信息,请参阅Java Security Standard Algorithm Names Specification中的MessageDigest部分。- 结果
- 算法的名称
-
getDigestLength
public final int getDigestLength()
以字节为单位返回摘要的长度,如果提供程序不支持此操作,并且实现不可克隆,则返回0。- 结果
- 摘要长度(以字节为单位),如果此操作不受提供程序支持,则执行不可克隆。
- 从以下版本开始:
- 1.2
-
clone
public Object clone() throws CloneNotSupportedException
如果实现是可克隆的,则返回克隆。- 重写:
-
clone在MessageDigestSpi - 结果
- 一个克隆,如果实现是可克隆的。
- 异常
-
CloneNotSupportedException- 如果在不支持Cloneable的实现上调用此方法。 - 另请参见:
-
Cloneable
-
-