Exemplo n.º 1
2
		public void TestCase()
		{
			using (MessageDigestContext ctx = new MessageDigestContext(MessageDigest.SHA1)) {
				for (int i = 0; i < tests.Length; i++) {
					byte[] msg = Encoding.ASCII.GetBytes(this.tests[i]);
					byte[] ret = ctx.Digest(msg);

					string str = BitConverter.ToString(ret);
					Assert.AreEqual(results[i], str);
				}

				byte[] buf = Encoding.ASCII.GetBytes(new string('a', 1000));
				ctx.Init();
				for (int i = 0; i < 1000; i++) {
					ctx.Update(buf);
				}

				byte[] retx = ctx.DigestFinal();
				string strx = BitConverter.ToString(retx);
				Assert.AreEqual(bigret, strx);
			}
		}
Exemplo n.º 2
0
		public void Execute(string[] args)
		{
			using (MessageDigestContext ctx = new MessageDigestContext(MessageDigest.SHA))
			{

				for (int i = 0; i < tests.Length; i++)
				{
					byte[] msg = Encoding.ASCII.GetBytes(this.tests[i]);
					byte[] ret = ctx.Digest(msg);

					string str = BitConverter.ToString(ret);
					if (str != this.results[i])
					{
						Console.WriteLine("error calculating SHA on {0}", this.tests[i]);
						Console.WriteLine("got {0} instead of {1}", str, this.results[i]);
					}
					else
						Console.WriteLine("test {0} ok", i);
				}

				byte[] buf = Encoding.ASCII.GetBytes(new string('a', 1000));
				ctx.Init();
				for (int i = 0; i < 1000; i++)
				{
					ctx.Update(buf);
				}

				byte[] retx = ctx.DigestFinal();
				string strx = BitConverter.ToString(retx);
				if (strx != bigret)
				{
					Console.WriteLine("error calculating SHA 'a' * 1000");
					Console.WriteLine("got {0} instead of {1}", strx, bigret);
				}
				else
					Console.WriteLine("test 3 ok");
			}
		}
Exemplo n.º 3
0
		private void GenericTest(string name, MessageDigestContext ctx, byte[][] results, int alen)
		{
			Console.WriteLine("Testing {0}", name);

			byte[] digest = ctx.Digest(Encoding.ASCII.GetBytes("abc"));
			string str1 = BitConverter.ToString(digest);
			string str2 = BitConverter.ToString(results[0]);
			Assert.AreEqual(str2, str1);

			Console.Write(".");

			byte[] msg = Encoding.ASCII.GetBytes(
				"abcdefgh" + "bcdefghi" + "cdefghij" + "defghijk" +
				"efghijkl" + "fghijklm" + "ghijklmn" + "hijklmno" +
				"ijklmnop" + "jklmnopq" + "klmnopqr" + "lmnopqrs" +
				"mnopqrst" + "nopqrstu");
			digest = ctx.Digest(msg);
			str1 = BitConverter.ToString(digest);
			str2 = BitConverter.ToString(results[1]);
			Assert.AreEqual(str2, str1);

			Console.Write(".");

			ctx.Init();
			for (int i = 0; i < 1000000; i += alen)
			{
				msg = Encoding.ASCII.GetBytes(new string('a', alen));
				int len = (1000000 - i) < alen ? 1000000 - i : alen;
				byte[] tmp = new byte[len];
				Buffer.BlockCopy(msg, 0, tmp, 0, len);
				ctx.Update(tmp);
			}
			digest = ctx.DigestFinal();

			str1 = BitConverter.ToString(digest);
			str2 = BitConverter.ToString(results[2]);
			Assert.AreEqual(str2, str1);

			Console.Write(".");

			Console.WriteLine(" passed.");
		}
Exemplo n.º 4
0
		private void GenericTest(string name, MessageDigestContext ctx, byte[][] results)
		{
			Console.WriteLine("Testing {0}", name);

			byte[] digest = ctx.Digest(Encoding.ASCII.GetBytes("abc"));
			string str1 = BitConverter.ToString(digest);
			string str2 = BitConverter.ToString(results[0]);
			Assert.AreEqual(str2, str1);

			Console.Write(".");

			byte[] msg = Encoding.ASCII.GetBytes(
				"abcdbcde" + "cdefdefg" + "efghfghi" +
				"ghijhijk" + "ijkljklm" + "klmnlmno" + "mnopnopq");
			digest = ctx.Digest(msg);
			str1 = BitConverter.ToString(digest);
			str2 = BitConverter.ToString(results[1]);
			Assert.AreEqual(str2, str1);

			Console.Write(".");

			ctx.Init();
			for (int i = 0; i < 1000000; i += 160)
			{
				msg = Encoding.ASCII.GetBytes(
					"aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" +
					"aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" +
					"aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" +
					"aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" +
					"aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa" + "aaaaaaaa");
				int len = (1000000 - i) < 160 ? 1000000 - i : 160;
				byte[] tmp = new byte[len];
				Buffer.BlockCopy(msg, 0, tmp, 0, len);
				ctx.Update(tmp);
			}
			digest = ctx.DigestFinal();

			str1 = BitConverter.ToString(digest);
			str2 = BitConverter.ToString(results[2]);
			Assert.AreEqual(str2, str1);

			Console.Write(".");

			Console.WriteLine(" passed.");
		}