[GB.CRYPT]
* NEW: SHA256 and SHA512 crypting were implemented. git-svn-id: svn://localhost/gambas/trunk@6019 867c0c6c-44f3-4631-809d-bfa615b0a4ec
This commit is contained in:
parent
46cc49ac65
commit
9b20abedb0
1 changed files with 76 additions and 38 deletions
|
@ -36,23 +36,42 @@
|
|||
#include "c_crypt.h"
|
||||
#include "main.h"
|
||||
|
||||
enum { USE_DES, USE_MD5, USE_SHA256, USE_SHA512 };
|
||||
|
||||
static char *do_crypt(const char *passwd, const char *prefix, bool use_md5)
|
||||
static char *do_crypt(const char *passwd, const char *prefix, int mode)
|
||||
{
|
||||
static char key_table[65] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcedefghijklmnopqrstuvwxyz./";
|
||||
static bool init = FALSE;
|
||||
|
||||
int i, n;
|
||||
char key[16];
|
||||
char key[18];
|
||||
char *result;
|
||||
char errormsg[35];
|
||||
char errormsg[64];
|
||||
|
||||
switch(mode)
|
||||
{
|
||||
case USE_MD5:
|
||||
strcpy(key, "$1$");
|
||||
n = 8;
|
||||
break;
|
||||
case USE_SHA256:
|
||||
strcpy(key, "$5$");
|
||||
n = 13;
|
||||
break;
|
||||
case USE_SHA512:
|
||||
strcpy(key, "$6$");
|
||||
n = 13;
|
||||
break;
|
||||
case USE_DES:
|
||||
default:
|
||||
n = 2;
|
||||
}
|
||||
|
||||
if (prefix)
|
||||
{
|
||||
n = use_md5 ? 8 : 2;
|
||||
if (strlen(prefix) != n)
|
||||
{
|
||||
snprintf(errormsg, 35, "size must be %d characters long", n);
|
||||
snprintf(errormsg, sizeof(errormsg), "must be %d characters long", n);
|
||||
goto __BAD_PREFIX;
|
||||
}
|
||||
|
||||
|
@ -60,18 +79,15 @@ static char *do_crypt(const char *passwd, const char *prefix, bool use_md5)
|
|||
{
|
||||
if (strchr(key_table, prefix[i]) == NULL)
|
||||
{
|
||||
snprintf(errormsg, 35, "character '%c' not allowed" , prefix[i]);
|
||||
snprintf(errormsg, sizeof(errormsg), "character '%c' is not allowed" , prefix[i]);
|
||||
goto __BAD_PREFIX;
|
||||
}
|
||||
}
|
||||
|
||||
if (use_md5)
|
||||
{
|
||||
strcpy(key, "$1$");
|
||||
strcat(key, prefix);
|
||||
}
|
||||
else
|
||||
if (mode == USE_DES)
|
||||
strcpy(key, prefix);
|
||||
else
|
||||
strcat(key, prefix);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -81,9 +97,14 @@ static char *do_crypt(const char *passwd, const char *prefix, bool use_md5)
|
|||
init = TRUE;
|
||||
}
|
||||
|
||||
if (use_md5)
|
||||
if (mode == USE_DES)
|
||||
{
|
||||
for (i = 0; i < 2; i++)
|
||||
key[i] = key_table[(int)((random() / (RAND_MAX + 1.0)) * sizeof(key_table))];
|
||||
key[2] = 0;
|
||||
}
|
||||
else if (mode == USE_MD5)
|
||||
{
|
||||
strcpy(key, "$1$");
|
||||
for (i = 0; i < 8; i++)
|
||||
key[i + 3] = key_table[(int)((random() / (RAND_MAX + 1.0)) * sizeof(key_table))];
|
||||
key[11] = '$';
|
||||
|
@ -91,9 +112,10 @@ static char *do_crypt(const char *passwd, const char *prefix, bool use_md5)
|
|||
}
|
||||
else
|
||||
{
|
||||
for (i = 0; i < 2; i++)
|
||||
key[i] = key_table[(int)((random() / (RAND_MAX + 1.0)) * sizeof(key_table))];
|
||||
key[2] = 0;
|
||||
for (i = 0; i < 13; i++)
|
||||
key[i + 3] = key_table[(int)((random() / (RAND_MAX + 1.0)) * sizeof(key_table))];
|
||||
key[16] = '$';
|
||||
key[17] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -110,7 +132,6 @@ __BAD_PREFIX:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static bool check_crypt(const char *passwd, const char *crypted)
|
||||
{
|
||||
char *result = crypt(passwd, crypted);
|
||||
|
@ -124,28 +145,45 @@ static bool check_crypt(const char *passwd, const char *crypted)
|
|||
return strcmp(result, crypted) != 0;
|
||||
}
|
||||
|
||||
BEGIN_METHOD(CCRYPT_call_des, GB_STRING password; GB_STRING key)
|
||||
|
||||
char *result;
|
||||
|
||||
result = do_crypt(GB.ToZeroString(ARG(password)), MISSING(key) ? NULL : GB.ToZeroString(ARG(key)), USE_DES);
|
||||
if (result)
|
||||
GB.ReturnNewZeroString(result);
|
||||
|
||||
END_METHOD
|
||||
|
||||
BEGIN_METHOD(CCRYPT_call_md5, GB_STRING password; GB_STRING key)
|
||||
|
||||
char *result;
|
||||
|
||||
result = do_crypt(GB.ToZeroString(ARG(password)), MISSING(key) ? NULL : GB.ToZeroString(ARG(key)), TRUE);
|
||||
result = do_crypt(GB.ToZeroString(ARG(password)), MISSING(key) ? NULL : GB.ToZeroString(ARG(key)), USE_MD5);
|
||||
if (result)
|
||||
GB.ReturnNewZeroString(result);
|
||||
|
||||
END_METHOD
|
||||
|
||||
|
||||
BEGIN_METHOD(CCRYPT_call_des, GB_STRING password; GB_STRING key)
|
||||
BEGIN_METHOD(CCRYPT_call_sha256, GB_STRING password; GB_STRING key)
|
||||
|
||||
char *result;
|
||||
|
||||
result = do_crypt(GB.ToZeroString(ARG(password)), MISSING(key) ? NULL : GB.ToZeroString(ARG(key)), FALSE);
|
||||
result = do_crypt(GB.ToZeroString(ARG(password)), MISSING(key) ? NULL : GB.ToZeroString(ARG(key)), USE_SHA256);
|
||||
if (result)
|
||||
GB.ReturnNewZeroString(result);
|
||||
|
||||
END_METHOD
|
||||
|
||||
BEGIN_METHOD(CCRYPT_call_sha512, GB_STRING password; GB_STRING key)
|
||||
|
||||
char *result;
|
||||
|
||||
result = do_crypt(GB.ToZeroString(ARG(password)), MISSING(key) ? NULL : GB.ToZeroString(ARG(key)), USE_SHA512);
|
||||
if (result)
|
||||
GB.ReturnNewZeroString(result);
|
||||
|
||||
END_METHOD
|
||||
|
||||
BEGIN_METHOD(CCRYPT_check, GB_STRING password; GB_STRING crypt)
|
||||
|
||||
|
@ -153,16 +191,16 @@ BEGIN_METHOD(CCRYPT_check, GB_STRING password; GB_STRING crypt)
|
|||
|
||||
END_METHOD
|
||||
|
||||
|
||||
GB_DESC CCryptDesc[] =
|
||||
{
|
||||
GB_DECLARE("Crypt", 0), GB_VIRTUAL_CLASS(),
|
||||
|
||||
GB_STATIC_METHOD("_call", "s", CCRYPT_call_md5, "(Password)s[(Prefix)s]"),
|
||||
GB_STATIC_METHOD("Check", "b", CCRYPT_check, "(Password)s(Crypt)s"),
|
||||
GB_STATIC_METHOD("MD5", "s", CCRYPT_call_md5, "(Password)s[(Prefix)s]"),
|
||||
GB_STATIC_METHOD("DES", "s", CCRYPT_call_des, "(Password)s[(Prefix)s]"),
|
||||
GB_STATIC_METHOD("MD5", "s", CCRYPT_call_md5, "(Password)s[(Prefix)s]"),
|
||||
GB_STATIC_METHOD("SHA256", "s", CCRYPT_call_sha256, "(Password)s[(Prefix)s]"),
|
||||
GB_STATIC_METHOD("SHA512", "s", CCRYPT_call_sha512, "(Password)s[(Prefix)s]"),
|
||||
|
||||
GB_END_DECLARE
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue