summary refs log tree commit diff
path: root/crypto/src/util/encoders/HexEncoder.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2019-09-09 15:52:36 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2019-09-09 15:52:36 +0700
commit71a58e93463e0a7e4f277069f9e133a98a9a8bcb (patch)
treeb14b3c28149ece39a59bf14d6f2d45bed7774dcd /crypto/src/util/encoders/HexEncoder.cs
parentAdd sanity checks on scalar mult. outputs (diff)
downloadBouncyCastle.NET-ed25519-71a58e93463e0a7e4f277069f9e133a98a9a8bcb.tar.xz
Port of strict hex decoding from bc-java
Diffstat (limited to '')
-rw-r--r--crypto/src/util/encoders/HexEncoder.cs26
1 files changed, 26 insertions, 0 deletions
diff --git a/crypto/src/util/encoders/HexEncoder.cs b/crypto/src/util/encoders/HexEncoder.cs
index af526e0da..950bc8477 100644
--- a/crypto/src/util/encoders/HexEncoder.cs
+++ b/crypto/src/util/encoders/HexEncoder.cs
@@ -172,5 +172,31 @@ namespace Org.BouncyCastle.Utilities.Encoders
 
             return length;
         }
+
+        internal byte[] DecodeStrict(string str, int off, int len)
+        {
+            if (null == str)
+                throw new ArgumentNullException("str");
+            if (off < 0 || len < 0 || off > (str.Length - len))
+                throw new IndexOutOfRangeException("invalid offset and/or length specified");
+            if (0 != (len & 1))
+                throw new ArgumentException("a hexadecimal encoding must have an even number of characters", "len");
+
+            int resultLen = len >> 1;
+            byte[] result = new byte[resultLen];
+
+            int strPos = off;
+            for (int i = 0; i < resultLen; ++i)
+            {
+                byte b1 = decodingTable[str[strPos++]];
+                byte b2 = decodingTable[str[strPos++]];
+
+                if ((b1 | b2) >= 0x80)
+                    throw new IOException("invalid characters encountered in Hex data");
+
+                result[i] = (byte)((b1 << 4) | b2);
+            }
+            return result;
+        }
     }
 }