Exemplo n.º 1
0
		protected static void ConvertTest(BaseEncoding encoding)
		{
			var initialData = new Byte[128];

			var odd = false;

			for (var index = 0; index < UnitTestsIterationsCount; index++, odd = !odd)
			{
				// Generate test data
				Random.NextBytes(initialData);

				var offset = odd ? 0 : Random.Next(64);

				var count = odd ? 128 : Random.Next(64);

				// Convert byte array to string
				var intermediateResult = odd ? encoding.Encode(initialData) : encoding.Encode(initialData, offset, count);

				// Convert string to byte array
				var actualResult = encoding.Decode(intermediateResult, 0, intermediateResult.Length);

				// Compare by item
				for (Int32 expectedResultIndex = offset, actualResultIndex = 0; expectedResultIndex < offset + count; expectedResultIndex++, actualResultIndex++)
				{
					var expectedItem = initialData[expectedResultIndex];

					var actualItem = actualResult[actualResultIndex];

					Assert.AreEqual(expectedItem, actualItem);
				}
			}
		}
Exemplo n.º 2
0
        static Base64Encoding()
        {
            Blowfish = new BaseEncoding("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", true);

            UnixCrypt = new BaseEncoding("./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", true);

            UnixMD5 = new BaseEncoding("./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", false);
        }
Exemplo n.º 3
0
 // ========= USAGE 2 =========
 // char[] BaseEncoding.Encode(byte[] bytes, int offset, int length)
 // byte[] BaseEncoding.Decode(char[] chars, int offset, int length)
 static void Usage2(string testName, BaseEncoding encoding, string testVector)
 {
     byte[] origin    = Encoding.Default.GetBytes(testVector);
     char[] baseChars = encoding.Encode(origin, 0, origin.Length);
     byte[] bytes     = encoding.Decode(baseChars, 0, baseChars.Length);
     Console.WriteLine("[" + testName + "]\tVector: " + testVector + "\tBaseString: "
                       + new string(baseChars) + "\t" + (ArrayEquals(origin, bytes) ? "Success" : "failed"));
 }
Exemplo n.º 4
0
        static Base64Encoding()
        {
            Blowfish = new BaseEncoding("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", true);

            UnixCrypt = new BaseEncoding("./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", true);

            UnixMD5 = new BaseEncoding("./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", false);
        }
Exemplo n.º 5
0
        public static string GetUniqueID()
        {
            var guidBytes = new byte[9];

            System.Security.Cryptography.RandomNumberGenerator.Fill(guidBytes);
            var guid = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + "_" + BaseEncoding.ToBaseString(guidBytes, BaseEncoding.CHAR_SETS.BASE36); //HexUtil.GetHexFromBytes(guidBytes);

            return(guid);
        }
Exemplo n.º 6
0
        // ========= USAGE 1 =========
        // string BaseEncoding.ToBaseString(byte[] bytes)
        // byte[] BaseEncoding.FromBaseString(string s)
        static void Usage1(string testName, BaseEncoding encoding, string testVector)
        {
            byte[] origin     = Encoding.Default.GetBytes(testVector);
            string baseString = encoding.ToBaseString(origin);

            byte[] bytes = encoding.FromBaseString(baseString);
            Console.WriteLine("[" + testName + "]\tVector: " + testVector + "\tBaseString: "
                              + baseString + "\t" + (ArrayEquals(origin, bytes) ? "Success" : "failed"));
        }
Exemplo n.º 7
0
		protected static void TestDecodeMethodArguments(BaseEncoding encoding)
		{
			try // null
			{
				encoding.Decode(null);

				Assert.Fail(typeof(ArgumentNullException).ToString());
			}
			catch (ArgumentNullException)
			{
			}
		}
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            BaseEncoding b64 = BaseEncoding.Base64
            , b64Safe        = BaseEncoding.Base64Safe
            , b32            = BaseEncoding.Base32
            , b32Hex         = BaseEncoding.Base32Hex
            , b16            = BaseEncoding.Base16;

            foreach (string vector in TEST_VECTORS)
            {
                Usage1("Base64", b64, vector);
                Usage2("Base64Safe", b64Safe, vector);
                Usage1("Base32", b32, vector);
                Usage3("Base32Hex", b32Hex, vector);
                Usage2("Base16", b16, vector);
            }
            Console.ReadKey();
        }
Exemplo n.º 9
0
		protected static void TestDecodeOffsetCountMethodArguments(BaseEncoding encoding)
		{
			try // null
			{
				encoding.Decode(null, 0, 2);

				Assert.Fail(typeof(ArgumentNullException).ToString());
			}
			catch (ArgumentNullException)
			{
			}

			try // bad count
			{
				encoding.Decode("abcd", 0, -4);

				Assert.Fail(typeof(ArgumentOutOfRangeException).ToString());
			}
			catch (ArgumentOutOfRangeException)
			{
			}

			try // bad offset
			{
				encoding.Decode("abcd", -1, 4);

				Assert.Fail(typeof(ArgumentOutOfRangeException).ToString());
			}
			catch (ArgumentOutOfRangeException)
			{
			}

			try // bad offset
			{
				encoding.Decode("abcd", 1, 4);

				Assert.Fail(typeof(ArgumentOutOfRangeException).ToString());
			}
			catch (ArgumentOutOfRangeException)
			{
			}
		}
Exemplo n.º 10
0
        // ========= USAGE 3 =========
        // int BaseEncoding.GetEncodeCount(int length)
        // int BaseEncoding.Encode(byte[] bytesIn, int offsetIn, int lengthIn, char[] charsOut, int offsetOut)
        // int BaseEncoding.GetDecodeCount(char[] chars, int offset, int length)
        // int BaseEncoding.Decode(char[] charsIn, int offsetIn, int lengthIn, byte[] bytesOut, int offsetOut)
        static void Usage3(string testName, BaseEncoding encoding, string testVector)
        {
            int encodeOffset = 99, decodeOffset = 199;

            byte[] origin          = Encoding.Default.GetBytes(testVector);
            byte[] originBuffer    = WrapArray <byte>(origin, 33, 44);
            char[] baseCharsBuffer = new char[encoding.GetEncodeCount(origin.Length) + encodeOffset * 2];
            // == encode
            int encodeNum = encoding.Encode(originBuffer, 33, origin.Length, baseCharsBuffer, encodeOffset);

            // ==
            byte[] binBuffer = new byte[encoding.GetDecodeCount(baseCharsBuffer, encodeOffset, encodeNum) + decodeOffset * 2];
            // == decode
            int decodeNum = encoding.Decode(baseCharsBuffer, encodeOffset, encodeNum, binBuffer, decodeOffset);

            // ====== result
            char[] baseChars = SubArray <char>(baseCharsBuffer, encodeOffset, encodeNum);
            byte[] binData   = SubArray <byte>(binBuffer, decodeOffset, decodeNum);
            // == output
            Console.WriteLine("[" + testName + "]\tVector: " + testVector + "\tBaseString: "
                              + new string(baseChars) + "\t" + (ArrayEquals(origin, binData) ? "Success" : "failed"));
        }
Exemplo n.º 11
0
 static Base2Encoding()
 {
     Binary = new BaseEncoding("01", true);
 }
Exemplo n.º 12
0
 static Base16Encoding()
 {
     Hex = new BaseEncoding("0123456789ABCDEF", true, null, ch => char.ToUpperInvariant(ch));
 }
Exemplo n.º 13
0
		protected static void TestEncodeOffsetCountMethodArguments(BaseEncoding encoding)
		{
			var testData = new Byte[]
			{
				0, 1, 2, 3
			};

			try // null data
			{
				encoding.Encode(null, 0, 2);

				Assert.Fail(typeof(ArgumentNullException).ToString());
			}
			catch (ArgumentNullException)
			{
			}

			try // bad length
			{
				encoding.Encode(testData, 0, -4);

				Assert.Fail(typeof(ArgumentOutOfRangeException).ToString());
			}
			catch (ArgumentOutOfRangeException)
			{
			}

			try // bad offset
			{
				encoding.Encode(testData, -1, 4);

				Assert.Fail(typeof(ArgumentOutOfRangeException).ToString());
			}
			catch (ArgumentOutOfRangeException)
			{
			}

			try // bad offset
			{
				encoding.Encode(testData, 1, 4);

				Assert.Fail(typeof(ArgumentOutOfRangeException).ToString());
			}
			catch (ArgumentOutOfRangeException)
			{
			}

			try // good pass zero length
			{
				encoding.Encode(testData, 0, 0);
			}
			catch (Exception)
			{
				Assert.Fail();
			}
		}
Exemplo n.º 14
0
		protected static void TestEncodeMethodArguments(BaseEncoding encoding)
		{
			var testData = new Byte[]
			{
				0, 1, 2, 3
			};

			try // null data
			{
				encoding.Encode(null);

				Assert.Fail(typeof(ArgumentNullException).ToString());
			}
			catch (ArgumentNullException)
			{
			}

			try // good pass zero length
			{
				encoding.Encode(new Byte[0]);
			}
			catch (Exception)
			{
				Assert.Fail();
			}

			try // good pass
			{
				encoding.Encode(testData);
			}
			catch (Exception)
			{
				Assert.Fail();
			}
		}
Exemplo n.º 15
0
 static Base2Encoding()
 {
     Binary = new BaseEncoding("01", true);
 }
Exemplo n.º 16
0
 static Base16Encoding()
 {
     Hex = new BaseEncoding("0123456789ABCDEF", true, null, ch => char.ToUpperInvariant(ch));
 }