public class Mac extends Object implements Cloneable
MAC提供了一种基于秘密密钥来检查在不可靠介质中传输或存储在信息中的信息的完整性的方法。 通常,在共享密钥的两方之间使用消息认证码,以验证在这些方之间传输的信息。
基于加密散列函数的MAC机制被称为HMAC。 HMAC可以与任何加密散列函数一起使用,例如MD5或SHA-1与秘密共享密钥的组合。 HMAC在RFC 2104中规定。
 Java平台的每个实现都需要支持以下标准Mac算法: 
| Modifier | Constructor and Description | 
|---|---|
| protected  | Mac(MacSpi macSpi, Provider provider, String algorithm)
              创建MAC对象。 
             | 
| Modifier and Type | Method and Description | 
|---|---|
| Object | clone()
              如果提供程序实现是可克隆的,则返回克隆。 
             | 
| byte[] | doFinal()
              完成MAC操作。 
             | 
| byte[] | doFinal(byte[] input)
              处理给定的字节数组并完成MAC操作。 
             | 
| void | doFinal(byte[] output, int outOffset)
              完成MAC操作。 
             | 
| String | getAlgorithm()
              返回此 
              Mac对象的算法名称。 | 
| static Mac | getInstance(String algorithm)
              返回实现指定MAC算法的 
              Mac对象。 | 
| static Mac | getInstance(String algorithm, Provider provider)
              返回实现指定MAC算法的 
              Mac对象。 | 
| static Mac | getInstance(String algorithm, String provider)
              返回实现指定的MAC算法的 
              Mac对象。 | 
| int | getMacLength()
              以字节为单位返回MAC的长度。 
             | 
| Provider | getProvider()
              返回此 
              Mac对象的提供者。 | 
| void | init(Key key)
              使用给定的键初始化此 
              Mac对象。 | 
| void | init(Key key, AlgorithmParameterSpec params)
              使用给定的键和算法参数初始化此 
              Mac对象。 | 
| void | reset()
              重设此 
              Mac对象。 | 
| void | update(byte input)
              处理给定的字节。 
             | 
| void | update(byte[] input)
              处理给定的字节数组。 
             | 
| void | update(byte[] input, int offset, int len)
              处理第一 
              len字节input,起始于offset以下。 | 
| void | update(ByteBuffer input)
              处理 
              input.remaining()字节的ByteBufferinput,从input.position()开始。 | 
public final String getAlgorithm()
Mac对象的算法名称。 
            这是在创建此Mac对象的getInstance调用之一中指定的getInstance 。 
Mac对象的算法名称。 
           public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException
Mac对象。 
           此方法遍历已注册的安全提供程序列表,从最优选的提供程序开始。 返回从支持指定算法的第一个提供者封装MacSpi实现的新Mac对象。
 请注意,可以通过Security.getProviders()方法检索已注册提供商的列表。 
algorithm - 请求的MAC算法的标准名称。 
            有关标准算法名称的信息,请参阅Java Cryptography Architecture Standard Algorithm Name Documentation中的Mac部分。 
           Mac对象。 
           NoSuchAlgorithmException - 如果没有提供程序支持指定算法的MacSpi实现。 
           Provider 
           public static final Mac getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
Mac对象。 
           返回从指定的提供程序封装MacSpi实现的新Mac对象。 指定的提供者必须在安全提供程序列表中注册。
 请注意,注册提供商的列表可以通过Security.getProviders()方法检索 。 
algorithm - 请求的MAC算法的标准名称。 
            有关标准算法名称的信息,请参阅Java Cryptography Architecture Standard Algorithm Name Documentation中的Mac部分。 
           provider - 提供者的名称。 
           Mac对象。 
           NoSuchAlgorithmException - 如果指定算法的MacSpi实现不能从指定的提供者获得。 
           NoSuchProviderException - 如果指定的提供程序未在安全提供程序列表中注册。 
           IllegalArgumentException - 如果 
            provider为空或为空。 
           Provider 
           public static final Mac getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
Mac对象。 
           返回从指定的Provider对象封装MacSpi实现的新Mac对象。 请注意,指定的Provider对象不必在提供者列表中注册。
algorithm - 请求的MAC算法的标准名称。 
            有关标准算法名称的信息,请参阅Java Cryptography Architecture Standard Algorithm Name Documentation中的Mac部分。 
           provider - 提供商。 
           Mac对象。 
           NoSuchAlgorithmException - 如果指定的算法的MacSpi实现从指定的Provider对象不可用。 
           IllegalArgumentException - 如果 
            provider为空。 
           Provider 
           public final Provider getProvider()
Mac对象的提供程序。 
          Mac对象的提供者。 
           public final int getMacLength()
public final void init(Key key) throws InvalidKeyException
Mac对象。 
          key - 关键。 
           InvalidKeyException - 如果给定的键不适合初始化此MAC。 
           public final void init(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException
Mac对象。 
          key - 关键。 
           params - 算法参数。 
           InvalidKeyException - 如果给定的键不适合初始化此MAC。 
           InvalidAlgorithmParameterException - 如果给定的算法参数不适合此MAC。 
           public final void update(byte input)
                  throws IllegalStateException 
          input - 要处理的输入字节。 
           IllegalStateException - 如果此 
            Mac尚未初始化。 
           public final void update(byte[] input)
                  throws IllegalStateException 
          input - 要处理的字节数组。 
           IllegalStateException - 如果此 
            Mac尚未初始化。 
           public final void update(byte[] input,
                         int offset,
                         int len)
                  throws IllegalStateException 
          len字节 
           input ,起始于 
           offset以下。 
          input - 输入缓冲区。 
           offset - 
            input中的输入开始的偏移量。 
           len - 要处理的字节数。 
           IllegalStateException - 如果此 
            Mac尚未初始化。 
           public final void update(ByteBuffer input)
input.remaining()字节的ByteBuffer input ,从input.position()开始。 
           返回时,缓冲区的位置将等于其限制; 
           其限制将不会改变。 
          input - ByteBuffer 
           IllegalStateException - 如果 
            Mac尚未初始化。 
           public final byte[] doFinal()
                     throws IllegalStateException 
           调用此方法会将此Mac对象重置为先前通过调用init(Key)或init(Key, AlgorithmParameterSpec) 。 也就是说,如果需要,通过对update和doFinal新呼叫,对象被重置并可用于从相同的密钥生成另一个MAC。 (为了使用不同的键来重用此Mac对象,必须通过调用init(Key)或init(Key, AlgorithmParameterSpec) 。 
IllegalStateException - 如果此 
            Mac尚未初始化。 
           public final void doFinal(byte[] output,
                          int outOffset)
                   throws ShortBufferException,
                          IllegalStateException 
           调用此方法将此Mac对象重置为先前通过调用init(Key)或init(Key, AlgorithmParameterSpec) 。 也就是说,对象被重置并且可用于通过对update和doFinal新呼叫从相同的密钥生成另一个MAC。 (为了使用不同的键重用此Mac对象,必须通过调用init(Key)或init(Key, AlgorithmParameterSpec) 。 
 MAC结果存储在output ,从outOffset开始。 
output - 存储MAC结果的缓冲区 
           outOffset - 存储MAC的 
            output中的偏移量 
           ShortBufferException - 如果给定的输出缓冲区太小,不能容纳结果 
           IllegalStateException - 如果此 
            Mac尚未初始化。 
           public final byte[] doFinal(byte[] input)
                     throws IllegalStateException 
           调用此方法将此Mac对象重置为先前通过调用init(Key)或init(Key, AlgorithmParameterSpec) 。 也就是说,如果需要,通过对update和doFinal新呼叫,对象被重置并可用于从相同的密钥生成另一个MAC。 (为了使用不同的键来重用此Mac对象,必须通过调用init(Key)或init(Key, AlgorithmParameterSpec) 。 
input - 以字节为单位的数据 
           IllegalStateException - 如果此 
            Mac尚未初始化。 
           public final void reset()
Mac对象。 
            调用此方法将此Mac对象重置为先前通过调用init(Key)或init(Key, AlgorithmParameterSpec) 。 也就是说,对象被重置并且可用于通过对update和doFinal新呼叫从相同的密钥生成另一MAC。 (为了使用不同的键重用此Mac对象,必须通过调用init(Key)或init(Key, AlgorithmParameterSpec) 。 
public final Object clone() throws CloneNotSupportedException
clone在 
            Object 
           CloneNotSupportedException - 如果在不支持 
            Cloneable的代理上调用此方法。 
           Cloneable 
            Submit a bug or feature 
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
 Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.