~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* Copyright (C) 2010 Rackspace

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

#include "config.h"
#include "mysql_password.h"
#include "drizzled/algorithm/sha1.h"
#include "drizzled/util/convert.h"

using namespace std;
using namespace drizzled;

const char* MySQLPasswordName = "mysql_password";

MySQLPassword::MySQLPassword(void):
  Item_str_func()
{ }

const char *MySQLPassword::func_name() const
{
  return MySQLPasswordName;
}
  
void MySQLPassword::fix_length_and_dec()
{
  max_length= args[0]->max_length;
}

bool MySQLPassword::check_argument_count(int n)
{
  return (n == 1);
}

String *MySQLPassword::val_str(String *str)
{
  String argument;
  String *password= args[0]->val_str(&argument);
  SHA1_CTX ctx;
  uint8_t hash_tmp1[SHA1_DIGEST_LENGTH];
  uint8_t hash_tmp2[SHA1_DIGEST_LENGTH];

  SHA1Init(&ctx);
  SHA1Update(&ctx, reinterpret_cast<uint8_t *>(password->ptr()),
             password->length());
  SHA1Final(hash_tmp1, &ctx);

  SHA1Init(&ctx);
  SHA1Update(&ctx, hash_tmp1, SHA1_DIGEST_LENGTH);
  SHA1Final(hash_tmp2, &ctx);

  str->realloc(SHA1_DIGEST_LENGTH * 2);
  drizzled_string_to_hex(str->ptr(), reinterpret_cast<const char*>(hash_tmp2),
                         SHA1_DIGEST_LENGTH);
  str->length(SHA1_DIGEST_LENGTH * 2);

  return str;
}