HMAC.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2006-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.jcraft;
00031 
00032 import java.security.*;
00033 
00034 class HMAC{
00035 
00036   /*
00037    * Refer to RFC2104.
00038    *
00039    * H(K XOR opad, H(K XOR ipad, text))
00040    *
00041    * where K is an n byte key
00042    * ipad is the byte 0x36 repeated 64 times
00043    * opad is the byte 0x5c repeated 64 times
00044    * and text is the data being protected
00045    */
00046   private static final int B=64;
00047   private byte[] k_ipad=null;
00048   private byte[] k_opad=null;
00049 
00050   private MessageDigest md=null;
00051 
00052   private int bsize=0;
00053 
00054   protected void setH(MessageDigest md){
00055     this.md=md;
00056     bsize=md.getDigestLength();
00057   }
00058 
00059   public int getBlockSize(){return bsize;};
00060   public void init(byte[] key) throws Exception{
00061     if(key.length>bsize){
00062       byte[] tmp=new byte[bsize];
00063       System.arraycopy(key, 0, tmp, 0, bsize);    
00064       key=tmp;
00065     }
00066 
00067     /* if key is longer than B bytes reset it to key=MD5(key) */
00068     if(key.length>B){
00069       md.update(key, 0, key.length);
00070       key=md.digest();
00071     }
00072 
00073     k_ipad=new byte[B];
00074     System.arraycopy(key, 0, k_ipad, 0, key.length);
00075     k_opad=new byte[B];
00076     System.arraycopy(key, 0, k_opad, 0, key.length);
00077 
00078     /* XOR key with ipad and opad values */
00079     for(int i=0; i<B; i++) {
00080       k_ipad[i]^=(byte)0x36;
00081       k_opad[i]^=(byte)0x5c;
00082     }
00083 
00084     md.update(k_ipad, 0, B);
00085   }
00086 
00087   private final byte[] tmp=new byte[4];
00088   public void update(int i){
00089     tmp[0]=(byte)(i>>>24);
00090     tmp[1]=(byte)(i>>>16);
00091     tmp[2]=(byte)(i>>>8);
00092     tmp[3]=(byte)i;
00093     update(tmp, 0, 4);
00094   }
00095 
00096   public void update(byte foo[], int s, int l){
00097     md.update(foo, s, l);
00098   }
00099 
00100   public void doFinal(byte[] buf, int offset){
00101     byte[] result=md.digest();
00102     md.update(k_opad, 0, B);
00103     md.update(result, 0, bsize);
00104     try{md.digest(buf, offset, bsize);}catch(Exception e){}
00105     md.update(k_ipad, 0, B);
00106   }
00107 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1