GLBasic forum

Main forum => GLBasic - en => Topic started by: adaz on 2014-Jul-04

Title: Generating md5 hash
Post by: adaz on 2014-Jul-04
Hi All,

Is there a way to generate md5 hash from a string within GLBasic? I need a cross-platform solution (it has to work on iPhone too), so an inline code would be great!

Thank you ALL,

Adaz
Title: Re: Generating md5 hash
Post by: kanonet on 2014-Jul-05
Is there a special reason why you want to use the outdated and already broken MD5 algorithm? In case something from the more modern SHA-2 family will do the job too: www.glbasic.com/forum/index.php?topic=3833.msg73359#msg73359
Title: Re: Generating md5 hash
Post by: adaz on 2014-Jul-05
Thanks, but a 3rd-hosted php script uses MD5, and I have to provide md5 for them.
Title: Re: Generating md5 hash
Post by: MrTAToad on 2014-Jul-05
I had thought someone had done a routine for MD5, but unfortunately it looks like I was wrong -  no-one has done a routine.
Title: Re: Generating md5 hash
Post by: MrPlow on 2014-Jul-05
I think there is md5 in Android Extras but not sure about iOS
Title: Re: Generating md5 hash
Post by: spacefractal on 2014-Jul-06
Android Extras have a hash values (which is here all code in glbasic, and can been found in StoreKit). Howover its not a md5 string, but returns a value based on the string.

here is the functionused for storekit (which worked for both iOS and AE):
Code (glbasic) Select

INLINE
#define min(a,b) (a<b ? a : b)
ENDINLINE

INLINE
}
#ifdef WIN32
extern "C" __stdcall int SetWindowTextA(void*, const char*);
#endif
#if defined( LINUX) || defined (MACOSX)
extern "C" void SDL_WM_SetCaption  (const char *title, const char *icon);
#endif
namespace __GLBASIC__{
ENDINLINE


FUNCTION HashNumber%:text$
INLINE
long result,temp,counter;

result=0;
for (counter=0; counter<text_Str.len(); counter++)
{
result=(result<<4)+((char) text_Str[counter]);
temp=result & 0xF000;
if (temp)
{
result=result^(temp>>12);
}

result=(result & (temp^0xFFFF)) & 0xFFFF;

}

return result;
ENDINLINE
ENDFUNCTION

FUNCTION HashNumberBig%:text$
INLINE
long result,temp,counter;

result=0;
for (counter=0; counter<text_Str.len(); counter++)
{
result=(result<<6)+((char) text_Str[counter]);
temp=result & 0xF00000;
if (temp)
{
result=result^(temp>>24);
}

result=(result & (temp^0xFFFFFF)) & 0xFFFFFF;

}

return result;
ENDINLINE
ENDFUNCTION
Title: Re: Generating md5 hash
Post by: adaz on 2014-Jul-08
Thanks Guys, but I really needed an md5 function.

In the meantime I have developed a solution for iOS / Xcode, so I will answer my own question now - for others.
It works for me flawlessly!


Open your Xcode project, and insert this line in the beginning of inapp.mm:

Code (glbasic) Select

#import <CommonCrypto/CommonDigest.h>



Insert these lines after #pragma mark GLB:

Code (glbasic) Select

- (NSString *) _glb_md5:(NSString *) input {
    const char *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
   
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
   
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
   
    return  output;
}



Insert these lines after extern "C" {

Code (glbasic) Select

    char g_data[2048];
    char* glb_md5(char* text) {
    temp_string = [my_inapp _glb_md5: [NSString stringWithUTF8String:text]];
g_data[0]='\0';
strcpy(g_data, [temp_string UTF8String]);
return g_data;
    }



After that you can call this glb_md5 function from GLBasic - if you declare it in your GLBasic project:

Code (glbasic) Select

?IFDEF IPHONE
        IMPORT "C" char* glb_md5(const char* text)
?ELSE
        FUNCTION glb_md5: text                  //glb_md5 won't work on Windows!! That's an XCode function! So you need this dummy function to be able to run your app on Windows without md5.
        ENDFUNCTION
?ENDIF
Title: Re: Generating md5 hash
Post by: spacefractal on 2014-Jul-08
its will not been compatible with Android Extras, that is why im diddent do implement a real md5, but instead just returns a simple HASH integer value returned for each item if purchased or not, based on the internal SKU. So im cannot implement this one on my StoreKit, even its a nice code.
Title: Re: Generating md5 hash
Post by: MrTAToad on 2014-Jul-08
It should be fairly easy for someone to implement a "proper" version...