~stewart/drizzle/embedded-innodb-create-select-transaction-arrgh

« back to all changes in this revision

Viewing changes to sql/sql_crypt.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2001, 2003, 2005 MySQL AB
 
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
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
#ifdef USE_PRAGMA_IMPLEMENTATION
 
26
#pragma implementation                          // gcc: Class implementation
 
27
#endif
 
28
 
 
29
#include "mysql_priv.h"
 
30
 
 
31
SQL_CRYPT::SQL_CRYPT(const char *password)
 
32
{
 
33
  ulong rand_nr[2];
 
34
  hash_password(rand_nr,password, strlen(password));
 
35
  crypt_init(rand_nr);
 
36
}
 
37
 
 
38
void SQL_CRYPT::crypt_init(ulong *rand_nr)
 
39
{
 
40
  uint i;
 
41
  randominit(&rand,rand_nr[0],rand_nr[1]);
 
42
 
 
43
  for (i=0 ; i<=255; i++)
 
44
   decode_buff[i]= (char) i;
 
45
 
 
46
  for (i=0 ; i<= 255 ; i++)
 
47
  {
 
48
    int idx= (uint) (my_rnd(&rand)*255.0);
 
49
    char a= decode_buff[idx];
 
50
    decode_buff[idx]= decode_buff[i];
 
51
    decode_buff[+i]=a;
 
52
  }
 
53
  for (i=0 ; i <= 255 ; i++)
 
54
   encode_buff[(uchar) decode_buff[i]]=i;
 
55
  org_rand=rand;
 
56
  shift=0;
 
57
}
 
58
 
 
59
 
 
60
void SQL_CRYPT::encode(char *str,uint length)
 
61
{
 
62
  for (uint i=0; i < length; i++)
 
63
  {
 
64
    shift^=(uint) (my_rnd(&rand)*255.0);
 
65
    uint idx= (uint) (uchar) str[0];
 
66
    *str++ = (char) ((uchar) encode_buff[idx] ^ shift);
 
67
    shift^= idx;
 
68
  }
 
69
}
 
70
 
 
71
 
 
72
void SQL_CRYPT::decode(char *str,uint length)
 
73
{
 
74
  for (uint i=0; i < length; i++)
 
75
  {
 
76
    shift^=(uint) (my_rnd(&rand)*255.0);
 
77
    uint idx= (uint) ((uchar) str[0] ^ shift);
 
78
    *str = decode_buff[idx];
 
79
    shift^= (uint) (uchar) *str++;
 
80
  }
 
81
}