6832016: {DigestMD5Base,Des3DkCrypto}.setParityBit should use Integer.bitCount

Reviewed-by: weijun
This commit is contained in:
Christian Thalinger 2009-05-20 10:11:23 +08:00 committed by Weijun Wang
parent 9a67d0776e
commit 2005a31e5d
2 changed files with 8 additions and 43 deletions

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -1516,11 +1516,6 @@ abstract class DigestMD5Base extends AbstractSaslImpl {
// ---------------- DES and 3 DES key manipulation routines // ---------------- 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); private static final BigInteger MASK = new BigInteger("7f", 16);
/** /**
@ -1529,21 +1524,9 @@ abstract class DigestMD5Base extends AbstractSaslImpl {
*/ */
private static void setParityBit(byte[] key) { private static void setParityBit(byte[] key) {
for (int i = 0; i < key.length; i++) { for (int i = 0; i < key.length; i++) {
int bitCount = 0; int b = key[i] & 0xfe;
for (int maskIndex = 0; b |= (Integer.bitCount(b) & 1) ^ 1;
maskIndex < PARITY_BIT_MASK.length; maskIndex++) { key[i] = (byte) b;
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);
}
} }
} }

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -169,33 +169,15 @@ public class Des3DkCrypto extends DkCrypto {
return result; 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 * Sets the parity bit (0th bit) in each byte so that each byte
* contains an odd number of 1's. * contains an odd number of 1's.
*/ */
private static void setParityBit(byte[] key) { private static void setParityBit(byte[] key) {
for (int i = 0; i < key.length; i++) { for (int i = 0; i < key.length; i++) {
int bitCount = 0; int b = key[i] & 0xfe;
for (int maskIndex = 0; b |= (Integer.bitCount(b) & 1) ^ 1;
maskIndex < PARITY_BIT_MASK.length; maskIndex++) { key[i] = (byte) b;
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);
}
} }
} }