MAC (Message authentication code, messaging authentication Code algorithm) is a key hash function algorithm that is compatible with the features of the MD and SHA algorithms, and the key is added on this basis. Therefore, the Mac is also called an HMAC. the MAC algorithm sets up two series of Message digest Algorithms, MD and SHA. MD series has HmacMD2, HmacMD4, HmacMD5 three kinds of algorithms. The SHA series hasHmacSHA1,HmacSHA224,HmacSHA256,HmacSHA384,HmacSHA512 These five kinds of algorithms. The length of the MAC algorithm summary value is the length of the specific digest algorithm. such as HmacMD5 even if the digest value of the corresponding MD5 algorithm is 128 bits long. Specific as follows:
implementation of MAC algorithm
1. Java Self-band class implementation
Note: Java Automatic classes only supportHmacMD5,HmacSHA1,HmacSHA256,HmacSHA384,HmacSHA512 These algorithms
The MAC algorithm is a message digest algorithm with a key, so its implementation is divided into two steps 1) to create the key 2) to obtain the Message digest implementation code as follows:
/*** 1. Create key */ //create a corresponding digest algorithm (such as md5\sha1\sha256, etc.) key generator object Keygenerator keygenerator keygenerator = keygenerator. getinstance ("HmacMD5"); //Generate key Secretkey Secretkey = Keygenerator.generatekey (); //The byte array to get the key //byte[] key = secretkey.getencoded (); /*** 2. Generate Mac summary based on key */ //Create a Mac instance object that corresponds to a digest algorithm (such as md5\sha1\sha256, etc.) Mac Mac = Mac. getinstance (Secretkey.getalgorithm ()); //Initialize the key of the Mac instance object Mac.init (secretkey); //Get mac summary information byte[]result = mac.dofinal ("China". GetBytes ());System. Out. println (tohexstring(Result));
|
From for notes (Wiz)
Mac Information Summary