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>2020-07-30 18:54:46 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2020-07-30 18:54:46 +0700
commit6df85c63349e5a0ddc9187de8779443fa8b55963 (patch)
treec3cca3d575f452ae9d96cdf8880a9610e1af4866 /crypto/src/util/encoders/HexEncoder.cs
parentAdd generic private key output to openssl (diff)
downloadBouncyCastle.NET-ed25519-6df85c63349e5a0ddc9187de8779443fa8b55963.tar.xz
Encoder performance
Diffstat (limited to '')
-rw-r--r--crypto/src/util/encoders/HexEncoder.cs75
1 files changed, 57 insertions, 18 deletions
diff --git a/crypto/src/util/encoders/HexEncoder.cs b/crypto/src/util/encoders/HexEncoder.cs
index 950bc8477..090c9d7e3 100644
--- a/crypto/src/util/encoders/HexEncoder.cs
+++ b/crypto/src/util/encoders/HexEncoder.cs
@@ -39,26 +39,40 @@ namespace Org.BouncyCastle.Utilities.Encoders
             InitialiseDecodingTable();
         }
 
+        public int Encode(byte[] inBuf, int inOff, int inLen, byte[] outBuf, int outOff)
+        {
+            int inPos = inOff;
+            int inEnd = inOff + inLen;
+            int outPos = outOff;
+
+            while (inPos < inEnd)
+            {
+                uint b = inBuf[inPos++];
+
+                outBuf[outPos++] = encodingTable[b >> 4];
+                outBuf[outPos++] = encodingTable[b & 0xF];
+            }
+
+            return outPos - outOff;
+        }
+
         /**
         * encode the input data producing a Hex output stream.
         *
         * @return the number of bytes produced.
         */
-        public int Encode(
-            byte[]	data,
-            int		off,
-            int		length,
-            Stream	outStream)
+        public int Encode(byte[] buf, int off, int len, Stream outStream)
         {
-            for (int i = off; i < (off + length); i++)
+            byte[] tmp = new byte[72];
+            while (len > 0)
             {
-                int v = data[i];
-
-                outStream.WriteByte(encodingTable[v >> 4]);
-                outStream.WriteByte(encodingTable[v & 0xf]);
+                int inLen = System.Math.Min(36, len);
+                int outLen = Encode(buf, off, inLen, tmp, 0);
+                outStream.Write(tmp, 0, outLen);
+                off += inLen;
+                len -= inLen;
             }
-
-            return length * 2;
+            return len * 2;
         }
 
         private static bool Ignore(char c)
@@ -80,6 +94,8 @@ namespace Org.BouncyCastle.Utilities.Encoders
         {
             byte b1, b2;
             int outLen = 0;
+            byte[] buf = new byte[36];
+            int bufOff = 0;
             int end = off + length;
 
             while (end > off)
@@ -112,11 +128,22 @@ namespace Org.BouncyCastle.Utilities.Encoders
                 if ((b1 | b2) >= 0x80)
                     throw new IOException("invalid characters encountered in Hex data");
 
-                outStream.WriteByte((byte)((b1 << 4) | b2));
+                buf[bufOff++] = (byte)((b1 << 4) | b2);
+
+                if (bufOff == buf.Length)
+                {
+                    outStream.Write(buf, 0, bufOff);
+                    bufOff = 0;
+                }
 
                 outLen++;
             }
 
+            if (bufOff > 0)
+            {
+                outStream.Write(buf, 0, bufOff);
+            }
+
             return outLen;
         }
 
@@ -130,10 +157,11 @@ namespace Org.BouncyCastle.Utilities.Encoders
             string	data,
             Stream	outStream)
         {
-            byte    b1, b2;
-            int     length = 0;
-
-            int     end = data.Length;
+            byte b1, b2;
+            int length = 0;
+            byte[] buf = new byte[36];
+            int bufOff = 0;
+            int end = data.Length;
 
             while (end > 0)
             {
@@ -165,11 +193,22 @@ namespace Org.BouncyCastle.Utilities.Encoders
                 if ((b1 | b2) >= 0x80)
                     throw new IOException("invalid characters encountered in Hex data");
 
-                outStream.WriteByte((byte)((b1 << 4) | b2));
+                buf[bufOff++] = (byte)((b1 << 4) | b2);
+
+                if (bufOff == buf.Length)
+                {
+                    outStream.Write(buf, 0, bufOff);
+                    bufOff = 0;
+                }
 
                 length++;
             }
 
+            if (bufOff > 0)
+            {
+                outStream.Write(buf, 0, bufOff);
+            }
+
             return length;
         }