📄 proc.cpp
字号:
// xp_blowfishencrypt
//
// Michael Coles, MCDBA
//
// Ver. 0.9.0.0: Updated 1/19/2006 to fix memory fragmentation problems that show up
// when run on very large datasets
//
#include "../common/stdafx/stdafx.h"
#include "../common/base64/base64.h"
#include "../common/params/params.h"
#include "blowfish.h"
#define XP_NOERROR 0
#define XP_ERROR 1
#define MAXCOLNAME 25
#define MAXNAME 25
#define MAXTEXT 255
#define ERR_USAGE "Usage: EXEC xp_blowfishencrypt @param1, @param2 [, param3 OUTPUT]"
#define ERR_KEY_SIZE "Key (@param2) must be 32 - 448 bits in length (8 - 56 bytes)"
#define ERR_OUTPUT "If @param3 is specified, it must be OUTPUT parameter"
#ifdef __cplusplus
extern "C" {
#endif
RETCODE __declspec(dllexport) xp_blowfishencrypt(SRV_PROC *srvproc);
#ifdef __cplusplus
}
#endif
void PrintError (SRV_PROC *srvproc, char *ErrorMsg);
RETCODE __declspec(dllexport) xp_blowfishencrypt(SRV_PROC *srvproc)
{
SRVRETCODE sqlerr = XP_NOERROR;
int j = params::getparamcount(srvproc);
params *P1 = new params;
params *P2 = new params;
params *P3 = new params;
BYTE *text = NULL;
ULONG textlength = 0;
BYTE *key = NULL;
ULONG keylength = 0;
BYTE *text2 = NULL;
ULONG enc_len = 0;
if (j < 2 || j > 3) { // Less than 2 or more than 3 parameters
PrintError (srvproc, ERR_USAGE);
sqlerr = XP_ERROR;
} else {
// Get the parameters
params::getparam(srvproc, 1, P1); // @param1 contains our encrypted Base64 text
params::getparam(srvproc, 2, P2); // @param2 is our key
params::getparam(srvproc, 3, P3); // @param3 is our optional output parameter
// Get our key length
keylength = CBase64Utils::CalculateDecodeBufferSize(P2->length);
key = new BYTE [keylength + 10];
memset(key, 0, keylength + 10);
// Now decode our key from Base64
keylength = CBase64Utils::Decode(P2->cdata, key, P2->length);
if (keylength < 8 || keylength > 56) { // Is our keylength between 32 and 448 bits?
PrintError (srvproc, ERR_KEY_SIZE);
sqlerr = XP_ERROR;
} else {
// Now create and initialize the Blowfish object
Blowfish b;
b.Set_Passwd((char *)key, keylength);
text2 = new BYTE [P1->length + 8];
memset (text2, 0, P1->length + 8);
memcpy (text2, P1->cdata, P1->length);
ULONG enc_len = (P1->length % 8 == 0) ? P1->length : P1->length + (8 - (P1->length % 8));
b.Encrypt((char *)text2, enc_len);
// Now get our text length
textlength = CBase64Utils::CalculateEncodeBufferSize(enc_len);
text = new BYTE[textlength + 16];
memset(text, 0, textlength + 16);
CBase64Utils::Encode(text2, text, enc_len);
if (j == 2) { // Only 2 params, so return a SQL result set
DBCHAR colname [25];
_snprintf(colname, 25, "ENCRYPTED");
srv_describe(srvproc, 1, colname, SRV_NULLTERM, SRVVARCHAR, textlength, SRVVARCHAR, 0, 0);
srv_setcoldata(srvproc, 1, text);
srv_setcollen(srvproc, 1, textlength);
srv_sendrow(srvproc);
srv_senddone(srvproc, SRV_DONE_MORE | SRV_DONE_COUNT, (DBUSMALLINT)0, (DBINT)1);
} else { // 3 params, so check if @param3 is OUTPUT type
if (!P3->isoutput) {
PrintError (srvproc, ERR_OUTPUT);
sqlerr = XP_ERROR;
} else { // Output result to @param3
srv_paramsetoutput(srvproc, 3, text, textlength, P1->isnull);
srv_senddone(srvproc, SRV_DONE_MORE, (DBUSMALLINT)0, (DBINT)0);
}
}
}
}
if (text != NULL) {
memset(text, 0, textlength);
delete [] text;
text = NULL;
}
if (text2 != NULL) {
memset(text2, 0, enc_len);
delete [] text2;
text2 = NULL;
}
if (key != NULL) {
memset(key, 0, keylength);
delete [] key;
key = NULL;
}
if (P1 != NULL) {
delete P1;
P1 = NULL;
}
if (P2 != NULL) {
delete P2;
P2 = NULL;
}
if (P3 != NULL) {
delete P3;
P3 = NULL;
}
return sqlerr;
}
// This routine sends an error message to the SQL Server Output Window
//
// Parameters:
// SRV_PROC *srvproc The server process pointer passed in by SQL Server
// char *ErrorMsg The error message string to print
//
void PrintError (SRV_PROC *srvproc, char *ErrorMsg)
{
// Print the error message on the SQL Server output screen
srv_sendmsg(srvproc, SRV_MSG_ERROR, XP_ERROR, SRV_INFO, 1,
NULL, 0, (DBUSMALLINT) __LINE__,
ErrorMsg,
SRV_NULLTERM);
// Alert SQL Server that an error has occurred
srv_senddone(srvproc, (SRV_DONE_ERROR | SRV_DONE_MORE), 0, 0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -