TheLinuxDuck
07-30-2001, 03:01 PM
Does anyone know of an md5 class for java? If not, does anyone know how to emulate an unsigned integer in java?
::sigh::
::sigh::
|
Click to See Complete Forum and Search --> : java and md5 TheLinuxDuck 07-30-2001, 03:01 PM Does anyone know of an md5 class for java? If not, does anyone know how to emulate an unsigned integer in java? ::sigh:: TheLinuxDuck 07-30-2001, 03:12 PM Nevermind, I found one here (http://www.cs.hut.fi/~santtu/java/) Thanks though! feve 07-30-2001, 04:50 PM I don't know if you're still interested in another one. Sun's JDK does come with one as part of the java.security package. Try the API here (http://java.sun.com/j2se/1.3/docs/api/java/security/MessageDigest.html) if you are interested. TheLinuxDuck 07-30-2001, 05:41 PM Feve: Thanks! I'll prolly use that one then, because the one I found was written in '96, and contains some deprecated methods. It works nicely, though, other than that. [ 30 July 2001: Message edited by: TheLinuxDuck ] Dru Lee Parsec 07-30-2001, 08:54 PM Unsigned anything in Java: There ain't none. Trick: Cast it to the next largest size and bitwise AND it with 0xffff So if you put F45C into an int then it will display as a negative number. But you want the actual unsigned value so you do this: int n = [some soon to be unsigned number]; long myLogVariable = n & 0xffff; myLongVariable is now the unsigned version of n. TheLinuxDuck 07-31-2001, 09:00 AM Thanks for the heads up, Dru. I knew that java didn't have unsigned variables, so I wasn't sure how to deal with that. The md5 source code that I found (link above) uses that trick for doing some integer additions. Bitwise stuff have never been my strong suit, so I wasn't sure exactly what was going on, but now I do. And knowing is half the battle! G.I.Joe! nanode 07-31-2001, 05:27 PM Linux duck: Here's some code I stole from a book and tweaked somewhat. It simply does MD5 from command line input. I was playing around with verifying the integrity of serialized objects and found this interesting: import java.io.*; import java.security.*; import sun.misc.*; public class MD5Input { public static void main(String[] args) { if (args.length < 1) { System.out.println("Usage: java MD5Input <string input>"); return; } String input = args[0]; for(int i=1;i < args.length;i++) { input += " " + args[i]; } try { byte[] inputBytes = input.getBytes("UTF8"); MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(inputBytes); byte[] digest = md5.digest(); BASE64Encoder encoder = new BASE64Encoder(); String base64 = encoder.encode(digest); System.out.println(base64); } catch(Exception e) { System.out.println("MD5 failed"); } } } TheLinuxDuck 08-03-2001, 03:30 PM nanode: Thanks for the code sample.. (^= I have a question, though. I've got an older browser (so that the applet will be usable by many) that doesn't have the MD5 algorithm in it's provider. How do I include that in with the applet, so I don't need it? TheLinuxDuck 08-06-2001, 08:49 AM Shameless bump. It's either I figure out how to include the MD5 algorithm with the applet, or I have to use a completely separate class to do the MD5 sum. I don't want to have to do that, but I will! (^= nanode 08-06-2001, 11:33 AM TLD: Do you mean that my code requires a newer java version than 1.1x? It probably does. Depending on what you intended your applet and/or webpage to use MD5 for, there might be some work arounds. A quick hack to integrate command-line progs to a webpage is with PHP. This is potentially very bad security, so watch out: function translate($latinString) { trim($latinString); $commandLine = "/usr/local/words/latin ".$latinString; exec($commandLine, $rawLines, $ok); $returnString; for($i=0;$i<sizeof($rawLines);$i++) { $returnString .= "<BR>".$rawLines[$i]; } return($returnString); } OF course that defeats the purpose of using java in the first place. I do a lot of applet work at my job. Cool technology, but java 2 applets are not yet feasible for the internet. The silly 20MB plugin is enough to alienate most people. We could blame Microsoft, but the problem wouldn't go away :( TheLinuxDuck 08-06-2001, 11:46 AM Take the following action event, for example. It merely makes a call to MD5Create, using the u and p TextField objects for it's input. // u and p are TextField objects public boolean action(Event evt, Object obj) { MD5Create myMD5; String fullString; fullString=new String(u.getText()+p.getText()); myMD5=new MD5Create(fullString); u.setText(fullString+myMD5.toString()); return true; } Now, here is the actual MD5Create, which basically just tells us an error, if one exists (since I'm only doing testing right now) class MD5Create { private String endProduct; public MD5Create(String input) { MessageDigest myDigest; try { myDigest=MessageDigest.getInstance("MD5"); } catch(NoSuchAlgorithmException e) { endProduct=new String("NoSuchAlgorithm"); } } public String toString() { if(endProduct==null) return "null"; return endProduct; } } Nothing too complex. The applet merely tries to make a call to an MessageDigest object. It always, for me, returns the 'NoSuchAlgorithm' message. My assumption is that the MD5 algorithm isn't included in the java version that is being accessed by my browser. The browser is older. But, I think it is wise to make the applet work in older browsers. Is there wa way to include the MD5 algorithm with the java class, or am I just misunderstanding what the algorithm really is? I'm sad. nanode 08-07-2001, 12:31 PM TLD: I'm still very new to MD5 and how it's implemented in java. There's probably a simple way to programmatically get a listing of all available algorithms. Try using one that is available just for amusement. TheLinuxDuck 08-07-2001, 02:47 PM Good idea. Now I just have to figure out what is available. (^= justlinux.com
Copyright Internet.com Inc. All Rights Reserved. |