hash a password string using SHA512 like C#
-
I am developing logon function for my iPhone Application, so I want to hash the password using the SHA512 hashing algorithm then get the result as NSString (the result should be the same with SHA512 in C#). After spending a lot of time in the internet, I still not find out the solution yet! :( Is there anyone has the solution and sample code, please help me! Thanks a lot! [Update] In my C# code, the password is stored using SecureString, so maybe it's cause make different byte array between objective-c and C#
-
Answer:
This function will hash a string using SHA512. The resulting string is a hex representation of the hash: + (NSString *) createSHA512:(NSString *)source { const char *s = [source cStringUsingEncoding:NSASCIIStringEncoding]; NSData *keyData = [NSData dataWithBytes:s length:strlen(s)]; uint8_t digest[CC_SHA512_DIGEST_LENGTH] = {0}; CC_SHA512(keyData.bytes, keyData.length, digest); NSData *out = [NSData dataWithBytes:digest length:CC_SHA512_DIGEST_LENGTH]; return [out description]; } Don't forget to include the correct header: #include <CommonCrypto/CommonDigest.h>
Son Nguyen at Stack Overflow Visit the source
Other answers
I am using this one: It matches PHP SHA512 algorithm output. (NSString *)createSHA512:(NSString *)string { const char *cstr = [string cStringUsingEncoding:NSUTF8StringEncoding]; NSData *data = [NSData dataWithBytes:cstr length:string.length]; uint8_t digest[CC_SHA512_DIGEST_LENGTH]; CC_SHA512(data.bytes, data.length, digest); NSMutableString* output = [NSMutableString StringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2]; for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++) [output appendFormat:@"%02x", digest[i]]; return output; }
Martynas
Related Q & A:
- How to do Network discovery using UDP Broadcast C#?Best solution by Stack Overflow
- How to convert from string to char in C++?Best solution by Stack Overflow
- How to protect a video with a password?Best solution by Super User
- How do I put a password on a zip folder?Best solution by Yahoo! Answers
- How to search for a particular string in a text file using java?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.