Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java Security  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 还可不可以再改进完善一下这个md5程序??
nothing

天外飞仙.....

CJSDN高级会员


发贴: 1636
积分: 131
于 2003-07-11 00:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
// 程序名为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;
  }
}


nothing edited on 2003-07-11 02:33


躲得过的怪物,躲不过的刺激

作者 Re:还可不可以再改进完善一下这个md5程序?? [Re:nothing]
menzy



版主


发贴: 754
积分: 113
于 2003-07-11 18:32 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
a bug on
try{
...
}catch (Exception e){
return null;
}
//
other codes are perfect.



作者 Re:还可不可以再改进完善一下这个md5程序?? [Re:menzy]
nothing

天外飞仙.....

CJSDN高级会员


发贴: 1636
积分: 131
于 2003-07-11 18:51 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我也想改掉这部分,总是改不好....



躲得过的怪物,躲不过的刺激

作者 Re:还可不可以再改进完善一下这个md5程序?? [Re:nothing]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2003-07-11 22:23 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

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;
}
}



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:还可不可以再改进完善一下这个md5程序?? [Re:floater]
nothing

天外飞仙.....

CJSDN高级会员


发贴: 1636
积分: 131
于 2003-07-11 23:08 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
MMM.java:22: warning: finally clause cannot complete normally



躲得过的怪物,躲不过的刺激

作者 Re:还可不可以再改进完善一下这个md5程序?? [Re:nothing]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2003-07-12 02:30 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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.


floater edited on 2003-07-12 02:34

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:还可不可以再改进完善一下这个md5程序?? [Re:nothing]
nov1



发贴: 0
积分: 0
于 2003-07-13 06:02 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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);
}
}



作者 Re:还可不可以再改进完善一下这个md5程序?? [Re:nothing]
nothing

天外飞仙.....

CJSDN高级会员


发贴: 1636
积分: 131
于 2003-07-13 12:29 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
it's OK !

thanks !!

SmileSmile




躲得过的怪物,躲不过的刺激

作者 Re:还可不可以再改进完善一下这个md5程序?? [Re:nothing]
TopCool

Miracle

CJSDN高级会员


发贴: 253
积分: 10
于 2003-07-18 11:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
nov1 牛


夹着尾巴做人,扎扎实实学技术
作者 Re:还可不可以再改进完善一下这个md5程序?? [Re:nothing]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2003-07-19 00:39 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
The constructor should be as simple as possible, throwing any exception here will make troubleshooting later on very hard. - My exp.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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