熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

md5算法的java源代碼

2013-11-23 19:29:26  來源: Java核心技術 

  public class MD
  {
  /*
  * A Java implementation of the RSA Data Security Inc MD Message
  * Digest Algorithm as defined in RFC
  * Based on the JavaScript implementation of Paul Johnston
  * Copyright (C) Paul Johnston
  * See for details
  * Java Version by Thomas Weber (Orange Interactive GmbH)
  */
  
  /*
  * Convert a bit number to a hex string with lsbyte first
  */
  String hex_chr = abcdef;
  private String rhex(int num)
  {
  String str = ;
  for(int j = ; j <= 3; j++)
  str = str + hex_chr.charAt((num >> (j * + )) & xF) + hex_chrcharAt((num >> (j * )) & xF);
  return str;
  }
  
  /*
  * Convert a string to a sequence of word blocks stored as an array
  * Append padding bits and the length as described in the MD standard
  */
  private int[] strblks_MD(String str)
  {
  int nblk = ((strlength() + ) >> ) + ;
  int[] blks = new int[nblk * ];
  int i = ;
  for(i = ; i < nblk * ; i++) {
  blks[i] = ;
  }
  for(i = ; i < strlength(); i++) {
  blks[i >> ] |= strcharAt(i) << ((i % ) * );
  }
  blks[i >> ] |= x << ((i % ) * );
  blks[nblk * ] = strlength()*;
  
  return blks;
  }
  
  /*
  * Add integers wrapping at ^
  */
  private int add(int x int y)
  {
  return ((x&xFFFFFFF) + (y&xFFFFFFF)) ^ (x&x) ^ (y&x);
  }
  
  /*
  * Bitwise rotate a bit number to the left
  */
  private int rol(int num int cnt)
  {
  return (num << cnt) | (num >>> ( cnt));
  }
  
  /*
  * These functions implement the basic operation for each round of the
  * algorithm
  */
  private int cmn(int q int a int b int x int s int t)
  {
  return add(rol(add(add(a q) add(x t)) s) b);
  }
  private int ff(int a int b int c int d int x int s int t)
  {
  return cmn((b & c) | ((~b) & d) a b x s t);
  }
  private int gg(int a int b int c int d int x int s int t)
  {
  return cmn((b & d) | (c & (~d)) a b x s t);
  }
  private int hh(int a int b int c int d int x int s int t)
  {
  return cmn(b ^ c ^ d a b x s t);
  }
  private int ii(int a int b int c int d int x int s int t)
  {
  return cmn(c ^ (b | (~d)) a b x s t);
  }
  
  /*
  * Take a string and return the hex representation of its MD
  */
  public String calcMD(String str)
  {
  int[] x = strblks_MD(str);
  int a = x;
  int b = xEFCDAB;
  int c = xBADCFE;
  int d = x;
  
  for(int i = ; i < xlength; i += )
  {
  int olda = a;
  int oldb = b;
  int oldc = c;
  int oldd = d;
  
  a = ff(a b c d x[i+ ] xDAA);
  d = ff(d a b c x[i+ ] xECB);
  c = ff(c d a b x[i+ ] xDB);
  b = ff(b c d a x[i+ ] xCBDCEEE);
  a = ff(a b c d x[i+ ] xFCFAF);
  d = ff(d a b c x[i+ ] xCA);
  c = ff(c d a b x[i+ ] xA);
  b = ff(b c d a x[i+ ] xFD);
  a = ff(a b c d x[i+ ] xD);
  d = ff(d a b c x[i+ ] xBFAF);
  c = ff(c d a b x[i+] xFFFFBB);
  b = ff(b c d a x[i+] xCDBE);
  a = ff(a b c d x[i+] xB);
  d = ff(d a b c x[i+] xFD);
  c = ff(c d a b x[i+] xAE);
  b = ff(b c d a x[i+] xB);
  
  a = gg(a b c d x[i+ ] xFE);
  d = gg(d a b c x[i+ ] xCB);
  c = gg(c d a b x[i+] xEA);
  b = gg(b c d a x[i+ ] xEBCAA);
  a = gg(a b c d x[i+ ] xDFD);
  d = gg(d a b c x[i+] x);
  c = gg(c d a b x[i+] xDAE);
  b = gg(b c d a x[i+ ] xEDFBC);
  a = gg(a b c d x[i+ ] xECDE);
  d = gg(d a b c x[i+] xCD);
  c = gg(c d a b x[i+ ] xFDD);
  b = gg(b c d a x[i+ ] xAED);
  a = gg(a b c d x[i+] xAEE);
  d = gg(d a b c x[i+ ] xFCEFAF);
  c = gg(c d a b x[i+ ] xFD);
  b = gg(b c d a x[i+] xDACA);
  
  a = hh(a b c d x[i+ ] xFFFA);
  d = hh(d a b c x[i+ ] xF);
  c = hh(c d a b x[i+] xDD);
  b = hh(b c d a x[i+] xFDEC);
  a = hh(a b c d x[i+ ] xABEEA);
  d = hh(d a b c x[i+ ] xBDECFA);
  c = hh(c d a b x[i+ ] xFBBB);
  b = hh(b c d a x[i+] xBEBFBC);
  a = hh(a b c d x[i+] xBEC);
  d = hh(d a b c x[i+ ] xEAAFA);
  c = hh(c d a b x[i+ ] xDEF);
  b = hh(b c d a x[i+ ] xD);
  a = hh(a b c d x[i+ ] xDDD);
  d = hh(d a b c x[i+] xEDBE);
  c = hh(c d a b x[i+] xFACF);
  b = hh(b c d a x[i+ ] xCAC);
  
  a = ii(a b c d x[i+ ] xF);
  d = ii(d a b c x[i+ ] xAFF);
  c = ii(c d a b x[i+] xABA);
  b = ii(b c d a x[i+ ] xFCA);
  a = ii(a b c d x[i+] xBC);
  d = ii(d a b c x[i+ ] xFCCC);
  c = ii(c d a b x[i+] xFFEFFD);
  b = ii(b c d a x[i+ ] xDD);
  a = ii(a b c d x[i+ ] xFAEF);
  d = ii(d a b c x[i+] xFECEE);
  c = ii(c d a b x[i+ ] xA);
  b = ii(b c d a x[i+] xEA);
  a = ii(a b c d x[i+ ] xFE);
  d = ii(d a b c x[i+] xBDAF);
  c = ii(c d a b x[i+ ] xADDBB);
  b = ii(b c d a x[i+ ] xEBD);
  
  a = add(a olda);
  b = add(b oldb);
  c = add(c oldc);
  d = add(d oldd);
  }
  return rhex(a) + rhex(b) + rhex(c) + rhex(d);
  }
  
  }
  
  另: 一中國人寫的md 的javabean:
  
  /************************************************
  MD 算法的Java Bean
  @author:Topcat Tuppin
  Last Modified:Mar
  *************************************************/
  //package beartool;
  import javalangreflect*;
  /*************************************************
  md 類實現了RSA Data Security Inc在提交給IETF
  的RFC中的MD messagedigest 算法
  *************************************************/
  
  public class MD {
  /* 下面這些SS實際上是一個*的矩陣在原始的C實現中是用#define 實現的
  這裡把它們實現成為static final是表示了只讀切能在同一個進程空間內的多個
  Instance間共享*/
  static final int S = ;
  static final int S = ;
  static final int S = ;
  static final int S = ;
  
  static final int S = ;
  static final int S = ;
  static final int S = ;
  static final int S = ;
  
  static final int S = ;
  static final int S = ;
  static final int S
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26958.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.