Skip to content

Instantly share code, notes, and snippets.

@BrianLeishman
Last active February 27, 2018 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianLeishman/d7903a4acba75707c05fc581e1c714c3 to your computer and use it in GitHub Desktop.
Save BrianLeishman/d7903a4acba75707c05fc581e1c714c3 to your computer and use it in GitHub Desktop.
MySQL UDF for generating SHA3 hashes without hex encoding (much faster)
/*
* Copyright (c) 2018, brian
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* File: unhex_unhex_sha3.c
* Author: brian
*
* Created on February 26, 2018, 9:26 AM
*/
/*
* USAGE INSTRUCTIONS:
*
* make sure libmysqlclient-dev and rhash is installed:
* apt install libmysqlclient-dev rhash librhash-dev
*
* Replace "/usr/lib/mysql/plugin" with your MySQL plugins directory (can be found by running "select @@plugin_dir;")
* gcc -I/usr/include/mysql -o unhex_sha3.so -shared unhex_sha3.c -lrhash -fPIC -O2 && cp unhex_sha3.so /usr/lib/mysql/plugin/unhex_sha3.so
*
* Then, on the server:
* create function`unhex_sha3`returns string soname'unhex_sha3.so';
*
* And use/test like:
* select`unhex_sha3`('yeet',224);
*
* Note: this function is intended to work EXACTLY like the native unhex(sha2()) functions,
* with the same argument order, but this is for both functions, so this function
* returns the non-hex encoded output, e.g. a 224 bit hash will return 28 (most likely) unprintable bytes,
* instead of the 56 printable bytes.
*
* Store in a BINARY(28) column type!
*
* Yeet!
*
*/
#ifdef STANDARD
/* STANDARD is defined, don't use any mysql functions */
#include <string.h>
#ifdef __WIN__
typedef unsigned __int64 ulonglong; /* Microsofts 64 bit types */
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif /*__WIN__*/
#else
#include <my_global.h>
#include <my_sys.h>
#if defined(MYSQL_SERVER)
#include <m_string.h>
#else
/* when compiled as standalone */
#include <string.h>
#endif
#endif
#include <mysql.h>
#include <rhash.h>
#ifdef HAVE_DLOPEN
my_bool unhex_sha3_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void unhex_sha3_deinit(UDF_INIT *initid);
char *unhex_sha3(UDF_INIT *initid __attribute__ ((unused)), UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *message __attribute__ ((unused)));
my_bool unhex_sha3_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
if (args->arg_count != 2) {
strcpy(message, "`unhex_sha3`() requires 2 parameters: the message part, and the bits");
return 1;
}
args->arg_type[1] = INT_RESULT;
initid->maybe_null = 1; //can return null
return 0;
}
char *unhex_sha3(UDF_INIT *initid __attribute__ ((unused)), UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *message __attribute__ ((unused))) {
long long int_val;
int_val = *((long long*) args->args[1]);
if (int_val == 0) {
int_val = 256;
}
if (args->args[0] == NULL || args->args[1] == NULL ||
(int_val != 224 && int_val != 256 && int_val != 384 && int_val != 512)) {
*is_null = 1;
return 0;
}
unsigned long len = int_val / 8;
unsigned char *hash = malloc(sizeof (char) * (len));
unsigned hash_id;
switch (int_val) {
case 224:
hash_id = RHASH_SHA3_224;
break;
case 256:
hash_id = RHASH_SHA3_256;
break;
case 384:
hash_id = RHASH_SHA3_384;
break;
case 512:
hash_id = RHASH_SHA3_512;
break;
}
if (rhash_msg(hash_id, args->args[0], args->lengths[0], hash)) {
*is_null = 1;
return 0;
}
*length = len;
return hash;
}
#endif /* HAVE_DLOPEN */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment