00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ 00002 /* 00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are met: 00007 00008 1. Redistributions of source code must retain the above copyright notice, 00009 this list of conditions and the following disclaimer. 00010 00011 2. Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in 00013 the documentation and/or other materials provided with the distribution. 00014 00015 3. The names of the authors may not be used to endorse or promote products 00016 derived from this software without specific prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, 00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, 00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 package com.jcraft.jsch.jce; 00031 00032 import com.jcraft.jsch.MAC; 00033 import javax.crypto.*; 00034 import javax.crypto.spec.*; 00035 00036 public class HMACMD5 implements MAC{ 00037 private static final String name="hmac-md5"; 00038 private static final int BSIZE=16; 00039 private Mac mac; 00040 public int getBlockSize(){return BSIZE;}; 00041 public void init(byte[] key) throws Exception{ 00042 if(key.length>BSIZE){ 00043 byte[] tmp=new byte[BSIZE]; 00044 System.arraycopy(key, 0, tmp, 0, BSIZE); 00045 key=tmp; 00046 } 00047 00048 SecretKeySpec skey=new SecretKeySpec(key, "HmacMD5"); 00049 mac=Mac.getInstance("HmacMD5"); 00050 mac.init(skey); 00051 } 00052 00053 private final byte[] tmp=new byte[4]; 00054 public void update(int i){ 00055 tmp[0]=(byte)(i>>>24); 00056 tmp[1]=(byte)(i>>>16); 00057 tmp[2]=(byte)(i>>>8); 00058 tmp[3]=(byte)i; 00059 update(tmp, 0, 4); 00060 } 00061 public void update(byte foo[], int s, int l){ 00062 mac.update(foo, s, l); 00063 } 00064 public void doFinal(byte[] buf, int offset){ 00065 try{ 00066 mac.doFinal(buf, offset); 00067 } 00068 catch(ShortBufferException e){ 00069 } 00070 } 00071 00072 public String getName(){ 00073 return name; 00074 } 00075 }