public class Mac
extends Object implements Cloneable
| java.lang.Object | |
| javax.crypto.Mac | |
这个类提供了“消息认证码”(MAC)算法的功能。
基于密钥,MAC提供了一种方法来检查通过或存储在不可靠介质中的信息的完整性。 通常,在共享密钥的双方之间使用消息认证码以验证在这些方之间传输的信息。
基于密码散列函数的MAC机制被称为HMAC。 HMAC可以与任何加密散列函数一起使用,例如,MD5或SHA-1,以及秘密共享密钥。 HMAC在RFC 2104中指定。
Android提供了以下 Mac算法
| Name | Supported (API Levels) |
|---|---|
| DESedeMAC | 1–8 |
| DESedeMAC/CFB8 | 1–8 |
| DESedeMAC64 | 1–8 |
| DESMAC | 1–8 |
| DESMAC/CFB8 | 1–8 |
| DESwithISO9797 | 1–8 |
| HmacMD5 | 1+ |
| HmacSHA1 | 1+ |
| HmacSHA224 | 1–8, 22+ |
| HmacSHA256 | 1+ |
| HmacSHA384 | 1+ |
| HmacSHA512 | 1+ |
| ISO9797ALG3MAC | 1–8 |
| PBEwithHmacSHA | 1+ |
| PBEwithHmacSHA1 | 1+ |
Protected constructors |
|
|---|---|
Mac(MacSpi macSpi, Provider provider, String algorithm) 创建一个MAC对象。 |
|
公共方法(Public methods) |
|
|---|---|
final Object |
clone() 如果提供者实现是可复制的,则返回一个克隆。 |
final byte[] |
doFinal(byte[] input) 处理给定的字节数组并完成MAC操作。 |
final void |
doFinal(byte[] output, int outOffset) 完成MAC操作。 |
final byte[] |
doFinal() 完成MAC操作。 |
final String |
getAlgorithm() 返回此 |
static final Mac |
getInstance(String algorithm) 返回实现指定MAC算法的 |
static final Mac |
getInstance(String algorithm, String provider) 返回实现指定MAC算法的 |
static final Mac |
getInstance(String algorithm, Provider provider) 返回实现指定MAC算法的 |
final int |
getMacLength() 以字节为单位返回MAC的长度。 |
final Provider |
getProvider() 返回此 |
final void |
init(Key key, AlgorithmParameterSpec params) 使用给定的密钥和算法参数初始化此对象 |
final void |
init(Key key) 用给定的键初始化此 |
final void |
reset() 重置此 |
final void |
update(byte[] input) 处理给定的字节数组。 |
final void |
update(byte[] input, int offset, int len) 处理第一 |
final void |
update(ByteBuffer input) 处理 |
final void |
update(byte input) 处理给定的字节。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
Mac (MacSpi macSpi, Provider provider, String algorithm)
创建一个MAC对象。
| 参数(Parameters) | |
|---|---|
macSpi |
MacSpi: the delegate |
provider |
Provider: the provider |
algorithm |
String: the algorithm |
Object clone ()
如果提供者实现是可复制的,则返回一个克隆。
| 返回(Returns) | |
|---|---|
Object |
a clone if the provider implementation is cloneable. |
| 抛出异常(Throws) | |
|---|---|
CloneNotSupportedException |
if this is called on a delegate that does not support Cloneable. |
byte[] doFinal (byte[] input)
处理给定的字节数组并完成MAC操作。
此方法的调用重置该Mac目的是它是当先前经由到呼叫初始化的状态init(Key)或init(Key, AlgorithmParameterSpec) 。 也就是说,如果需要,该对象将重置并可用于通过对update和doFinal新呼叫从相同密钥生成另一个MAC。 (为了使用不同的密钥重新使用这个Mac对象,必须通过调用init(Key)或init(Key, AlgorithmParameterSpec)来重新初始化它。
| 参数(Parameters) | |
|---|---|
input |
byte: data in bytes |
| 返回(Returns) | |
|---|---|
byte[] |
the MAC result. |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this Mac has not been initialized. |
void doFinal (byte[] output,
int outOffset)
完成MAC操作。
此方法的调用重置该Mac目的是它是当先前经由到呼叫初始化的状态init(Key)或init(Key, AlgorithmParameterSpec) 。 也就是说,如果需要,该对象将被重置并可用于通过对update和doFinal新调用从同一个密钥生成另一个MAC。 (为了重新使用此Mac用不同的密钥对象时,它必须通过将呼叫重新初始化init(Key)或init(Key, AlgorithmParameterSpec) 。
MAC结果存储在 output ,从 outOffset开始。
| 参数(Parameters) | |
|---|---|
output |
byte: the buffer where the MAC result is stored |
outOffset |
int: the offset in output where the MAC is stored |
| 抛出异常(Throws) | |
|---|---|
ShortBufferException |
if the given output buffer is too small to hold the result |
IllegalStateException |
if this Mac has not been initialized. |
byte[] doFinal ()
完成MAC操作。
此方法的调用重置该Mac目的是它是当先前经由到呼叫初始化的状态init(Key)或init(Key, AlgorithmParameterSpec) 。 也就是说,如果需要,该对象将被重置并可用于通过对update和doFinal新调用从同一个密钥生成另一个MAC。 (为了使用不同的密钥重新使用这个Mac对象,它必须通过调用init(Key)或init(Key, AlgorithmParameterSpec)重新初始化。
| 返回(Returns) | |
|---|---|
byte[] |
the MAC result. |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this Mac has not been initialized. |
String getAlgorithm ()
返回此 Mac对象的算法名称。
这与在创建此 Mac对象的 getInstance调用之一中指定的名称相同。
| 返回(Returns) | |
|---|---|
String |
the algorithm name of this Mac object. |
Mac getInstance (String algorithm)
返回实现指定MAC算法的 Mac对象。
该方法遍历注册安全提供程序的列表,从最优先的提供程序开始。 返回封装来自支持指定算法的第一个Provider的MacSpi实现的新Mac对象。
请注意,注册供应商列表可能通过 Security.getProviders()方法检索。
| 参数(Parameters) | |
|---|---|
algorithm |
String: the standard name of the requested MAC algorithm. See the Mac section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names. |
| 返回(Returns) | |
|---|---|
Mac |
the new Mac object. |
| 抛出异常(Throws) | |
|---|---|
NoSuchAlgorithmException |
if no Provider supports a MacSpi implementation for the specified algorithm. |
也可以看看:
Mac getInstance (String algorithm, String provider)
返回实现指定MAC算法的 Mac对象。
返回封装指定提供程序的MacSpi实现的新Mac对象。 指定的提供者必须在安全提供者列表中注册。
请注意,注册供应商列表可能通过 Security.getProviders()方法检索。
| 参数(Parameters) | |
|---|---|
algorithm |
String: the standard name of the requested MAC algorithm. See the Mac section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names. |
provider |
String: the name of the provider. |
| 返回(Returns) | |
|---|---|
Mac |
the new Mac object. |
| 抛出异常(Throws) | |
|---|---|
NoSuchAlgorithmException |
if a MacSpi 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 is null or empty. |
也可以看看:
Mac getInstance (String algorithm, Provider provider)
返回实现指定MAC算法的 Mac对象。
返回封装指定Provider对象的MacSpi实现的新Mac对象。 请注意,指定的Provider对象不必在提供程序列表中注册。
| 参数(Parameters) | |
|---|---|
algorithm |
String: the standard name of the requested MAC algorithm. See the Mac section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names. |
provider |
Provider: the provider. |
| 返回(Returns) | |
|---|---|
Mac |
the new Mac object. |
| 抛出异常(Throws) | |
|---|---|
NoSuchAlgorithmException |
if a MacSpi implementation for the specified algorithm is not available from the specified Provider object. |
IllegalArgumentException |
if the provider is null. |
也可以看看:
int getMacLength ()
以字节为单位返回MAC的长度。
| 返回(Returns) | |
|---|---|
int |
the MAC length in bytes. |
Provider getProvider ()
返回此 Mac对象的提供者。
| 返回(Returns) | |
|---|---|
Provider |
the provider of this Mac object. |
void init (Key key, AlgorithmParameterSpec params)
用给定的密钥和算法参数初始化此对象 Mac 。
| 参数(Parameters) | |
|---|---|
key |
Key: the key. |
params |
AlgorithmParameterSpec: the algorithm parameters. |
| 抛出异常(Throws) | |
|---|---|
InvalidKeyException |
if the given key is inappropriate for initializing this MAC. |
InvalidAlgorithmParameterException |
if the given algorithm parameters are inappropriate for this MAC. |
void init (Key key)
使用给定的键初始化此 Mac对象。
| 参数(Parameters) | |
|---|---|
key |
Key: the key. |
| 抛出异常(Throws) | |
|---|---|
InvalidKeyException |
if the given key is inappropriate for initializing this MAC. |
void reset ()
重置此 Mac对象。
此方法的调用重置该Mac目的是它是当先前经由到呼叫初始化的状态init(Key)或init(Key, AlgorithmParameterSpec) 。 也就是说,如果需要,该对象被重置并可用于通过对update和doFinal新调用从同一个密钥生成另一个MAC。 (为了重新使用此Mac用不同的密钥对象时,它必须通过将呼叫重新初始化init(Key)或init(Key, AlgorithmParameterSpec) 。
void update (byte[] input)
处理给定的字节数组。
| 参数(Parameters) | |
|---|---|
input |
byte: the array of bytes to be processed. |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this Mac has not been initialized. |
void update (byte[] input,
int offset,
int len)
处理第一 len字节 input ,起始于 offset以下。
| 参数(Parameters) | |
|---|---|
input |
byte: the input buffer. |
offset |
int: the offset in input where the input starts. |
len |
int: the number of bytes to process. |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this Mac has not been initialized. |
void update (ByteBuffer input)
处理input.remaining()在ByteBuffer的字节input ,起始于input.position() 。 返回时,缓冲区的位置将等于它的极限; 其限制不会改变。
| 参数(Parameters) | |
|---|---|
input |
ByteBuffer: the ByteBuffer |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this Mac has not been initialized. |
void update (byte input)
处理给定的字节。
| 参数(Parameters) | |
|---|---|
input |
byte: the input byte to be processed. |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this Mac has not been initialized. |