Drizzled Public API Documentation

mysql_password.cc

00001 /* Copyright (C) 2010 Rackspace
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 #include <config.h>
00017 #include "mysql_password.h"
00018 #include <drizzled/algorithm/sha1.h>
00019 #include <drizzled/util/convert.h>
00020 
00021 using namespace std;
00022 
00023 namespace drizzle_plugin
00024 {
00025 
00026 const char* MySQLPasswordName = "mysql_password";
00027 
00028 MySQLPassword::MySQLPassword(void):
00029   Item_str_func()
00030 { }
00031 
00032 const char *MySQLPassword::func_name() const
00033 {
00034   return MySQLPasswordName;
00035 }
00036   
00037 void MySQLPassword::fix_length_and_dec()
00038 {
00039   max_length= args[0]->max_length;
00040 }
00041 
00042 bool MySQLPassword::check_argument_count(int n)
00043 {
00044   return (n == 1);
00045 }
00046 
00047 drizzled::String *MySQLPassword::val_str(drizzled::String *str)
00048 {
00049   drizzled::String argument;
00050   drizzled::String *password= args[0]->val_str(&argument);
00051   drizzled::SHA1_CTX ctx;
00052   uint8_t hash_tmp1[SHA1_DIGEST_LENGTH];
00053   uint8_t hash_tmp2[SHA1_DIGEST_LENGTH];
00054 
00055   drizzled::SHA1Init(&ctx);
00056   drizzled::SHA1Update(&ctx, reinterpret_cast<uint8_t *>(password->ptr()),
00057                        password->length());
00058   drizzled::SHA1Final(hash_tmp1, &ctx);
00059 
00060   drizzled::SHA1Init(&ctx);
00061   drizzled::SHA1Update(&ctx, hash_tmp1, SHA1_DIGEST_LENGTH);
00062   drizzled::SHA1Final(hash_tmp2, &ctx);
00063 
00064   str->realloc(SHA1_DIGEST_LENGTH * 2);
00065   drizzled::drizzled_string_to_hex(str->ptr(),
00066                                    reinterpret_cast<const char*>(hash_tmp2),
00067                                    SHA1_DIGEST_LENGTH);
00068   str->length(SHA1_DIGEST_LENGTH * 2);
00069 
00070   return str;
00071 }
00072 
00073 } /* namespace drizzle_plugin */