Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / desweak.cs
1 // DESTest.cs was failing with rev 38316...
2 // but the DES code was changed after the fix
3 // so the original code lives here forever ;-)
4
5 using System;
6
7 class Program {
8
9         public const int keySizeByte = 8;
10
11         internal static readonly ulong[] weakKeys = {
12                 0x0101010101010101, /* 0000000 0000000 */
13                 0xFEFEFEFEFEFEFEFE, /* FFFFFFF FFFFFFF */
14                 0x1F1F1F1F0E0E0E0E, /* 0000000 FFFFFFF */
15                 0xE0E0E0E0F1F1F1F1  /* FFFFFFF 0000000 */
16         };
17
18         internal static ulong PackKey (byte[] key) 
19         {
20                 byte[] paritySetKey = new byte [keySizeByte];
21                 // adapted from bouncycastle - see bouncycastle.txt
22                 for (int i=0; i < key.Length; i++) {
23                         byte b = key [i];
24                         paritySetKey [i] = (byte)((b & 0xfe) |
25                                 ((((b >> 1) ^ (b >> 2) ^ (b >> 3) ^ (b >> 4) ^
26                                 (b >> 5) ^ (b >> 6) ^ (b >> 7)) ^ 0x01) & 0x01));
27                 }
28
29                 ulong res = 0;
30                 for (int i = 0, sh = 64; (sh = sh - 8) >= 0; i++) {
31                         res |= (ulong) paritySetKey [i] << sh;
32                 }
33                 return res;
34         }
35
36         static int Main ()
37         {
38                 byte[] wk2p = { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E };
39                 ulong lk = PackKey (wk2p);
40                 foreach (ulong wk in weakKeys) {
41                         if (lk == wk)
42                                 return 0;
43                 }
44                 return 1;
45         }
46 }