summary refs log tree commit diff
path: root/crypto/src/asn1/DerInteger.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-06-20 20:39:59 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-06-20 20:39:59 +0700
commit37a36810c786707b204108095c8f23214c73df70 (patch)
tree98a6346c291cde2638cf222bc551174ef245f609 /crypto/src/asn1/DerInteger.cs
parentFix tag for unprotectedAttrs field of Asn1.Cms.EncryptedData (diff)
downloadBouncyCastle.NET-ed25519-37a36810c786707b204108095c8f23214c73df70.tar.xz
Add Try... methods to DerInteger for small value accessors
Diffstat (limited to '')
-rw-r--r--crypto/src/asn1/DerInteger.cs39
1 files changed, 39 insertions, 0 deletions
diff --git a/crypto/src/asn1/DerInteger.cs b/crypto/src/asn1/DerInteger.cs
index d68a71e9d..666723527 100644
--- a/crypto/src/asn1/DerInteger.cs
+++ b/crypto/src/asn1/DerInteger.cs
@@ -209,6 +209,45 @@ namespace Org.BouncyCastle.Asn1
             }
         }
 
+        public bool TryGetIntPositiveValueExact(out int value)
+        {
+            int count = bytes.Length - start;
+            if (count > 4 || (count == 4 && 0 != (bytes[start] & 0x80)))
+            {
+                value = default;
+                return false;
+            }
+
+            value = IntValue(bytes, start, SignExtUnsigned);
+            return true;
+        }
+
+        public bool TryGetIntValueExact(out int value)
+        {
+            int count = bytes.Length - start;
+            if (count > 4)
+            {
+                value = default;
+                return false;
+            }
+
+            value = IntValue(bytes, start, SignExtSigned);
+            return true;
+        }
+
+        public bool TryGetLongValueExact(out long value)
+        {
+            int count = bytes.Length - start;
+            if (count > 8)
+            {
+                value = default;
+                return false;
+            }
+
+            value = LongValue(bytes, start, SignExtSigned);
+            return true;
+        }
+
         internal override IAsn1Encoding GetEncoding(int encoding)
         {
             return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.Integer, bytes);