All pastes #989628 Raw Edit

Untitled

public text v1 · immutable
#989628 ·published 2008-04-19 15:47 UTC
rendered paste body
#include "algorithm.h"


/*char * KeyGen(int AppsId)
{
	static char * serial = 0;
	int a, b, c, d;
	
	if(!serial)
		serial = (char *)malloc(25);

	do
	{
		a = rand() % 100000;
		b = rand() % 100000;
		c = rand() % 100000;
		d = rand() % 100000;
	}
	while(((a - b) + (c - d)) != AppsId);
	
	sprintf(serial, "%05d-%05d-%05d-%05d", a, c, b, d);
	
	return serial;
}
*/

char * KeyGen(char* Appschar)
{
    static char * serial = NULL;
    static char * xbuffer = NULL;
    static char * mdstring = NULL;
    int a, b, c, d;
    int AppL, SerL;
    char k;
    md5_state_t state;
    md5_byte_t digest[16];

    if (!serial)
        serial = (char *)malloc(60);
    memset(serial, '\0', 0x60);

    if(!xbuffer)
        xbuffer = (char *)malloc(0x150);
    memset(xbuffer, '\0', 0x150);

    if(!mdstring)
        mdstring = (char *)malloc(35);
    memset(mdstring, '\0', 0x35);

    a = rand();
    b = rand() % 0x10000;
    c = rand() % 0x10000;
    d = rand() % 0x100;
    sprintf(serial,"%08X-%04X-%04X-%01X",a,b,c,d);
    SerL = strlen(serial);

    xbuffer[0] = '1';
    AppL = strlen(Appschar);

    for (k = 0 ; k < AppL ; k++)
    {
        if (k % 2 == 0)
        {
            xbuffer[k+1] = k;
        }
        else
        {
            xbuffer[k+1] = Appschar[k];
        }
    }

    for (k = 0 ; k < AppL ; k++)
    {
        if (k % 2 == 1)
        {
            xbuffer[k+1+AppL] = k;
        }
        else
        {
            xbuffer[k+1+AppL] = Appschar[k];
        }
    }
    strcat(xbuffer,"00");
    strcat(xbuffer,serial);
    strcat(xbuffer,Appschar);
    md5_init(&state);
    md5_append(&state, (const md5_byte_t *)xbuffer, strlen(xbuffer));
    md5_finish(&state, digest);
    for (k = 0 ; k < 16 ; k++)
        sprintf(mdstring + 2*k, "%02x", digest[k]);

    for (k = 0 ; k < 35 ; k++)
    {
        if (k % 8 != 0)
        {
            if (k % 2 != 0)
            {
                serial[SerL+k/2] = mdstring[k];
            }
        }
        else
        {
            serial[SerL+k/2] = '-';
            k++;
        }
    }
    return serial;
}