Question

I have an application in which when I enter emailid,then emailid is converted into array of integers using md5 digest.

I have written the code for converting into array but the array is not getting generated in proper format. This is format which I need:

[2, -88, 14, -36, -128, -124, -32, -91, 0, 107, -41, -114, -118, 100, -45, 45];

but my code is not retiring in this format. This is my code:

static NSData* digest(NSData *data, unsigned char* (*cc_digest)(const void*, CC_LONG, unsigned char*), CC_LONG digestLength)
{
    unsigned char md[digestLength];
    (void)cc_digest([data bytes], [data length], md);
     NSData* data1 = [NSData dataWithBytes:(const void *)md length:sizeof(unsigned char)*digestLength];
    return [NSData dataWithBytes:md length:digestLength];

}

- (NSData *) md5
{
    NSString *str =@"mitalee.yadav@gmail.com";
    NSData *data = [str dataUsingEncoding:NSASCIIStringEncoding];
    return digest(data, CC_MD5, CC_MD5_DIGEST_LENGTH);
}

in my appdidfinishlaunching I'm doing this

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
 NSData *dat = [self md5];
    NSUInteger len = [dat length];
    Byte *byteData= (Byte*)malloc(len);
    [dat  getBytes:byteData length:len];

}

but this is returning bytes in this format

<02a80edc 8084e0a5 006bd78e 8a64d32d>

Was it helpful?

Solution

Loop your byte array and do follow on it..

int result[16];
for(int i = 0; i < 16; i++)
{
     Byte b = byteData[i];//0xDC;
     result[i] = (b & 0x80) > 0 ? b - 0xFF -1 : b;
}

This will convert byte into signed int. I am sure that there must be optimised way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top