Results 1 to 1 of 1
  1. #1
    chaibva is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default MessageDigest output different from that generated using j2se SHA-1 or MD5

    Can you please help.

    I have 2 methods for generating hashed strings using md2 ,md5,sha-1 . the problem is that they produce different output. one runs under j2se and the other j2me.

    here is the code.

    J2SE
    public static String calculateSecurityHash(String stringInput, String algorithmName) throws java.security.NoSuchAlgorithmException {

    String hexMessageEncode = "";

    byte[] buffer = stringInput.getBytes("UTF-8");

    java.security.MessageDigest messageDigest =
    java.security.MessageDigest.getInstance(algorithmN ame);

    messageDigest.update(buffer);

    byte[] messageDigestBytes = messageDigest.digest();

    for (int index = 0; index < messageDigestBytes.length; index++) {

    int countEncode = messageDigestBytes[index] & 0xff;

    if (Integer.toHexString(countEncode).length() == 1) {
    hexMessageEncode = hexMessageEncode + "0";
    }

    hexMessageEncode = hexMessageEncode + Integer.toHexString(countEncode);

    }

    return hexMessageEncode;

    }

    j2ME code


    public static String calculateSecurityHash(String stringInput, String algorithmName,int len) throws java.security.NoSuchAlgorithmException, DigestException, UnsupportedEncodingException {

    String hexMessageEncode = "";

    byte[] buffer = stringInput.getBytes("UTF-8");

    java.security.MessageDigest messageDigest =
    java.security.MessageDigest.getInstance(algorithmN ame);

    messageDigest.update(buffer, 0, buffer.length);




    byte [] messageDigestBytes = new byte[len];
    messageDigest.digest(messageDigestBytes, 0, len);

    for (int index = 0; index < messageDigestBytes.length; index++) {

    int countEncode = messageDigestBytes[index] & 0xff;

    if (Integer.toHexString(countEncode).length() == 1) {
    hexMessageEncode = hexMessageEncode + "0";
    }

    hexMessageEncode = hexMessageEncode + Integer.toHexString(countEncode);

    }

    return hexMessageEncode;

    }
    Last edited by chaibva; 07-19-2011 at 05:50 AM.

Similar Threads

  1. toString() method of java.security.MessageDigest
    By Nicholas Jordan in forum Advanced Java
    Replies: 4
    Last Post: 09-18-2008, 02:32 PM
  2. Why to Use J2SE and Option for using this..
    By mgrprasad90 in forum Java Applets
    Replies: 0
    Last Post: 08-27-2008, 09:27 AM
  3. Sdk & J2se
    By Gajesh Tripathi in forum New To Java
    Replies: 0
    Last Post: 10-18-2007, 07:49 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •