Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

javax.crypto
Class Mac  view Mac download Mac.java

java.lang.Object
  extended byjavax.crypto.Mac
All Implemented Interfaces:
java.lang.Cloneable

public class Mac
extends java.lang.Object
implements java.lang.Cloneable

This class implements a "message authentication code" (MAC), a method to ensure the integrity of data transmitted between two parties who share a common secret key.

The best way to describe a MAC is as a keyed one-way hash function, which looks like:

D = MAC(K, M)

where K is the key, M is the message, and D is the resulting digest. One party will usually send the concatenation M || D to the other party, who will then verify D by computing D' in a similar fashion. If D == D', then the message is assumed to be authentic.


Field Summary
private  java.lang.String algorithm
          The name of the algorithm.
private  MacSpi macSpi
          The underlying MAC implementation.
private  java.security.Provider provider
          The provider we got our implementation from.
private static java.lang.String SERVICE
           
private  boolean virgin
          Whether or not we've been initialized.
 
Constructor Summary
protected Mac(MacSpi macSpi, java.security.Provider provider, java.lang.String algorithm)
          Creates a new Mac instance.
 
Method Summary
 java.lang.Object clone()
          Clone this instance, if the underlying implementation supports it.
 byte[] doFinal()
          Finishes the computation of a MAC and returns the digest.
 byte[] doFinal(byte[] input)
          Finishes the computation of a MAC with a final byte array (or computes a MAC over those bytes only) and returns the digest.
 void doFinal(byte[] output, int outOffset)
          Finishes the computation of a MAC and places the result into the given array.
 java.lang.String getAlgorithm()
          Returns the name of this MAC algorithm.
static Mac getInstance(java.lang.String algorithm)
          Get an instance of the named algorithm from the first provider with an appropriate implementation.
static Mac getInstance(java.lang.String algorithm, java.security.Provider provider)
          Get an instance of the named algorithm from a provider.
static Mac getInstance(java.lang.String algorithm, java.lang.String provider)
          Get an instance of the named algorithm from the named provider.
 int getMacLength()
          Get the size of the MAC.
 java.security.Provider getProvider()
          Get the provider of the underlying implementation.
 void init(java.security.Key key)
          Initialize this MAC with a key and no parameters.
 void init(java.security.Key key, java.security.spec.AlgorithmParameterSpec params)
          Initialize this MAC with a key and parameters.
 void reset()
          Reset this instance.
 void update(byte input)
          Update the computation with a single byte.
 void update(byte[] input)
          Update the computation with a byte array.
 void update(byte[] input, int offset, int length)
          Update the computation with a portion of a byte array.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SERVICE

private static final java.lang.String SERVICE
See Also:
Constant Field Values

macSpi

private MacSpi macSpi
The underlying MAC implementation.


provider

private java.security.Provider provider
The provider we got our implementation from.


algorithm

private java.lang.String algorithm
The name of the algorithm.


virgin

private boolean virgin
Whether or not we've been initialized.

Constructor Detail

Mac

protected Mac(MacSpi macSpi,
              java.security.Provider provider,
              java.lang.String algorithm)
Creates a new Mac instance.

Method Detail

getInstance

public static final Mac getInstance(java.lang.String algorithm)
                             throws java.security.NoSuchAlgorithmException
Get an instance of the named algorithm from the first provider with an appropriate implementation.


getInstance

public static final Mac getInstance(java.lang.String algorithm,
                                    java.lang.String provider)
                             throws java.security.NoSuchAlgorithmException,
                                    java.security.NoSuchProviderException
Get an instance of the named algorithm from the named provider.


getInstance

public static final Mac getInstance(java.lang.String algorithm,
                                    java.security.Provider provider)
                             throws java.security.NoSuchAlgorithmException
Get an instance of the named algorithm from a provider.


doFinal

public final byte[] doFinal()
                     throws java.lang.IllegalStateException
Finishes the computation of a MAC and returns the digest.

After this method succeeds, it may be used again as just after a call to init, and can compute another MAC using the same key and parameters.


doFinal

public final byte[] doFinal(byte[] input)
                     throws java.lang.IllegalStateException
Finishes the computation of a MAC with a final byte array (or computes a MAC over those bytes only) and returns the digest.

After this method succeeds, it may be used again as just after a call to init, and can compute another MAC using the same key and parameters.


doFinal

public final void doFinal(byte[] output,
                          int outOffset)
                   throws java.lang.IllegalStateException,
                          ShortBufferException
Finishes the computation of a MAC and places the result into the given array.

After this method succeeds, it may be used again as just after a call to init, and can compute another MAC using the same key and parameters.


getAlgorithm

public final java.lang.String getAlgorithm()
Returns the name of this MAC algorithm.


getMacLength

public final int getMacLength()
Get the size of the MAC. This is the size of the array returned by doFinal() 55 and doFinal(byte[]) 55 , and the minimum number of bytes that must be available in the byte array passed to doFinal(byte[],int) 55 .


getProvider

public final java.security.Provider getProvider()
Get the provider of the underlying implementation.


init

public final void init(java.security.Key key)
                throws java.security.InvalidKeyException
Initialize this MAC with a key and no parameters.


init

public final void init(java.security.Key key,
                       java.security.spec.AlgorithmParameterSpec params)
                throws java.security.InvalidAlgorithmParameterException,
                       java.security.InvalidKeyException
Initialize this MAC with a key and parameters.


reset

public final void reset()
Reset this instance. A call to this method returns this instance back to the state it was in just after it was initialized.


update

public final void update(byte input)
                  throws java.lang.IllegalStateException
Update the computation with a single byte.


update

public final void update(byte[] input)
                  throws java.lang.IllegalStateException
Update the computation with a byte array.


update

public final void update(byte[] input,
                         int offset,
                         int length)
                  throws java.lang.IllegalStateException
Update the computation with a portion of a byte array.


clone

public final java.lang.Object clone()
                             throws java.lang.CloneNotSupportedException
Clone this instance, if the underlying implementation supports it.