Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 96 lines (87 sloc) 3.466 kb
6af4a803 » spouliot
2012-01-14 Useful tools to compare different algorithms and implementations
1 using System;
2 using System.Security.Cryptography;
3 using System.Text;
4
5 class Program {
6
7 static void Test (HashAlgorithm digest)
8 {
9 byte[] result1, result2, result3;
10
11 if (digest is SHA1) {
12 Console.WriteLine ("Testing results wrt FIPS 180-1 test vectors");
13 result1 = new byte [] { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
14 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d };
15 result2 = new byte [] { 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae,
16 0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1 };
17 result3 = new byte [] { 0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e,
18 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f };
19 } else if (digest is SHA256) {
20 Console.WriteLine ("Testing results wrt FIPS 180-2 test vectors");
21 result1 = new byte [] { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
22 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
23 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
24 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad };
25 result2 = new byte [] { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
26 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
27 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
28 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 };
29 result3 = new byte [] { 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92,
30 0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67,
31 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e,
32 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0 };
33 } else {
34 Console.WriteLine ("No test vectors were found.");
35 return;
36 }
37 string input1 = "abc";
38 string input2 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
39
40 byte[] input = Encoding.Default.GetBytes (input1);
41 byte[] output = digest.ComputeHash (input);
42 Console.WriteLine ("FIPS 180 Test 1: {0}",
43 BitConverter.ToString (result1) != BitConverter.ToString (output) ?
44 "FAIL" : "PASS");
45
46 input = Encoding.Default.GetBytes (input2);
47 output = digest.ComputeHash (input);
48 Console.WriteLine ("FIPS 180 Test 2: {0}",
49 BitConverter.ToString (result2) != BitConverter.ToString (output) ?
50 "FAIL" : "PASS");
51
52 input = new byte [1000000];
53 for (int i = 0; i < 1000000; i++)
54 input[i] = 0x61; // a
55 output = digest.ComputeHash (input);
56 Console.WriteLine ("FIPS 180 Test 3: {0}",
57 BitConverter.ToString (result3) != BitConverter.ToString (output) ?
58 "FAIL" : "PASS");
59 }
60
61 static void Perf (HashAlgorithm digest)
62 {
63 Console.WriteLine ("Performance tests for different block sizes, 30 seconds each");
64 int block = 1;
65 while (block <= 64 * 1024) {
66 Speed (digest, block);
67 block <<= 2;
68 }
69 }
70
71 static void Speed (HashAlgorithm digest, int block)
72 {
73 byte[] input = new byte [block];
74 byte[] output = new byte [block];
75 DateTime now = DateTime.UtcNow;
76 long size = 0;
77 while ((DateTime.UtcNow - now).TotalSeconds < 30) {
78 digest.TransformBlock (input, 0, input.Length, output, 0);
79 size += input.Length;
80 }
81 digest.TransformFinalBlock (input, 0, input.Length);
82 size += input.Length;
83 double speed = size / (DateTime.UtcNow - now).TotalSeconds;
84 Console.WriteLine ("{0}: {1}: {2} Mbytes/sec", block, digest, speed / 1024 / 1024);
85 }
86
87 static void Main (string[] args)
88 {
89 foreach (string arg in args) {
90 HashAlgorithm hash = (HashAlgorithm) CryptoConfig.CreateFromName (arg);
91 Console.WriteLine ("{0}: {1}", arg, hash);
92 Test (hash);
93 Perf (hash);
94 }
95 }
96 }
Something went wrong with that request. Please try again.