You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.0 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. /* Copyright (C) 2000-2001, 2003, 2005 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /*
  13. Functions to handle the encode() and decode() functions
  14. The strongness of this crypt is large based on how good the random
  15. generator is. It should be ok for short strings, but for communication one
  16. needs something like 'ssh'.
  17. */
  18. #ifdef USE_PRAGMA_IMPLEMENTATION
  19. #pragma implementation // gcc: Class implementation
  20. #endif
  21. #include "sql_priv.h"
  22. #include "sql_crypt.h"
  23. #include "password.h"
  24. void SQL_CRYPT::init(ulong *rand_nr)
  25. {
  26. uint i;
  27. randominit(&rand,rand_nr[0],rand_nr[1]);
  28. for (i=0 ; i<=255; i++)
  29. decode_buff[i]= (char) i;
  30. for (i=0 ; i<= 255 ; i++)
  31. {
  32. int idx= (uint) (my_rnd(&rand)*255.0);
  33. char a= decode_buff[idx];
  34. decode_buff[idx]= decode_buff[i];
  35. decode_buff[+i]=a;
  36. }
  37. for (i=0 ; i <= 255 ; i++)
  38. encode_buff[(uchar) decode_buff[i]]=i;
  39. org_rand=rand;
  40. shift=0;
  41. }
  42. void SQL_CRYPT::encode(char *str,uint length)
  43. {
  44. for (uint i=0; i < length; i++)
  45. {
  46. shift^=(uint) (my_rnd(&rand)*255.0);
  47. uint idx= (uint) (uchar) str[0];
  48. *str++ = (char) ((uchar) encode_buff[idx] ^ shift);
  49. shift^= idx;
  50. }
  51. }
  52. void SQL_CRYPT::decode(char *str,uint length)
  53. {
  54. for (uint i=0; i < length; i++)
  55. {
  56. shift^=(uint) (my_rnd(&rand)*255.0);
  57. uint idx= (uint) ((uchar) str[0] ^ shift);
  58. *str = decode_buff[idx];
  59. shift^= (uint) (uchar) *str++;
  60. }
  61. }