summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2015-11-12 19:31:58 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2015-11-12 19:31:58 +0700
commit98d63d3099b874804e901b8e00288e899a370da5 (patch)
treea2f0e31f1534421683057c28413672442ccedf0c
parentUse culture-independent prefix-check in MacUtilities.GetMac (diff)
downloadBouncyCastle.NET-ed25519-98d63d3099b874804e901b8e00288e899a370da5.tar.xz
Refactoring of "unused bits" changes
-rw-r--r--crypto/src/asn1/DerBitString.cs36
1 files changed, 17 insertions, 19 deletions
diff --git a/crypto/src/asn1/DerBitString.cs b/crypto/src/asn1/DerBitString.cs
index ad7a7e349..fa37e6c0c 100644
--- a/crypto/src/asn1/DerBitString.cs
+++ b/crypto/src/asn1/DerBitString.cs
@@ -95,29 +95,29 @@ namespace Org.BouncyCastle.Asn1
 
             Debug.Assert(0 < bytes && bytes <= 4);
 
-            byte[] result = new byte[bytes];
+            byte[] data = new byte[bytes];
             --bytes;
 
             for (int i = 0; i < bytes; i++)
             {
-                result[i] = (byte)namedBits;
+                data[i] = (byte)namedBits;
                 namedBits >>= 8;
             }
 
             Debug.Assert((namedBits & 0xFF) != 0);
 
-            result[bytes] = (byte)namedBits;
+            data[bytes] = (byte)namedBits;
 
-            int pad = 0;
-            while ((namedBits & (1 << pad)) == 0)
+            int padBits = 0;
+            while ((namedBits & (1 << padBits)) == 0)
             {
-                ++pad;
+                ++padBits;
             }
 
-            Debug.Assert(pad < 8);
+            Debug.Assert(padBits < 8);
 
-            this.mData = result;
-            this.mPadBits = pad;
+            this.mData = data;
+            this.mPadBits = padBits;
         }
 
         public DerBitString(
@@ -187,20 +187,18 @@ namespace Org.BouncyCastle.Asn1
             {
                 int last = mData[mData.Length - 1];
                 int mask = (1 << mPadBits) - 1;
+                int unusedBits = last & mask;
 
-                if ((last & mask) != 0)
+                if (unusedBits != 0)
                 {
-                    byte[] result = Arrays.Prepend(mData, (byte)mPadBits);
+                    byte[] contents = Arrays.Prepend(mData, (byte)mPadBits);
 
                     /*
-                    * X.690-0207 11.2.1: Each unused bit in the final octet of the encoding of a bit string value shall be set to zero.
-                    * 
-                    * NOTE: 'pad' is constrained to be 0 if 'bytes' are empty, in which case this is a no-op. 
-                    */
-                    last ^= (last & mask);
-                    result[result.Length - 1] &= (byte)last;
-
-                    derOut.WriteEncoded(Asn1Tags.BitString, result);
+                     * X.690-0207 11.2.1: Each unused bit in the final octet of the encoding of a bit string value shall be set to zero.
+                     */
+                    contents[contents.Length - 1] = (byte)(last ^ unusedBits);
+
+                    derOut.WriteEncoded(Asn1Tags.BitString, contents);
                     return;
                 }
             }