MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_crypt.cc
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
16 
17 
18 /*
19  Functions to handle the encode() and decode() functions
20  The strongness of this crypt is large based on how good the random
21  generator is. It should be ok for short strings, but for communication one
22  needs something like 'ssh'.
23 */
24 
25 #include "sql_priv.h"
26 #include "sql_crypt.h"
27 #include "password.h"
28 
29 void SQL_CRYPT::init(ulong *rand_nr)
30 {
31  uint i;
32  randominit(&rand,rand_nr[0],rand_nr[1]);
33 
34  for (i=0 ; i<=255; i++)
35  decode_buff[i]= (char) i;
36 
37  for (i=0 ; i<= 255 ; i++)
38  {
39  int idx= (uint) (my_rnd(&rand)*255.0);
40  char a= decode_buff[idx];
41  decode_buff[idx]= decode_buff[i];
42  decode_buff[+i]=a;
43  }
44  for (i=0 ; i <= 255 ; i++)
45  encode_buff[(uchar) decode_buff[i]]=i;
46  org_rand=rand;
47  shift=0;
48 }
49 
50 
51 void SQL_CRYPT::encode(char *str,uint length)
52 {
53  for (uint i=0; i < length; i++)
54  {
55  shift^=(uint) (my_rnd(&rand)*255.0);
56  uint idx= (uint) (uchar) str[0];
57  *str++ = (char) ((uchar) encode_buff[idx] ^ shift);
58  shift^= idx;
59  }
60 }
61 
62 
63 void SQL_CRYPT::decode(char *str,uint length)
64 {
65  for (uint i=0; i < length; i++)
66  {
67  shift^=(uint) (my_rnd(&rand)*255.0);
68  uint idx= (uint) ((uchar) str[0] ^ shift);
69  *str = decode_buff[idx];
70  shift^= (uint) (uchar) *str++;
71  }
72 }