summary refs log tree commit diff
path: root/crypto/src/asn1/x509/qualified/BiometricData.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-07-01 21:32:14 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-07-01 21:32:14 +0700
commit3c9d8a7198b04fa80f034c372fba4eea65c31e3b (patch)
treee1ee1e6c0a747dc4025acb490fd3cf9f2cde9b12 /crypto/src/asn1/x509/qualified/BiometricData.cs
parentAsn1Sequence subclass static methods (diff)
downloadBouncyCastle.NET-ed25519-3c9d8a7198b04fa80f034c372fba4eea65c31e3b.tar.xz
Refactoring in Asn1.X509.Qualified
Diffstat (limited to 'crypto/src/asn1/x509/qualified/BiometricData.cs')
-rw-r--r--crypto/src/asn1/x509/qualified/BiometricData.cs105
1 files changed, 43 insertions, 62 deletions
diff --git a/crypto/src/asn1/x509/qualified/BiometricData.cs b/crypto/src/asn1/x509/qualified/BiometricData.cs
index 816ffd26d..c2ede413d 100644
--- a/crypto/src/asn1/x509/qualified/BiometricData.cs
+++ b/crypto/src/asn1/x509/qualified/BiometricData.cs
@@ -17,88 +17,69 @@ namespace Org.BouncyCastle.Asn1.X509.Qualified
     public class BiometricData
         : Asn1Encodable
     {
-        private readonly TypeOfBiometricData typeOfBiometricData;
-        private readonly AlgorithmIdentifier hashAlgorithm;
-        private readonly Asn1OctetString     biometricDataHash;
-        private readonly DerIA5String        sourceDataUri;
-
-        public static BiometricData GetInstance(
-            object obj)
+        public static BiometricData GetInstance(object obj)
         {
-            if (obj == null || obj is BiometricData)
-            {
-                return (BiometricData)obj;
-            }
+            if (obj == null)
+                return null;
+            if (obj is BiometricData biometricData)
+                return biometricData;
+            return new BiometricData(Asn1Sequence.GetInstance(obj));
+        }
+
+        public static BiometricData GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new BiometricData(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
 
-            if (obj is Asn1Sequence)
-            {
-				return new BiometricData(Asn1Sequence.GetInstance(obj));
-            }
+        public static BiometricData GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new BiometricData(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));
 
-			throw new ArgumentException("unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
-		}
+        private readonly TypeOfBiometricData m_typeOfBiometricData;
+        private readonly AlgorithmIdentifier m_hashAlgorithm;
+        private readonly Asn1OctetString m_biometricDataHash;
+        private readonly DerIA5String m_sourceDataUri;
 
-		private BiometricData(
-			Asn1Sequence seq)
+        private BiometricData(Asn1Sequence seq)
         {
-			typeOfBiometricData = TypeOfBiometricData.GetInstance(seq[0]);
-			hashAlgorithm = AlgorithmIdentifier.GetInstance(seq[1]);
-			biometricDataHash = Asn1OctetString.GetInstance(seq[2]);
+            int count = seq.Count, pos = 0;
+            if (count < 3 || count > 4)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-			if (seq.Count > 3)
-			{
-				sourceDataUri = DerIA5String.GetInstance(seq[3]);
-			}
-        }
+            m_typeOfBiometricData = TypeOfBiometricData.GetInstance(seq[pos++]);
+			m_hashAlgorithm = AlgorithmIdentifier.GetInstance(seq[pos++]);
+			m_biometricDataHash = Asn1OctetString.GetInstance(seq[pos++]);
+            m_sourceDataUri = Asn1Utilities.ReadOptional(seq, ref pos, DerIA5String.GetOptional);
 
-		public BiometricData(
-            TypeOfBiometricData	typeOfBiometricData,
-            AlgorithmIdentifier	hashAlgorithm,
-            Asn1OctetString		biometricDataHash,
-            DerIA5String		sourceDataUri)
-        {
-            this.typeOfBiometricData = typeOfBiometricData;
-            this.hashAlgorithm = hashAlgorithm;
-            this.biometricDataHash = biometricDataHash;
-            this.sourceDataUri = sourceDataUri;
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
         }
 
-        public BiometricData(
-            TypeOfBiometricData	typeOfBiometricData,
-            AlgorithmIdentifier	hashAlgorithm,
-            Asn1OctetString		biometricDataHash)
+        public BiometricData(TypeOfBiometricData typeOfBiometricData, AlgorithmIdentifier hashAlgorithm,
+            Asn1OctetString biometricDataHash)
+            : this(typeOfBiometricData, hashAlgorithm, biometricDataHash, null)
         {
-            this.typeOfBiometricData = typeOfBiometricData;
-            this.hashAlgorithm = hashAlgorithm;
-            this.biometricDataHash = biometricDataHash;
-            this.sourceDataUri = null;
         }
 
-        public TypeOfBiometricData TypeOfBiometricData
+        public BiometricData(TypeOfBiometricData typeOfBiometricData, AlgorithmIdentifier hashAlgorithm,
+            Asn1OctetString biometricDataHash, DerIA5String sourceDataUri)
         {
-			get { return typeOfBiometricData; }
+            m_typeOfBiometricData = typeOfBiometricData ?? throw new ArgumentNullException(nameof(typeOfBiometricData));
+            m_hashAlgorithm = hashAlgorithm ?? throw new ArgumentNullException(nameof(hashAlgorithm));
+            m_biometricDataHash = biometricDataHash ?? throw new ArgumentNullException(nameof(biometricDataHash));
+            m_sourceDataUri = sourceDataUri;
         }
 
-		public AlgorithmIdentifier HashAlgorithm
-		{
-			get { return hashAlgorithm; }
-		}
+        public TypeOfBiometricData TypeOfBiometricData => m_typeOfBiometricData;
+
+        public AlgorithmIdentifier HashAlgorithm => m_hashAlgorithm;
 
-		public Asn1OctetString BiometricDataHash
-		{
-			get { return biometricDataHash; }
-		}
+        public Asn1OctetString BiometricDataHash => m_biometricDataHash;
 
-		public DerIA5String SourceDataUri
-		{
-			get { return sourceDataUri; }
-		}
+        public DerIA5String SourceDataUri => m_sourceDataUri;
 
         public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector(typeOfBiometricData, hashAlgorithm, biometricDataHash);
-            v.AddOptional(sourceDataUri);
-            return new DerSequence(v);
+            return m_sourceDataUri == null
+                ?  new DerSequence(m_typeOfBiometricData, m_hashAlgorithm, m_biometricDataHash)
+                :  new DerSequence(m_typeOfBiometricData, m_hashAlgorithm, m_biometricDataHash, m_sourceDataUri);
         }
     }
 }