⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 md5.java

📁 基于MJSIP的j2me客户端
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) 2005 Luca Veltri - University of Parma - Italy
 * 
 * This file is part of MjSip (http://www.mjsip.org)
 * 
 * MjSip is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * MjSip is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MjSip; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Author(s):
 * Luca Veltri (luca.veltri@unipr.it)
 */

package org.zoolu.tools;



/** MD5 hash algorithm.
  * <p> Implements the RSA Data Security, Inc. MD5 Message-Digest Algorithm.
  * This is almoust straight implementation of the reference implementation
  * given in RFC1321 by RSA.
  */
public class MD5 extends MessageDigest
{

   // ********************** Mangle functions **********************

   /** Rotates w, shifting n bits left. */
   //private static int rotateLeft(int w, int n) {  return (w << n) | (w >>> (32-n));  }

   /** Rotates w, shifting n bits right. */
   //private static int rotateRight(int w, int n) {  return (w >>> n) | (w << (32-n));  }

   /** Rotates an array of words, shifting 1 word left. */
   /*private static int[] rotateLeft(int[] w)
   {  int len=w.length;
      int w1=w[len-1];
      for (int i=len-1; i>0; i--) w[i]=w[i-1];
      w[0]=w1;
      return w;
   }*/

   /** Rotates an array of words, shifting 1 word right. */
   /*private static int[] rotateRight(int[] w)
   {  int len=w.length;
      int w0=w[0];
      for (int i=1; i<len; i++) w[i-1]=w[i];
      w[len-1]=w0;
      return w;
   }*/

   /** Gets the unsigned representatin of a byte (into a short) */
   //public static short uByte(byte b) {  return (short)(((short)b+256)%256);  } 

   /** Gets the unsigned representatin of a 32-bit word (into a long) */
   /*public static long uWord(int n)
   {  long wmask=0x10000;
      wmask*=wmask;
      return (long)(((long)n+wmask)%wmask);
   }*/ 

   /** Copies all bytes of array <i>src</i> into array <i>dst</i> with offset <i>offset</i> */
   //public static void copyBytes(byte[] src, byte[] dst, int offset) { for (int k=0; k<src.length; k++) dst[offset+k]=src[k]; }

   /** Copies the first <i>len</i> bytes of array <i>src</i> into array <i>dst</i> with offset <i>offset</i> */
   //public static void copyBytes(byte[] src, byte[] dst, int offset, int len) { for (int k=0; k<len; k++) dst[offset+k]=src[k]; }

   /** Transforms a 4-bytes array into a 32-bit word (with the more significative byte at left) */
   //public static long bytesToWord(byte[] b, int offset) {  return ((((((long)uByte(b[offset+3])<<8)+uByte(b[offset+2]))<<8)+uByte(b[offset+1]))<<8)+uByte(b[offset+0]);  }

   /** Transforms a 4-bytes array into a 32-bit word (with the more significative byte at left) */
   //public static long bytesToWord(byte[] b) {  return ((((((long)uByte(b[3])<<8)+uByte(b[2]))<<8)+uByte(b[1]))<<8)+uByte(b[0]);  }
   
   /** Transforms a 32-bit word (with the more significative byte at left) into a 4-bytes array */
   /*public static byte[] wordToBytes(long n)
   {  byte[] b=new byte[4];
      b[3]=(byte)(n>>24);
      b[2]=(byte)((n>>16)%256);
      b[1]=(byte)((n>>8)%256);
      b[0]=(byte)(n%256);
      return b;
   }*/


   // ************************* Attributes *************************

   /** The digest */
   byte[] message_digest;

   /** byte counter mod 2^64 */
   long count;

   /** 128bit state (A,B,C, and D words) */
   int state[];

   /** 64B (512 bits) chunk of the input message */
   byte block[];

   /** Number of bytes remained into the chunk */
   int block_offset;

   /** Padding */
   static byte zeropadding[]= { (byte)0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };


   /* Constants for MD5 transformation */
   /*
   static final int S11=7;
   static final int S12=12;
   static final int S13=17;
   static final int S14=22;
   static final int S21=5;
   static final int S22=9;
   static final int S23=14;
   static final int S24=20;
   static final int S31=4;
   static final int S32=11;
   static final int S33=16;
   static final int S34=23;
   static final int S41=6;
   static final int S42=10;
   static final int S43=15;
   static final int S44=21;
   */

   
   // *********************** Public methods ***********************

   /** Constructor */
   public MD5()
   {  init();
   }


   /** Constructor */
   public MD5(byte[] buffer)
   {  init();
      update(buffer);
   }


   /** Constructor */
   public MD5(byte[] buffer, int offset, int len)
   {  init();
      update(buffer,offset,len);
   }


   /** Constructor */
   public MD5(String str)
   {  init();
      update(str);
   }


   /** Inits the MD5 */
   private void init()
   {  count=0;
      // init the first block
      block=new byte[64];
      block_offset=0;
      // load magic initialization constants 
      state=new int[4];
      state[0]=0x67452301;
      state[1]=0xefcdab89;
      state[2]=0x98badcfe;
      state[3]=0x10325476;
      
      message_digest=null;
   }


   /** MessageDigest block update operation.
     * Continues a message-digest operation,
     * processing another message block, and updating the context. */
   /*public MD5 update(String str)
   {  byte[] buf=str.getBytes();
      return update(buf,0,buf.length);
   }*/


   /** MessageDigest block update operation.
     * Continues a message-digest operation,
     * processing another message block, and updating the context. */
   /*public MD5 update(byte[] buffer)
   {  return update(buffer,0,buffer.length);
   }*/


   /** MessageDigest block update operation.
     * Continues a message-digest operation,
     * processing another message block, and updating the context. */
   public MessageDigest update(byte[] buffer, int offset, int len)
   {
      if (message_digest!=null) return this;
      //else
       
      count+=len;
      
      // num of remaining bytes to be processed
      int size=block.length-block_offset;

      while(len>=size)
      {  // fill a block
         for (int i=0; i<size; i++) block[block_offset+i]=buffer[offset+i];

         // process the block
         transform(state,block);
         
         // update offset, len, etc.
         offset+=size;
         len-=size;
         block_offset=0;
         size=block.length-block_offset;
      }
      // copy the remaining bytes
      for (int i=0; i<len; i++) block[block_offset+i]=buffer[offset+i];
      block_offset+=len;     

      return this;
   }


   /** Gets the MessageDigest. The same as doFinal(). */
   /*public byte[] getDigest()
   {  return doFinal();
   }*/


   /** MessageDigest finalization. Ends a message-digest operation, writing the
     * the message digest and zeroizing the context. */
   public byte[] doFinal()
   {
      if (message_digest!=null) return message_digest;
      //else

      // num of padding zeros (node: block_offset is at most 64) 
      int npad=64-((block_offset+8)%64);
      
      // set total bit length into the last 8 bytes
      long len=count*8;
      byte[] len_field=new byte[8];    
      for (int i=0; i<8; i++) {  len_field[i]=(byte)(len%256); len>>=8;  }

      // process the last chunk, i.e. zero-pading(1-64B) + length-field(8B) (1 or 2 blocks) 
      update(zeropadding,0,npad);
      update(len_field,0,8);
      
      message_digest=new byte[16];
      // convert 4 words to 16 bytes
      //for (int i=0; i<4; i++) {  copyBytes(wordToBytes(state[i]),message_digest,i*4);  }
      int k=0;
      for (int i=0; i<4; i++)
      {  message_digest[k++]=(byte)((state[i]) & 0xff);
         message_digest[k++]=(byte)((state[i]>>>8) & 0xff);
         message_digest[k++]=(byte)((state[i]>>>16) & 0xff);
         message_digest[k++]=(byte)((state[i]>>>24) & 0xff);
      }
      return message_digest;
   }


   /** MD5 basic transformation. Transforms state based on block. */
   private static void transform(int[] state, byte[] block)
   {
      int a=state[0];
      int b=state[1];
      int c=state[2];
      int d=state[3];

      int[] x=new int[16];
      x[0]=((int) (block[0] & 0xff)) |
          (((int) (block[1] & 0xff)) << 8) |
          (((int) (block[2] & 0xff)) << 16) |
          (((int) (block[3])) << 24);
      x[1]=((int) (block[4] & 0xff)) |
          (((int) (block[5] & 0xff)) << 8) |
          (((int) (block[6] & 0xff)) << 16) |
          (((int) (block[7])) << 24);
      x[2]=((int) (block[8] & 0xff)) |
          (((int) (block[9] & 0xff)) << 8) |
          (((int) (block[10] & 0xff)) << 16) |
          (((int) (block[11])) << 24);
      x[3]=((int) (block[12] & 0xff)) |
          (((int) (block[13] & 0xff)) << 8) |
          (((int) (block[14] & 0xff)) << 16) |
          (((int) (block[15])) << 24);
      x[4]=((int) (block[16] & 0xff)) |
          (((int) (block[17] & 0xff)) << 8) |
          (((int) (block[18] & 0xff)) << 16) |
          (((int) (block[19])) << 24);
      x[5]=((int) (block[20] & 0xff)) |
          (((int) (block[21] & 0xff)) << 8) |
          (((int) (block[22] & 0xff)) << 16) |
          (((int) (block[23])) << 24);
      x[6]=((int) (block[24] & 0xff)) |
          (((int) (block[25] & 0xff)) << 8) |
          (((int) (block[26] & 0xff)) << 16) |
          (((int) (block[27])) << 24);
      x[7]=((int) (block[28] & 0xff)) |
          (((int) (block[29] & 0xff)) << 8) |
          (((int) (block[30] & 0xff)) << 16) |
          (((int) (block[31])) << 24);
      x[8]=((int) (block[32] & 0xff)) |
          (((int) (block[33] & 0xff)) << 8) |
          (((int) (block[34] & 0xff)) << 16) |
          (((int) (block[35])) << 24);
      x[9]=((int) (block[36] & 0xff)) |
          (((int) (block[37] & 0xff)) << 8) |
          (((int) (block[38] & 0xff)) << 16) |
          (((int) (block[39])) << 24);
      x[10]=((int)(block[40] & 0xff)) |
          (((int) (block[41] & 0xff)) << 8) |
          (((int) (block[42] & 0xff)) << 16) |
          (((int) (block[43])) << 24);
      x[11]=((int)(block[44] & 0xff)) |
          (((int) (block[45] & 0xff)) << 8) |
          (((int) (block[46] & 0xff)) << 16) |
          (((int) (block[47])) << 24);
      x[12]=((int)(block[48] & 0xff)) |
          (((int) (block[49] & 0xff)) << 8) |
          (((int) (block[50] & 0xff)) << 16) |
          (((int) (block[51])) << 24);
      x[13]=((int)(block[52] & 0xff)) |

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -