Topic: 还可不可以再改进完善一下这个md5程序??

  Print this page

1.还可不可以再改进完善一下这个md5程序?? Copy to clipboard
Posted by: nothing
Posted on: 2003-07-11 00:01

// 程序名为MMM.java
package md5;
import java.security.*;

public class MMM {
  public String calcMD5(String str) {
    try{
      MessageDigest alga=MessageDigest.getInstance("MD5");
      alga.update(str.getBytes());
      byte[] digesta=alga.digest();
      return byte2hex(digesta);
    }catch (Exception e){
      return null;
    }
  }
  
  public String byte2hex(byte[] b) { //二行制转字符串
    String hs="";
    String stmp="";
    for (int n=0;n<b.length;n++) {
      stmp=(Integer.toHexString(b[n] & 0XFF));
      if (stmp.length()==1) {
        hs=hs+"0"+stmp;
      }else {
        hs=hs+stmp;
      }
      if (n<b.length-1) {
        hs=hs+"";
      }
    }
  // return hs.toUpperCase();
  return hs;
  }
}

2.Re:还可不可以再改进完善一下这个md5程序?? [Re: nothing] Copy to clipboard
Posted by: menzy
Posted on: 2003-07-11 18:32

a bug on
try{
...
}catch (Exception e){
return null;
}
//
other codes are perfect.

3.Re:还可不可以再改进完善一下这个md5程序?? [Re: menzy] Copy to clipboard
Posted by: nothing
Posted on: 2003-07-11 18:51

我也想改掉这部分,总是改不好....

4.Re:还可不可以再改进完善一下这个md5程序?? [Re: nothing] Copy to clipboard
Posted by: floater
Posted on: 2003-07-11 22:23


public String calcMD5(String str)
{
String ret = null;
try
{
MessageDigest alga = MessageDigest.getInstance("MD5");
alga.update(str.getBytes());
byte[] digesta = alga.digest();
ret = byte2hex(digesta);
}
catch (NoSuchAlgorithmException e)
{
}
finally
{
return ret;
}
}

5.Re:还可不可以再改进完善一下这个md5程序?? [Re: floater] Copy to clipboard
Posted by: nothing
Posted on: 2003-07-11 23:08

MMM.java:22: warning: finally clause cannot complete normally

6.Re:还可不可以再改进完善一下这个md5程序?? [Re: nothing] Copy to clipboard
Posted by: floater
Posted on: 2003-07-12 02:30

which compiler are you using?

It's fine on jb8.

You can still run it since it's a warning, not an error, anyway.

Normally, you should handle the exception and move the return line to somewhere after the finally block so that it either blows up or you get a result.

7.Re:还可不可以再改进完善一下这个md5程序?? [Re: nothing] Copy to clipboard
Posted by: nov1
Posted on: 2003-07-13 06:02

import java.security.*;

public class MMM
{
static private MessageDigest msgDigest;

// if an algorithm does not exist, let caller to handle the NoSuchAlgorithmException
// because this class has no knowledge about how to handle it
public MMM(String alg)
throws NoSuchAlgorithmException
{
msgDigest = MessageDigest.getInstance(alg);
}

// or you can catch it if you believe it will never happen
public MMM()
{
try
{
msgDigest = MessageDigest.getInstance("MD5");
}
catch(NoSuchAlgorithmException e)
{}
}

public String calcHexDigest(String msg)
{
return bytes2hex(msgDigest.digest(msg.getBytes()));
}

public String bytes2hex(byte[] bytes)
{
StringBuffer buf = new StringBuffer(bytes.length * 2);
for(int pos = 0; pos < bytes.length; pos++)
{
int n = bytes[pos] & 0xFF;
buf.append(halfByte2hex((n >> 4) & 0xF));
buf.append(halfByte2hex(n & 0xF));
}
return buf.toString();
}

public char halfByte2hex(int n) // ensure: 0 <= n <= 15
{
return (char)(n > 9 ? 'A' + n - 10 : '0' + n);
}
}

8.Re:还可不可以再改进完善一下这个md5程序?? [Re: nothing] Copy to clipboard
Posted by: nothing
Posted on: 2003-07-13 12:29

it's OK !

thanks !!

SmileSmile

9.Re:还可不可以再改进完善一下这个md5程序?? [Re: nothing] Copy to clipboard
Posted by: TopCool
Posted on: 2003-07-18 11:10

nov1 牛

10.Re:还可不可以再改进完善一下这个md5程序?? [Re: nothing] Copy to clipboard
Posted by: floater
Posted on: 2003-07-19 00:39

The constructor should be as simple as possible, throwing any exception here will make troubleshooting later on very hard. - My exp.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923