summary refs log tree commit diff
path: root/crypto/src/bcpg/sig
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-10-27 19:33:38 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-10-27 19:33:38 +0700
commit0f5e5e54e55c23222f6f934f2c32f212f13a34ae (patch)
treeea7a9def6ee86a123457912c6866e3c95b3bb679 /crypto/src/bcpg/sig
parentPqc.Crypto.Bike cleanup (diff)
downloadBouncyCastle.NET-ed25519-0f5e5e54e55c23222f6f934f2c32f212f13a34ae.tar.xz
Refactoring in Bcpg.Sig
Diffstat (limited to 'crypto/src/bcpg/sig')
-rw-r--r--crypto/src/bcpg/sig/Exportable.cs12
-rw-r--r--crypto/src/bcpg/sig/Features.cs18
-rw-r--r--crypto/src/bcpg/sig/IssuerKeyId.cs37
-rw-r--r--crypto/src/bcpg/sig/KeyExpirationTime.cs25
-rw-r--r--crypto/src/bcpg/sig/NotationData.cs2
-rw-r--r--crypto/src/bcpg/sig/PrimaryUserId.cs15
-rw-r--r--crypto/src/bcpg/sig/Revocable.cs15
-rw-r--r--crypto/src/bcpg/sig/RevocationReason.cs2
-rw-r--r--crypto/src/bcpg/sig/SignatureCreationTime.cs27
-rw-r--r--crypto/src/bcpg/sig/SignatureExpirationTime.cs32
-rw-r--r--crypto/src/bcpg/sig/SignerUserId.cs4
11 files changed, 32 insertions, 157 deletions
diff --git a/crypto/src/bcpg/sig/Exportable.cs b/crypto/src/bcpg/sig/Exportable.cs
index 4d030346f..8a9eafe09 100644
--- a/crypto/src/bcpg/sig/Exportable.cs
+++ b/crypto/src/bcpg/sig/Exportable.cs
@@ -10,17 +10,7 @@ namespace Org.BouncyCastle.Bcpg.Sig
     {
         private static byte[] BooleanToByteArray(bool val)
         {
-            byte[]    data = new byte[1];
-
-            if (val)
-            {
-                data[0] = 1;
-                return data;
-            }
-            else
-            {
-                return data;
-            }
+            return new byte[1]{ Convert.ToByte(val) };
         }
 
         public Exportable(
diff --git a/crypto/src/bcpg/sig/Features.cs b/crypto/src/bcpg/sig/Features.cs
index 135d7f54e..f6123d612 100644
--- a/crypto/src/bcpg/sig/Features.cs
+++ b/crypto/src/bcpg/sig/Features.cs
@@ -19,31 +19,25 @@ namespace Org.BouncyCastle.Bcpg.Sig
            fingerprint format */
         public static readonly byte FEATURE_VERSION_5_PUBLIC_KEY = 0x04;
 
-        private static byte[] featureToByteArray(byte feature)
+        private static byte[] FeatureToByteArray(byte feature)
         {
-            byte[] data = new byte[1];
-            data[0] = feature;
-            return data;
+            return new byte[1]{ feature };
         }
 
         public Features(
             bool critical,
             bool isLongLength,
-            byte[] data): base(SignatureSubpacketTag.Features, critical, isLongLength, data)
+            byte[] data)
+            : base(SignatureSubpacketTag.Features, critical, isLongLength, data)
         {
-
         }
-      
 
-        public Features(bool critical, byte features): this(critical, false, featureToByteArray(features))
+        public Features(bool critical, byte features): this(critical, false, FeatureToByteArray(features))
         {
-
         }
-   
 
-        public Features(bool critical, int features):  this(critical, false, featureToByteArray((byte)features))
+        public Features(bool critical, int features):  this(critical, false, FeatureToByteArray((byte)features))
         {
-           
         }
 
         /**
diff --git a/crypto/src/bcpg/sig/IssuerKeyId.cs b/crypto/src/bcpg/sig/IssuerKeyId.cs
index 627ea3ecf..1281a110e 100644
--- a/crypto/src/bcpg/sig/IssuerKeyId.cs
+++ b/crypto/src/bcpg/sig/IssuerKeyId.cs
@@ -1,6 +1,4 @@
-using System;
-
-
+using Org.BouncyCastle.Crypto.Utilities;
 
 namespace Org.BouncyCastle.Bcpg.Sig
 {
@@ -10,21 +8,9 @@ namespace Org.BouncyCastle.Bcpg.Sig
     public class IssuerKeyId
         : SignatureSubpacket
     {
-        protected static byte[] KeyIdToBytes(
-            long    keyId)
+        protected static byte[] KeyIdToBytes(long keyId)
         {
-            byte[]    data = new byte[8];
-
-            data[0] = (byte)(keyId >> 56);
-            data[1] = (byte)(keyId >> 48);
-            data[2] = (byte)(keyId >> 40);
-            data[3] = (byte)(keyId >> 32);
-            data[4] = (byte)(keyId >> 24);
-            data[5] = (byte)(keyId >> 16);
-            data[6] = (byte)(keyId >> 8);
-            data[7] = (byte)keyId;
-
-            return data;
+            return Pack.UInt64_To_BE((ulong)keyId);
         }
 
         public IssuerKeyId(
@@ -42,21 +28,6 @@ namespace Org.BouncyCastle.Bcpg.Sig
         {
         }
 
-        public long KeyId
-        {
-			get
-			{
-				long keyId = ((long)(data[0] & 0xff) << 56)
-					| ((long)(data[1] & 0xff) << 48)
-					| ((long)(data[2] & 0xff) << 40)
-					| ((long)(data[3] & 0xff) << 32)
-					| ((long)(data[4] & 0xff) << 24)
-					| ((long)(data[5] & 0xff) << 16)
-					| ((long)(data[6] & 0xff) << 8)
-					| ((long)data[7] & 0xff);
-
-				return keyId;
-			}
-        }
+        public long KeyId => (long)Pack.BE_To_UInt64(data);
     }
 }
diff --git a/crypto/src/bcpg/sig/KeyExpirationTime.cs b/crypto/src/bcpg/sig/KeyExpirationTime.cs
index dfd3e76fd..62184b2a1 100644
--- a/crypto/src/bcpg/sig/KeyExpirationTime.cs
+++ b/crypto/src/bcpg/sig/KeyExpirationTime.cs
@@ -1,4 +1,4 @@
-using System;
+using Org.BouncyCastle.Crypto.Utilities;
 
 namespace Org.BouncyCastle.Bcpg.Sig
 {
@@ -8,17 +8,9 @@ namespace Org.BouncyCastle.Bcpg.Sig
     public class KeyExpirationTime
         : SignatureSubpacket
     {
-        protected static byte[] TimeToBytes(
-            long    t)
+        protected static byte[] TimeToBytes(long t)
         {
-            byte[]    data = new byte[4];
-
-            data[0] = (byte)(t >> 24);
-            data[1] = (byte)(t >> 16);
-            data[2] = (byte)(t >> 8);
-            data[3] = (byte)t;
-
-            return data;
+            return Pack.UInt32_To_BE((uint)t);
         }
 
         public KeyExpirationTime(
@@ -41,15 +33,6 @@ namespace Org.BouncyCastle.Bcpg.Sig
         *
         * @return second count for key validity.
         */
-        public long Time
-        {
-			get
-			{
-				long time = ((long)(data[0] & 0xff) << 24) | ((long)(data[1] & 0xff) << 16)
-					| ((long)(data[2] & 0xff) << 8) | ((long)data[3] & 0xff);
-
-				return time;
-			}
-        }
+        public long Time => (long)Pack.BE_To_UInt32(data);
     }
 }
diff --git a/crypto/src/bcpg/sig/NotationData.cs b/crypto/src/bcpg/sig/NotationData.cs
index 9ac6f89cf..71c34d3de 100644
--- a/crypto/src/bcpg/sig/NotationData.cs
+++ b/crypto/src/bcpg/sig/NotationData.cs
@@ -79,7 +79,7 @@ namespace Org.BouncyCastle.Bcpg.Sig
 
 		public bool IsHumanReadable
 		{
-			get { return data[0] == (byte)0x80; }
+			get { return data[0] == 0x80; }
 		}
 
 		public string GetNotationName()
diff --git a/crypto/src/bcpg/sig/PrimaryUserId.cs b/crypto/src/bcpg/sig/PrimaryUserId.cs
index 1f16f40eb..184dfe8f7 100644
--- a/crypto/src/bcpg/sig/PrimaryUserId.cs
+++ b/crypto/src/bcpg/sig/PrimaryUserId.cs
@@ -8,20 +8,9 @@ namespace Org.BouncyCastle.Bcpg.Sig
     public class PrimaryUserId
         : SignatureSubpacket
     {
-        private static byte[] BooleanToByteArray(
-            bool    val)
+        private static byte[] BooleanToByteArray(bool val)
         {
-            byte[]    data = new byte[1];
-
-            if (val)
-            {
-                data[0] = 1;
-                return data;
-            }
-            else
-            {
-                return data;
-            }
+            return new byte[1]{ Convert.ToByte(val) };
         }
 
         public PrimaryUserId(
diff --git a/crypto/src/bcpg/sig/Revocable.cs b/crypto/src/bcpg/sig/Revocable.cs
index 7aa91391f..6ded4d865 100644
--- a/crypto/src/bcpg/sig/Revocable.cs
+++ b/crypto/src/bcpg/sig/Revocable.cs
@@ -8,20 +8,9 @@ namespace Org.BouncyCastle.Bcpg.Sig
     public class Revocable
         : SignatureSubpacket
     {
-        private static byte[] BooleanToByteArray(
-            bool    value)
+        private static byte[] BooleanToByteArray(bool value)
         {
-            byte[]    data = new byte[1];
-
-            if (value)
-            {
-                data[0] = 1;
-                return data;
-            }
-            else
-            {
-                return data;
-            }
+            return new byte[1]{ Convert.ToByte(value) };
         }
 
         public Revocable(
diff --git a/crypto/src/bcpg/sig/RevocationReason.cs b/crypto/src/bcpg/sig/RevocationReason.cs
index 42afd5f5b..bd0c91428 100644
--- a/crypto/src/bcpg/sig/RevocationReason.cs
+++ b/crypto/src/bcpg/sig/RevocationReason.cs
@@ -46,9 +46,7 @@ namespace Org.BouncyCastle.Bcpg
         {
             byte[] data = GetData();
             if (data.Length == 1)
-            {
                 return string.Empty;
-            }
 
             byte[] description = new byte[data.Length - 1];
             Array.Copy(data, 1, description, 0, description.Length);
diff --git a/crypto/src/bcpg/sig/SignatureCreationTime.cs b/crypto/src/bcpg/sig/SignatureCreationTime.cs
index d172e5d52..233dd18e6 100644
--- a/crypto/src/bcpg/sig/SignatureCreationTime.cs
+++ b/crypto/src/bcpg/sig/SignatureCreationTime.cs
@@ -1,5 +1,6 @@
 using System;
 
+using Org.BouncyCastle.Crypto.Utilities;
 using Org.BouncyCastle.Utilities.Date;
 
 namespace Org.BouncyCastle.Bcpg.Sig
@@ -10,41 +11,25 @@ namespace Org.BouncyCastle.Bcpg.Sig
     public class SignatureCreationTime
         : SignatureSubpacket
     {
-		protected static byte[] TimeToBytes(
-            DateTime time)
+		protected static byte[] TimeToBytes(DateTime time)
         {
 			long t = DateTimeUtilities.DateTimeToUnixMs(time) / 1000L;
-			byte[] data = new byte[4];
-			data[0] = (byte)(t >> 24);
-            data[1] = (byte)(t >> 16);
-            data[2] = (byte)(t >> 8);
-            data[3] = (byte)t;
-            return data;
+            return Pack.UInt32_To_BE((uint)t);
         }
 
-        public SignatureCreationTime(
-            bool    critical,
-            bool    isLongLength,
-            byte[]  data)
+        public SignatureCreationTime(bool critical, bool isLongLength, byte[] data)
             : base(SignatureSubpacketTag.CreationTime, critical, isLongLength, data)
         {
         }
 
-        public SignatureCreationTime(
-            bool        critical,
-            DateTime    date)
+        public SignatureCreationTime(bool critical, DateTime date)
             : base(SignatureSubpacketTag.CreationTime, critical, false, TimeToBytes(date))
         {
         }
 
         public DateTime GetTime()
         {
-			long time = (long)(
-					((uint)data[0] << 24)
-				|	((uint)data[1] << 16)
-				|	((uint)data[2] << 8)
-				|	((uint)data[3])
-				);
+            uint time = Pack.BE_To_UInt32(data, 0);
 			return DateTimeUtilities.UnixMsToDateTime(time * 1000L);
         }
     }
diff --git a/crypto/src/bcpg/sig/SignatureExpirationTime.cs b/crypto/src/bcpg/sig/SignatureExpirationTime.cs
index 24f0a9f8a..44c714f33 100644
--- a/crypto/src/bcpg/sig/SignatureExpirationTime.cs
+++ b/crypto/src/bcpg/sig/SignatureExpirationTime.cs
@@ -1,4 +1,4 @@
-using System;
+using Org.BouncyCastle.Crypto.Utilities;
 
 namespace Org.BouncyCastle.Bcpg.Sig
 {
@@ -8,28 +8,17 @@ namespace Org.BouncyCastle.Bcpg.Sig
     public class SignatureExpirationTime
         : SignatureSubpacket
     {
-        protected static byte[] TimeToBytes(
-            long    t)
+        protected static byte[] TimeToBytes(long t)
         {
-            byte[] data = new byte[4];
-            data[0] = (byte)(t >> 24);
-            data[1] = (byte)(t >> 16);
-            data[2] = (byte)(t >> 8);
-            data[3] = (byte)t;
-            return data;
+            return Pack.UInt32_To_BE((uint)t);
         }
 
-        public SignatureExpirationTime(
-            bool    critical,
-            bool    isLongLength,
-            byte[]  data)
+        public SignatureExpirationTime(bool critical, bool isLongLength, byte[] data)
             : base(SignatureSubpacketTag.ExpireTime, critical, isLongLength, data)
         {
         }
 
-        public SignatureExpirationTime(
-            bool    critical,
-            long    seconds)
+        public SignatureExpirationTime(bool critical, long seconds)
             : base(SignatureSubpacketTag.ExpireTime, critical, false, TimeToBytes(seconds))
         {
         }
@@ -37,15 +26,6 @@ namespace Org.BouncyCastle.Bcpg.Sig
         /**
         * return time in seconds before signature expires after creation time.
         */
-        public long Time
-        {
-            get
-            {
-                long time = ((long)(data[0] & 0xff) << 24) | ((long)(data[1] & 0xff) << 16)
-                    | ((long)(data[2] & 0xff) << 8) | ((long)data[3] & 0xff);
-
-                return time;
-            }
-        }
+        public long Time => Pack.BE_To_UInt32(data, 0);
     }
 }
diff --git a/crypto/src/bcpg/sig/SignerUserId.cs b/crypto/src/bcpg/sig/SignerUserId.cs
index 8ab62ed2e..6f812e210 100644
--- a/crypto/src/bcpg/sig/SignerUserId.cs
+++ b/crypto/src/bcpg/sig/SignerUserId.cs
@@ -1,7 +1,3 @@
-using System;
-
-
-
 namespace Org.BouncyCastle.Bcpg.Sig
 {
     /**