I have one quick question... I am building a social media network site app and I need to hash the password NSString. How would I accomplish this? I have the password field on the app and would like to hash the string and encode it in SHA512 for the POST request. Thanks in advance, TechnologyGuy
Asked
Active
Viewed 6,218 times
4
-
possible duplicate of [How can I compute a SHA-2 (ideally SHA 256 or SHA 512) hash in iOS?](http://stackoverflow.com/questions/6228092/how-can-i-compute-a-sha-2-ideally-sha-256-or-sha-512-hash-in-ios) – duci9y Jul 29 '14 at 19:32
2 Answers
8
Already answered: hash a password string using SHA512 like C#
But here's the copy-pasted code:
#include <CommonCrypto/CommonDigest.h>
+ (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];
}
Or if you prefer a hashed output, try this:
+(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;
}
-
1I got this working by adding `(CC_LONG)` in front of all references to data.length. Is that correct? I think it is? Also, I found that I could add it to my AppDelegate implementation if I changed the `+(NSString...` to a `-(NSString...`. I'm kind of new with ObjC, so, is that good to do in this case? Anyway, it works for me. – Volomike Jan 29 '16 at 19:31
-
I didn't see in your routine how to add a salt. So, I did it a simple way by prepending a salt string (of characters) to the input string before calling `createSHA512`. – Volomike Jan 29 '16 at 19:32
-
In your bottom example data should be [NSData dataWithBytes:cstr length:strlen(string)]; It'd be better to use [string dataUsingEncoding:NSUTF8StringEncoding]; – mikeytdan May 26 '16 at 20:00
0
+ (NSData *)sha512:(NSData *)data {
unsigned char hash[CC_SHA512_DIGEST_LENGTH];
if ( CC_SHA512([data bytes], [data length], hash) ) {
NSData *sha1 = [NSData dataWithBytes:hash length:CC_SHA512_DIGEST_LENGTH];
return sha1;
}
return nil;
}
Put this in a category on NSData
, or use it whatever way you like, the code remains the same.
You should've researched your question. I found an answer in one google search.

duci9y
- 4,128
- 3
- 26
- 42
-
I have found other answers and did my research but the question still remains... How do I connect the password NSString to this method and how to I get the hashed SHA512 as a NSString? – TechnologyGuy Jul 30 '14 at 03:34
-
2
-