Inital Commit
[oweals/finalsclub.git] / node_modules / mysql / test / fixture / libmysql_password.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4
5 #define SCRAMBLE_LENGTH_323 8
6
7 typedef unsigned long ulong;
8 typedef unsigned int uint;
9 typedef unsigned char uchar;
10
11 struct rand_struct {
12   unsigned long seed1,seed2,max_value;
13   double max_value_dbl;
14 };
15
16 void hash_password(ulong *result, const char *password, uint password_len)
17 {
18   register ulong nr=1345345333L, add=7, nr2=0x12345671L;
19   ulong tmp;
20   const char *password_end= password + password_len;
21   for (; password < password_end; password++)
22   {
23     if (*password == ' ' || *password == '\t')
24       continue;                                 /* skip space in password */
25     tmp= (ulong) (uchar) *password;
26     nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
27     nr2+=(nr2 << 8) ^ nr;
28     add+=tmp;
29   }
30   result[0]=nr & (((ulong) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
31   result[1]=nr2 & (((ulong) 1L << 31) -1L);
32 }
33
34 void randominit(struct rand_struct *rand_st, ulong seed1, ulong seed2)
35 {                                               /* For mysql 3.21.# */
36 #ifdef HAVE_purify
37   bzero((char*) rand_st,sizeof(*rand_st));      /* Avoid UMC varnings */
38 #endif
39   rand_st->max_value= 0x3FFFFFFFL;
40   rand_st->max_value_dbl=(double) rand_st->max_value;
41   rand_st->seed1=seed1%rand_st->max_value ;
42   rand_st->seed2=seed2%rand_st->max_value;
43 }
44
45 double my_rnd(struct rand_struct *rand_st)
46 {
47   rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
48   rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
49   return (((double) rand_st->seed1)/rand_st->max_value_dbl);
50 }
51
52 void scramble_323(char *to, const char *message, const char *password)
53 {
54   struct rand_struct rand_st;
55   ulong hash_pass[2], hash_message[2];
56
57   if (password && password[0])
58   {
59     char extra, *to_start=to;
60     const char *message_end= message + SCRAMBLE_LENGTH_323;
61     hash_password(hash_pass,password, (uint) strlen(password));
62     hash_password(hash_message, message, SCRAMBLE_LENGTH_323);
63     randominit(&rand_st,hash_pass[0] ^ hash_message[0],
64                hash_pass[1] ^ hash_message[1]);
65     for (; message < message_end; message++)
66       *to++= (char) (floor(my_rnd(&rand_st)*31)+64);
67     extra=(char) (floor(my_rnd(&rand_st)*31));
68     while (to_start != to)
69       *(to_start++)^=extra;
70   }
71   *to= 0;
72 }
73
74 int main() {
75   const char password1[] = "root";
76   const char password2[] = "long password test";
77   const char password3[] = "saf789yasfbsd89f";
78   ulong result[2];
79   char scrm[9]; // SCRAMBLE_LENGTH_323+1
80   struct rand_struct rand_st;
81   int i;
82
83   // test hash_password
84   hash_password((ulong*)result, password1, strlen(password1));
85   printf("hash_password(\"%s\") = %08x%08x\n", password1, result[0], result[1]);
86
87   hash_password((ulong*)result, password2, strlen(password2));
88   printf("hash_password(\"%s\") = %08x%08x\n", password2, result[0], result[1]);
89
90   hash_password((ulong*)result, password3, strlen(password3));
91   printf("hash_password(\"%s\") = %08x%08x\n", password3, result[0], result[1]);
92
93
94   // test randominit
95   randominit(&rand_st, 0, 0);
96   printf("randominit(0x00000000,0x00000000) = %08x, %08x\n", rand_st.seed1, rand_st.seed2);
97
98   randominit(&rand_st, 0xFFFF, 0xFFFF);
99   printf("randominit(0x0000FFFF,0x0000FFFF) = %08x, %08x\n", rand_st.seed1, rand_st.seed2);
100
101   randominit(&rand_st, 0x50000000, 0x50000000);
102   printf("randominit(0x50000000,0x50000000) = %08x, %08x\n", rand_st.seed1, rand_st.seed2);
103
104   randominit(&rand_st, 0xFFFFFFFF, 0xFFFFFFFF);
105   printf("randominit(0xFFFFFFFF,0xFFFFFFFF) = %08x, %08x\n", rand_st.seed1, rand_st.seed2);
106
107
108   // test my_rnd
109   randominit(&rand_st, 3252345, 7149734);
110   printf("randominit(3252345, 7149734) = %08x, %08x\n", rand_st.seed1, rand_st.seed2);
111   for (i=0; i<10; i++){
112           printf("my_rnd() : %.16f\n", my_rnd(&rand_st));
113   }
114
115
116   // test scramble_323
117   scramble_323(scrm, "8bytesofstuff", "root");
118   printf("scramble323(8bytesofstuff, root): %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
119     scrm[0], scrm[1], scrm[2], scrm[3], scrm[4], scrm[5], scrm[6], scrm[7], scrm[8]);
120
121   scramble_323(scrm, "e8cf00cec9ec825af22", "saf789yasfbsd");
122   printf("scramble323(e8cf00cec9ec825af22, saf789yasfbsd): %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
123     scrm[0], scrm[1], scrm[2], scrm[3], scrm[4], scrm[5], scrm[6], scrm[7], scrm[8]);
124
125   return 23;
126 }
127