CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

The working version (TM) :)


[Replies] [Reply] [View by Thread] [Help]
[Back To SNEAK Development Forum]

Posted by Maj anvar On 2005-03-18 14:33:00
In Reply to Java - MD5 - First Draft Posted by Maj anvar On 2005-03-14 19:01:39

Maj
Maj anvar



Alright then, here's the working version. Thanks for pointing out that it were all hex value's (I must have been really sleeping back there :)
I tested it with the tests supplyed at http://faqs.org/rfcs/rfc1321.html and they match, so I believe this version is working properly, although there is maybe some optimalisation to do.

Comments/etc is welcomed. :)
package net.cyberarmy.sneak.scheme;

import net.cyberarmy.sneak.Scheme;
import java.security.MessageDigest;

public class MD5Scheme implements Scheme {
   public String getNameKey() {
        return "scheme.MD5.name";
    }

    public String getDescriptionKey() {
        return "scheme.MD5.description";
    }

    /**
     * Default method to run this.
     * @param input String -- The string we want to MD5.
     * @return String -- The result we get.
     */
    public String execute(String input) {

      if (input == null) return null;
      input = input.trim();
      if (input.length() == 0) return null;

      MessageDigest md = null;
      try {
         md = MessageDigest.getInstance("MD5");
      }

      // Eat the exception because it shouldn't happen.
      catch (Exception e) {
        return "Source-code Error";
      }

      // Get the array of bytes representing the MD5 hash.
      byte[] bytes = md.digest(input.getBytes());
      StringBuffer md5 = new StringBuffer();
      // Loop through the array to get all the bytes, convert 
      // them to hex, and add them to our string.
      for (int i = 0; i < 16; i++) {
        md5.append(String.format("%02x", bytes[i]));
      }
       return md5.toString();
    }
}
Major Anvar
It's not who you are that makes you do things, it's why you do things that determines who you are.

-Vandread



Replies:


Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.12182 seconds


About Us | Privacy Policy | Mission Statement | Help