Skip to content

Instantly share code, notes, and snippets.

@canaksoy
Created March 9, 2017 20:20
Show Gist options
  • Save canaksoy/1dfc0b9b6c35e5e2b85a235bf071fbce to your computer and use it in GitHub Desktop.
Save canaksoy/1dfc0b9b6c35e5e2b85a235bf071fbce to your computer and use it in GitHub Desktop.
encrParameters
//parameters
NSMutableArray *keys = [[NSMutableArray alloc] init];
for (NSString * key in parameters)
{
[keys addObject:key];
}
[keys sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSMutableString * stringParams = [[NSMutableString alloc] initWithString:@""];
for (NSString * val in keys)
{
[stringParams appendString:[self md5:parameters[val]]];
}
[stringParams appendString:[self md5:@"YOURSECRETKEY"]];
NSMutableDictionary *newParams = [parameters mutableCopy];
[newParams setObject:[self sha256HashFor:stringParams] forKey:@"encr"];
- (NSString *) md5:(NSString *) input
{
const char *cStr = [input UTF8String];
unsigned char digest[16];
CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
return output;
}
-(NSString*)sha256HashFor:(NSString*)input
{
const char* str = [input UTF8String];
unsigned char result[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(str, (CC_LONG)strlen(str), result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++)
{
[ret appendFormat:@"%02x",result[i]];
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment