summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
authorunknown <peter@linux.local>2002-06-11 13:37:48 +0400
committerunknown <peter@linux.local>2002-06-11 13:37:48 +0400
commitb6afdd09dbbfc37085061a421170c0585bb54f0d (patch)
treeb7b63c5072690768a1b6914399e49cc5422ea4ec /sql
parent15c99d52a2037d2a3f0b909dacd56aded6802c8b (diff)
downloadmariadb-git-b6afdd09dbbfc37085061a421170c0585bb54f0d.tar.gz
This changeset is mostly new version of previous commit modified according
to Monty's code style and optimization comments client/client_priv.h: New value for --single-transaction option client/mysqldump.c: Add --single-transaction option for consistent dumps mysql-test/r/func_str.result: New functions test results mysql-test/t/func_str.test: Tests for SHA/AES functions mysys/Makefile.am: Extra files require compilation now sql/item_create.cc: New functions sql/item_create.h: New functions sql/item_strfunc.cc: Implementation of SHA/AES_ENCRYPT/AES_DECRYPT sql/item_strfunc.h: Required class definition sql/lex.h: Add symbols for implemented functions BitKeeper/etc/logging_ok: Logging to logging@openlogging.org accepted
Diffstat (limited to 'sql')
-rw-r--r--sql/item_create.cc15
-rw-r--r--sql/item_create.h3
-rw-r--r--sql/item_strfunc.cc111
-rw-r--r--sql/item_strfunc.h28
-rw-r--r--sql/lex.h4
5 files changed, 161 insertions, 0 deletions
diff --git a/sql/item_create.cc b/sql/item_create.cc
index 6f64e9517ba..61e82b664ff 100644
--- a/sql/item_create.cc
+++ b/sql/item_create.cc
@@ -32,6 +32,16 @@ Item *create_func_acos(Item* a)
return new Item_func_acos(a);
}
+Item *create_func_aes_encrypt(Item* a, Item* b)
+{
+ return new Item_func_aes_encrypt(a, b);
+}
+
+Item *create_func_aes_decrypt(Item* a, Item* b)
+{
+ return new Item_func_aes_decrypt(a, b);
+}
+
Item *create_func_ascii(Item* a)
{
return new Item_func_ascii(a);
@@ -327,6 +337,11 @@ Item *create_func_sin(Item* a)
return new Item_func_sin(a);
}
+Item *create_func_sha(Item* a)
+{
+ return new Item_func_sha(a);
+}
+
Item *create_func_space(Item *a)
{
return new Item_func_repeat(new Item_string(" ",1),a);
diff --git a/sql/item_create.h b/sql/item_create.h
index 580596505da..323e053c261 100644
--- a/sql/item_create.h
+++ b/sql/item_create.h
@@ -18,6 +18,8 @@
Item *create_func_abs(Item* a);
Item *create_func_acos(Item* a);
+Item *create_func_aes_encrypt(Item* a, Item* b);
+Item *create_func_aes_decrypt(Item* a, Item* b);
Item *create_func_ascii(Item* a);
Item *create_func_asin(Item* a);
Item *create_func_bin(Item* a);
@@ -75,6 +77,7 @@ Item *create_func_rtrim(Item* a);
Item *create_func_sec_to_time(Item* a);
Item *create_func_sign(Item* a);
Item *create_func_sin(Item* a);
+Item *create_func_sha(Item* a);
Item *create_func_soundex(Item* a);
Item *create_func_space(Item *);
Item *create_func_sqrt(Item* a);
diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc
index 9c6681cdcbf..adf21ce384f 100644
--- a/sql/item_strfunc.cc
+++ b/sql/item_strfunc.cc
@@ -34,6 +34,8 @@
#include <openssl/des.h>
#endif /* HAVE_OPENSSL */
#include "md5.h"
+#include "sha1.h"
+#include "my_aes.h"
String empty_string("");
@@ -99,6 +101,115 @@ void Item_func_md5::fix_length_and_dec()
max_length=32;
}
+
+String *Item_func_sha::val_str(String *str)
+{
+ String * sptr= args[0]->val_str(str);
+ if (sptr) /* If we got value different from NULL */
+ {
+ SHA1_CONTEXT context; /* Context used to generate SHA1 hash */
+ /* Temporary buffer to store 160bit digest */
+ uint8_t digest[SHA1_HASH_SIZE];
+ null_value=0;
+ sha1_reset(&context); /* We do not have to check for error here */
+ /* No need to check error as the only case would be too long message */
+ sha1_input(&context,(const unsigned char *) sptr->ptr(), sptr->length());
+
+ if (str->alloc(SHA1_HASH_SIZE*2) || (sha1_result(&context,digest)) )
+ // Ensure that memory is free
+ {
+ null_value=1;
+ return 0;
+ }
+ sprintf((char *) str->ptr(),
+ "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\
+%02x%02x%02x%02x%02x%02x%02x%02x",
+ digest[0], digest[1], digest[2], digest[3],
+ digest[4], digest[5], digest[6], digest[7],
+ digest[8], digest[9], digest[10], digest[11],
+ digest[12], digest[13], digest[14], digest[15],
+ digest[16], digest[17], digest[18], digest[19]);
+
+ str->length((uint) SHA1_HASH_SIZE*2);
+ return str;
+ }
+ null_value=1;
+ return 0;
+}
+
+void Item_func_sha::fix_length_and_dec()
+{
+ max_length=SHA1_HASH_SIZE*2; // size of hex representation of hash
+}
+
+
+/* Implementation of AES encryption routines */
+
+String *Item_func_aes_encrypt::val_str(String *str)
+{
+ String * sptr = args[0]->val_str(str); // String to encrypt
+ String tmp_value; // required to handle second parameter
+ String * key= args[1]->val_str(&tmp_value); // key
+ int aes_length;
+ if (sptr && key) // we need both arguments to be not NULL
+ {
+ null_value=0;
+ aes_length=my_aes_get_size(sptr->length()); // calculate result length
+
+ if ( !str->alloc(aes_length) ) // Ensure that memory is free
+ {
+ // finally encrypt directly to allocated buffer.
+ if (my_aes_encrypt(sptr->ptr(),sptr->length(),str->ptr(),key->ptr(),
+ key->length()) == aes_length)
+ {
+ // we have to get expected result length
+ str->length((uint)aes_length);
+ return str;
+ }
+ }
+ }
+ null_value=1;
+ return 0;
+}
+
+void Item_func_aes_encrypt::fix_length_and_dec()
+{
+ max_length=my_aes_get_size(args[0]->max_length);
+}
+
+
+String *Item_func_aes_decrypt::val_str(String *str)
+{
+ String * sptr= args[0]->val_str(str); // String to decrypt
+ String tmp_value; // temporary string required for parsing
+ String * key= args[1]->val_str(&tmp_value); // key
+ int length; // original length after decrypt
+ if (sptr && key) // Need to have both arguments not NULL
+ {
+ null_value=0;
+ if ( !str->alloc(sptr->length()) ) // Ensure that memory is free
+ {
+ // finally decencrypt directly to allocated buffer.
+ length=my_aes_decrypt(sptr->ptr(),sptr->length(),str->ptr(),
+ key->ptr(),key->length());
+ if (length>=0) // if we got correct data data
+ {
+ str->length((uint)length);
+ return str;
+ }
+ }
+ }
+ // Bad parameters. No memory or bad data will all go here
+ null_value=1;
+ return 0;
+}
+
+void Item_func_aes_decrypt::fix_length_and_dec()
+{
+ max_length=args[0]->max_length;
+}
+
+
/*
** Concatinate args with the following premissess
** If only one arg which is ok, return value of arg
diff --git a/sql/item_strfunc.h b/sql/item_strfunc.h
index 1279a5099d5..85910f45c77 100644
--- a/sql/item_strfunc.h
+++ b/sql/item_strfunc.h
@@ -52,6 +52,34 @@ public:
const char *func_name() const { return "md5"; }
};
+class Item_func_sha :public Item_str_func
+{
+public:
+ Item_func_sha(Item *a) :Item_str_func(a) {}
+ String *val_str(String *);
+ void fix_length_and_dec();
+ const char *func_name() const { return "sha"; }
+};
+
+class Item_func_aes_encrypt :public Item_str_func
+{
+public:
+ Item_func_aes_encrypt(Item *a, Item *b) :Item_str_func(a,b) {}
+ String *val_str(String *);
+ void fix_length_and_dec();
+ const char *func_name() const { return "aes_encrypt"; }
+};
+
+class Item_func_aes_decrypt :public Item_str_func
+{
+public:
+ Item_func_aes_decrypt(Item *a, Item *b) :Item_str_func(a,b) {}
+ String *val_str(String *);
+ void fix_length_and_dec();
+ const char *func_name() const { return "aes_decrypt"; }
+};
+
+
class Item_func_concat :public Item_str_func
{
String tmp_value;
diff --git a/sql/lex.h b/sql/lex.h
index e03c4db6479..ffe4f1dda05 100644
--- a/sql/lex.h
+++ b/sql/lex.h
@@ -392,6 +392,8 @@ static SYMBOL sql_functions[] = {
{ "ABS", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_abs)},
{ "ACOS", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_acos)},
{ "ADDDATE", SYM(DATE_ADD_INTERVAL),0,0},
+ { "AES_ENCRYPT", SYM(FUNC_ARG2),0,CREATE_FUNC(create_func_aes_encrypt)},
+ { "AES_DECRYPT", SYM(FUNC_ARG2),0,CREATE_FUNC(create_func_aes_decrypt)},
{ "ASCII", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_ascii)},
{ "ASIN", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_asin)},
{ "ATAN", SYM(ATAN),0,0},
@@ -494,6 +496,8 @@ static SYMBOL sql_functions[] = {
{ "SUBDATE", SYM(DATE_SUB_INTERVAL),0,0},
{ "SIGN", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_sign)},
{ "SIN", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_sin)},
+ { "SHA", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_sha)},
+ { "SHA1", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_sha)},
{ "SOUNDEX", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_soundex)},
{ "SPACE", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_space)},
{ "SQRT", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_sqrt)},