This is my first try at iOS development. That being said, I am trying to md5 a password
This is my .m file
#import "SMViewController.h"
#import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access
@interface SMViewController ()
@end
@implementation SMViewController
.........
.........
.........
- (IBAction)buttonLoginClicked:(id)sender {
@try {
if([[self.textEmail text] isEqualToString:@""] || [[self.textPassword text] isEqualToString:@""] ) {
[self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
} else {
/*String parameters = parametersData + "&controler=" + user + "&function=" + login + "&json=&client_type=android_client&client_id=" + GCMRegistrar.getRegistrationId(context) + "&ver="+VER;*/
NSString *post =[[NSString alloc] initWithFormat:@"email=%@&pass=%@&controler=user&function=login&json=&client_type=ip_client&client_id=x&ver=1321",[self.textEmail text],[self.textPassword text]];
…………………………….
………
……..
……
}
- (IBAction)backgroundClick:(id)sender {
[self.textPassword resignFirstResponder];
[self.textEmail resignFirstResponder];
}
@end
@implementation NSString (MyExtensions)
- (NSString *)md5
{
const char *cStr = [self UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), result ); // This is the md5 call
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
@implementation NSData (MyExtensions)
- (NSString*)md5
{
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( self.bytes, self.length, result ); // This is the md5 call
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
my .h file:
@interface NSString (MyExtensions)
- (NSString *)md5;
@end
@interface NSData (MyExtensions)
- (NSString*)md5;
@end
How can I pass the textPassword to the md5
this is based on MD5 algorithm in Objective C
Since you've just created a category on NSString and NSData, you can easily call it on a string (or data) like you call any other method (for example init
).
So in your case it should be like this:
[[self.textPassword text] md5]
(that will return a string containing the md5 of the textPassword.text
)
Tips for your code:
Categories are usually stored in their own separate file, like NSString+MD5.h
and NSString+MD5.m
, with additions specified after the +
sign, and then imported where needed.
Also, you should access properties through the dot syntax, eg. self.textPassword.text
instead of [self.textPassword text]
, or at least don't mix them (because textPassword
is a property of self
also - you could then do [self textPassword]
and it would be the same).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.