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>2013-06-28 15:26:06 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2013-06-28 15:26:06 +0700
commit44288db4414158ac9b98a507b15e81d0d3c66ca6 (patch)
treeaa5ef88948ebb68ed6c8df81eb5da889641a9b50 /crypto/src/util/encoders/HexEncoder.cs
parentSet up text/binary handling for existing file types (diff)
downloadBouncyCastle.NET-ed25519-44288db4414158ac9b98a507b15e81d0d3c66ca6.tar.xz
Initial import of old CVS repository
Diffstat (limited to 'crypto/src/util/encoders/HexEncoder.cs')
-rw-r--r--crypto/src/util/encoders/HexEncoder.cs163
1 files changed, 163 insertions, 0 deletions
diff --git a/crypto/src/util/encoders/HexEncoder.cs b/crypto/src/util/encoders/HexEncoder.cs
new file mode 100644
index 000000000..e69a78b0b
--- /dev/null
+++ b/crypto/src/util/encoders/HexEncoder.cs
@@ -0,0 +1,163 @@
+using System;
+using System.IO;
+
+namespace Org.BouncyCastle.Utilities.Encoders
+{
+	public class HexEncoder
+		: IEncoder
+	{
+		private static readonly byte[] encodingTable =
+		{
+			(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
+			(byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
+		};
+
+        private static readonly byte[] decodingTable = ConstructDecodingTable(encodingTable);
+
+        private static byte[] ConstructDecodingTable(byte[] et)
+		{
+            byte[] dt = new byte[128];
+			for (int i = 0; i < et.Length; i++)
+			{
+				dt[et[i]] = (byte)i;
+			}
+
+			dt['A'] = dt['a'];
+			dt['B'] = dt['b'];
+			dt['C'] = dt['c'];
+			dt['D'] = dt['d'];
+			dt['E'] = dt['e'];
+			dt['F'] = dt['f'];
+
+            return dt;
+		}
+
+        /**
+		* 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)
+		{
+			for (int i = off; i < (off + length); i++)
+			{
+				int v = data[i];
+
+				outStream.WriteByte(encodingTable[v >> 4]);
+				outStream.WriteByte(encodingTable[v & 0xf]);
+			}
+
+			return length * 2;
+		}
+
+        private static bool Ignore(char c)
+        {
+            return c == '\n' || c =='\r' || c == '\t' || c == ' ';
+        }
+
+        /**
+		* decode the Hex encoded byte data writing it to the given output stream,
+		* whitespace characters will be ignored.
+		*
+		* @return the number of bytes produced.
+		*/
+		public int Decode(
+			byte[]	data,
+			int		off,
+			int		length,
+			Stream	outStream)
+		{
+			byte b1, b2;
+			int outLen = 0;
+			int end = off + length;
+
+			while (end > off)
+			{
+				if (!Ignore((char)data[end - 1]))
+				{
+					break;
+				}
+
+				end--;
+			}
+
+			int i = off;
+			while (i < end)
+			{
+				while (i < end && Ignore((char)data[i]))
+				{
+					i++;
+				}
+
+				b1 = decodingTable[data[i++]];
+
+				while (i < end && Ignore((char)data[i]))
+				{
+					i++;
+				}
+
+				b2 = decodingTable[data[i++]];
+
+				outStream.WriteByte((byte)((b1 << 4) | b2));
+
+				outLen++;
+			}
+
+			return outLen;
+		}
+
+		/**
+		* decode the Hex encoded string data writing it to the given output stream,
+		* whitespace characters will be ignored.
+		*
+		* @return the number of bytes produced.
+		*/
+		public int DecodeString(
+			string	data,
+			Stream	outStream)
+		{
+			byte    b1, b2;
+			int     length = 0;
+
+			int     end = data.Length;
+
+			while (end > 0)
+			{
+				if (!Ignore(data[end - 1]))
+				{
+					break;
+				}
+
+				end--;
+			}
+
+			int i = 0;
+			while (i < end)
+			{
+				while (i < end && Ignore(data[i]))
+				{
+					i++;
+				}
+
+				b1 = decodingTable[data[i++]];
+
+				while (i < end && Ignore(data[i]))
+				{
+					i++;
+				}
+
+				b2 = decodingTable[data[i++]];
+
+				outStream.WriteByte((byte)((b1 << 4) | b2));
+
+				length++;
+			}
+
+			return length;
+		}
+	}
+}