Here you can find the source of hmac(byte key[], byte text[])
Parameter | Description |
---|---|
key | a parameter |
text | a parameter |
public static byte[] hmac(byte key[], byte text[])
//package com.java2s; /*// w w w. j a v a2 s. c o m * Copyrigth (C) 2010 Henrik Baastrup. * * Licensed under the GNU Lesser General Public License version 3; * you may not use this file except in compliance with the License. * You should have received a copy of the license together with this * file but can obtain a copy of the License at: * * http://www.gnu.org/licenses/lgpl-3.0.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { /** * Internal used. * @param key * @param text * @return */ public static byte[] hmac(byte key[], byte text[]) { return hmac(key, text, 64); } /** * Internal used. * @param key * @param text * @param blockSize * @return */ public static byte[] hmac(byte key[], byte text[], int blockSize) { byte key0[]; if (key.length == blockSize) key0 = key; else if (key.length < blockSize) { key0 = new byte[blockSize]; for (int i = 0; i < key.length; i++) key0[i] = key[i]; for (int i = key.length; i < blockSize; i++) key0[i] = 0; } else { byte h[] = hash(key); key0 = new byte[blockSize]; int len = blockSize; if (h.length < blockSize) len = h.length; for (int i = 0; i < len; i++) key0[i] = h[i]; for (int i = len; i < blockSize; i++) key0[i] = 0; } byte ipad[] = new byte[key0.length]; for (int i = 0; i < key0.length; i++) ipad[i] = (byte) (key0[i] ^ 0x36); byte res[] = new byte[ipad.length + text.length]; for (int i = 0; i < ipad.length; i++) res[i] = ipad[i]; for (int i = 0; i < text.length; i++) res[i + ipad.length] = text[i]; byte h[] = hash(res); byte opad[] = new byte[key0.length]; for (int i = 0; i < key0.length; i++) opad[i] = (byte) (key0[i] ^ 0x5c); res = new byte[opad.length + h.length]; for (int i = 0; i < opad.length; i++) res[i] = opad[i]; for (int i = 0; i < h.length; i++) res[i + opad.length] = h[i]; h = hash(res); // System.out.print("hmac: "); // for (int i=0; i<h.length; i++) System.out.print(Integer.toString( ( h[i] & 0xff ) + 0x100, 16).substring( 1 )); // System.out.println(""); // System.out.print("key: "); // for (int i=0; i<key.length; i++) System.out.print(Integer.toString( ( key[i] & 0xff ) + 0x100, 16).substring( 1 )); // System.out.println(""); // System.out.print("text: "); // for (int i=0; i<text.length; i++) System.out.print(Integer.toString( ( text[i] & 0xff ) + 0x100, 16).substring( 1 )); // System.out.println(""); return h; } private static byte[] hash(byte input[]) { MessageDigest md; try { md = MessageDigest.getInstance("SHA1"); } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); return null; } md.update(input); byte digest[] = md.digest(); return digest; } }