Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8050982

MessageDigest.digest() results differ in JDK 7 and 8

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8u11
    • security-libs

      FULL PRODUCT VERSION :
      java version "1.7.0_51"
      Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
      Java HotSpot(TM) Client VM (build 24.51-b03, mixed mode, sharing)

      java version "1.8.0_11"
      Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
      Java HotSpot(TM) Client VM (build 25.11-b03, mixed mode)

      java version "1.8.0"
      Java(TM) SE Runtime Environment (build 1.8.0-b132)
      Java HotSpot(TM) Client VM (build 25.0-b70, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      window 7 professional 6.1.7601.17514

      A DESCRIPTION OF THE PROBLEM :
      when i try to hash a string pair and generate a byte array with encoding "GBK", and i test the function several times with four string pairs. But i get different result between jdk7 and jdk8
      A ("superuser" and "") result:equal
      B ("superuser" and "superuser") result:not equal
      C ("test62" and "test62") result:not equal
      D ("user6" and "user6") result:equal


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      all result will be same between jdk7 and jdk8 for every string pair

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      in jdk 7 i get :
      C:\Java\jdk1.7.0_51\bin>java Program test62 test62
      [-51, -126, -79, -62, 35, 41, 93, -102, -93, -21, -105, 58, 74, 102, 116, 17, 23, 14, 63]

      in jdk 8 i get:
      C:\Java\jdk\bin>java Program test62 test62
      [-51, -126, -79, -62, 35, 41, 93, -102, -93, -21, -105, 58, 74, 102, 116, 17, 23, 14, 63, 51]

      C:\Users\bfQ463\Downloads\jre-8u11-windows-i586\jre1.8.0_11\bin>java Program test62 test62
      [-51, -126, -79, -62, 35, 41, 93, -102, -93, -21, -105, 58, 74, 102, 116, 17, 23, 14, 63, 51]

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.UnsupportedEncodingException;
      import java.nio.charset.Charset;
      import java.security.MessageDigest;
      import java.security.NoSuchAlgorithmException;
      import java.util.*;



      public class Program
      {
          public static void main(String[] args) throws UnsupportedEncodingException
          {

              byte[] userNameBytes = ServerUtil.String2ByteArray(args[0], "GBK");
              byte[] passwordBytes = ServerUtil.String2ByteArray(args[1], "GBK");
              byte[] hashedPasswordBytes = ServerUtil.digestPassword(userNameBytes, passwordBytes);
              String tmp = new String(hashedPasswordBytes,"GBK");
              hashedPasswordBytes = tmp.getBytes("GBK");
              System.out.println(Arrays.toString(hashedPasswordBytes));
              
          }
          
          private static class ServerUtil
          {

              public synchronized static byte[] digestPassword(byte[] username, byte[] password)
              {
                  MessageDigest md = null;
                  if (md == null)
                  {
                      try
                      {
                          md = MessageDigest.getInstance("SHA");
                      }
                      catch (NoSuchAlgorithmException e)
                      {

                      }
                  }
                  md.reset();

                  if (password != null && password.length != 0)
                  {
                      md.update(password);
                  }
                  if (username != null && username.length != 0)
                  {
                      md.update(username);
                  }
                  else
                  {
                      return new byte[] {};
                  }

                  return md.digest();
              }

              public static byte[] String2ByteArray(String source, String encoding)
                      throws UnsupportedEncodingException
              {
                  if (null == source || "".equals(source))
                  {
                      return new byte[] {};
                  }
                  if (null == encoding || "".equals(encoding))
                  {
                      return source.getBytes();
                  }
                  return source.getBytes(encoding);
              }
          }
      }

      ---------- END SOURCE ----------

            mullan Sean Mullan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: