6832016: {DigestMD5Base,Des3DkCrypto}.setParityBit should use Integer.bitCount
authorweijun
Wed, 20 May 2009 10:11:23 +0800
changeset 2917 fcd464c4e52c
parent 2916 f8f76a2c1728
child 2918 395b9ffa7cc6
6832016: {DigestMD5Base,Des3DkCrypto}.setParityBit should use Integer.bitCount Reviewed-by: weijun Contributed-by: Christian Thalinger <christian.thalinger@sun.com>
jdk/src/share/classes/com/sun/security/sasl/digest/DigestMD5Base.java
jdk/src/share/classes/sun/security/krb5/internal/crypto/dk/Des3DkCrypto.java
--- a/jdk/src/share/classes/com/sun/security/sasl/digest/DigestMD5Base.java	Tue May 19 16:33:32 2009 -0700
+++ b/jdk/src/share/classes/com/sun/security/sasl/digest/DigestMD5Base.java	Wed May 20 10:11:23 2009 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 2000-2009 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -1516,11 +1516,6 @@
 
     // ---------------- DES and 3 DES key manipulation routines
 
-    /* Mask used to check for parity adjustment */
-    private static final byte[] PARITY_BIT_MASK = {
-        (byte)0x80, (byte)0x40, (byte)0x20, (byte)0x10,
-        (byte)0x08, (byte)0x04, (byte)0x02
-    };
     private static final BigInteger MASK = new BigInteger("7f", 16);
 
     /**
@@ -1529,21 +1524,9 @@
      */
     private static void setParityBit(byte[] key) {
         for (int i = 0; i < key.length; i++) {
-            int bitCount = 0;
-            for (int maskIndex = 0;
-                 maskIndex < PARITY_BIT_MASK.length; maskIndex++) {
-                if ((key[i] & PARITY_BIT_MASK[maskIndex])
-                    == PARITY_BIT_MASK[maskIndex]) {
-                    bitCount++;
-                }
-            }
-            if ((bitCount & 0x01) == 1) {
-                // Odd number of 1 bits in the top 7 bits. Set parity bit to 0
-                key[i] = (byte)(key[i] & (byte)0xfe);
-            } else {
-                // Even number of 1 bits in the top 7 bits. Set parity bit to 1
-                key[i] = (byte)(key[i] | 1);
-            }
+            int b = key[i] & 0xfe;
+            b |= (Integer.bitCount(b) & 1) ^ 1;
+            key[i] = (byte) b;
         }
     }
 
--- a/jdk/src/share/classes/sun/security/krb5/internal/crypto/dk/Des3DkCrypto.java	Tue May 19 16:33:32 2009 -0700
+++ b/jdk/src/share/classes/sun/security/krb5/internal/crypto/dk/Des3DkCrypto.java	Wed May 20 10:11:23 2009 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 2004-2009 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -169,33 +169,15 @@
         return result;
     }
 
-    /* Mask used to check for parity adjustment */
-    private static final byte[] PARITY_BIT_MASK = {
-        (byte)0x80, (byte)0x40, (byte)0x20, (byte)0x10,
-        (byte)0x08, (byte)0x04, (byte)0x02
-    };
-
     /**
      * Sets the parity bit (0th bit) in each byte so that each byte
      * contains an odd number of 1's.
      */
     private static void setParityBit(byte[] key) {
         for (int i = 0; i < key.length; i++) {
-            int bitCount = 0;
-            for (int maskIndex = 0;
-                 maskIndex < PARITY_BIT_MASK.length; maskIndex++) {
-                if ((key[i] & PARITY_BIT_MASK[maskIndex])
-                    == PARITY_BIT_MASK[maskIndex]) {
-                    bitCount++;
-                }
-            }
-            if ((bitCount & 0x01) == 1) {
-                // Odd number of 1 bits in the top 7 bits. Set parity bit to 0
-                key[i] = (byte)(key[i] & (byte)0xfe);
-            } else {
-                // Even number of 1 bits in the top 7 bits. Set parity bit to 1
-                key[i] = (byte)(key[i] | 1);
-            }
+            int b = key[i] & 0xfe;
+            b |= (Integer.bitCount(b) & 1) ^ 1;
+            key[i] = (byte) b;
         }
     }