How can I have an NSString hash for SHA512

I have one quick question ... I am creating an application for a network site on social networks and I need an NSString hash password. How would I do that? I have a password field in the application and would like a hash string and encode it in SHA512 for a POST request. Thanks in advance, TechnologyGuy

+3
source share
2 answers

Already answered: hash password string using SHA512 as C #

But here is the copied 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 hashed output, try the following:

 +(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; } 
+6
source
 + (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 however you like, the code remains the same.

You should research your question. I found the answer in one google search.

0
source

Source: https://habr.com/ru/post/987363/


All Articles