Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an MD5 Hash of a string in Cocoa?

I know SHA-1 is preferred, but this project requires I use MD5.

#include <openssl/md5.h>  - (NSString*) MD5Hasher: (NSString*) query {     NSData* hashed = [query dataUsingEncoding:NSUTF8StringEncoding];     unsigned char *digest = MD5([hashed bytes], [hashed length], NULL);     NSString *final = [NSString stringWithUTF8String: (char *)digest];     return final; } 

I got this code from an answer to another similar question on StackOverflow, but I get the following error from GDB when the program breaks at return final;

(gdb) p digest $1 = (unsigned char *) 0xa06310e4 "\0206b\260/\336\316^\021\b\a/9\310\225\204" (gdb) po final Cannot access memory at address 0x0 (gdb) po digest  Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_INVALID_ADDRESS at address: 0xb0623630 0x98531ed7 in objc_msgSend () The program being debugged was signaled while in a function called from GDB. GDB has restored the context to what it was before the call. To change this behavior use "set unwindonsignal off" Evaluation of the expression containing the function (_NSPrintForDebugger) will be abandoned. 

I can't make any sense of it.

like image 725
demonslayer319 Avatar asked Jan 07 '10 06:01

demonslayer319


People also ask

How do you generate the MD5 hash of a string?

Call MessageDigest. getInstance("MD5") to get a MD5 instance of MessageDigest you can use. The compute the hash by doing one of: Feed the entire input as a byte[] and calculate the hash in one operation with md.

How long is MD5 hash string?

The hash size for the MD5 algorithm is 128 bits. The ComputeHash methods of the MD5 class return the hash as an array of 16 bytes. Note that some MD5 implementations produce a 32-character, hexadecimal-formatted hash.

How do I check if a string is an MD5?

You can check using the following function: function isValidMd5($md5 ='') { return preg_match('/^[a-f0-9]{32}$/', $md5); } echo isValidMd5('5d41402abc4b2a76b9719d911017c592'); The MD5 (Message-digest algorithm) Hash is typically expressed in text format as a 32 digit hexadecimal number.

What is MD5 algorithm in Java?

MD5 is a widely used cryptographic hash function, which produces a hash of 128 bit. In this article, we will see different approaches to create MD5 hashes using various Java libraries.


1 Answers

This is the category I use:

NSString+MD5.h

@interface NSString (MD5)  - (NSString *)MD5String;  @end 

NSString+MD5.m

#import <CommonCrypto/CommonDigest.h>  @implementation NSString (MD5)  - (NSString *)MD5String {     const char *cStr = [self UTF8String];     unsigned char result[CC_MD5_DIGEST_LENGTH];     CC_MD5( cStr, (CC_LONG)strlen(cStr), result );      return [NSString stringWithFormat:         @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",         result[0], result[1], result[2], result[3],          result[4], result[5], result[6], result[7],         result[8], result[9], result[10], result[11],         result[12], result[13], result[14], result[15]     ];   }  @end 

Usage

NSString *myString = @"test"; NSString *md5 = [myString MD5String]; // returns NSString of the MD5 of test 
like image 51
Grant Paul Avatar answered Sep 18 '22 19:09

Grant Paul