开发者

Using input value of Textfield to calculate MD5

I met a problem while trying to passing the inputed strings of Textfield to calculate MD5.

When I just give a specified string, like abc, in my code, and do the MD5 calculation, it will return a correct result.

Things went wrong when I try to using a textfield to let the user input the same string, abc, and then I pass the textfield.text to md5 function to do the md5 hash. This time, the result was different.

I am totally confused with this problem and have been got stuck at there for nearly a week, but just couldn't figure out why and how to solve it.

Could you please help me with that?

here's my code:

Hello_MD5ViewController.h

//
//  Hello_MD5ViewController.h
//  Hello-MD5
//
//

#import <UIKit/UIKit.h>

@interface Hello_MD5ViewController : UIViewController {
    UILabel *md5Text;
    UITextField *plainText;

}
@property (nonatomic, retain) IBOutlet UILabel *md5Text;
- (IBAction)buttonPressed: (id)sender;
@end

Hello_MD5ViewController.m

//
//  Hello_MD5ViewController.m
//  Hello-MD5
//

#import "Hello_MD5ViewController.h"
#import <CommonCrypto/CommonDigest.h> //Import for CC_MD5 access

NSString* md5(NSString *str)
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    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]
            ];
}


@implementation Hello_MD5ViewController
@synthesize md5Text;

- (IBAction)buttonPressed:(id)sender {
    NSString *input = [[NSString alloc] initWithString: plainText.text];
    NSString *digest = md5(input); //if I use textfield.text to passing the string, the result will be wrong
    //NSString *digest = md5(@"123"); //if I give a string within code like this, it'll return correct result
    NSString *md5Result = [[NSString alloc] initWithFormat:
                           @"MD5 RESULT \n%@", digest];
    md5Text.text = md5Result;
    //Calculate MD5 value
}

- (void)dealloc
{
    [plainText release];
    [md5Text release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.md5Text = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Thank you for your help!

=====UPDATED VERSION @ GMT+1 0251 hrs 05/21/2011=======

Hello_MD5ViewController.h

//  Hello-MD5
//
//

#import <UIKit/UIKit.h>

/*@interface NSString (md5Extension)
- (NSString *) md5;
@end

@interface NSData (md5Extension)
- (NSString *) md5;
@end
*/
@interface Hello_MD5ViewController : UIViewController {
    UILabel *md5Text;
    UITextField *plainText;

}
//@property (nonatomic, retain) NSString *input;
@property (nonatomic, retain) IBOutlet UILabel *md5Text;
- (IBAction)buttonPressed: (id)sender;
@property (nonatomic, retain) IBOutlet UITextField *plaintext;
@end

Hello_MD5ViewController.m

//  Hello-MD5


#import "Hello_MD5ViewController.h"
#import <CommonCrypto/CommonDigest.h> //Import for CC开发者_如何学Python_MD5 access

NSString* md5(NSString *str)
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    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]
            ];
}

@implementation Hello_MD5ViewController
@synthesize md5Text;
@synthesize plaintext;

- (IBAction)buttonPressed:(id)sender {
    //NSString *input = [[NSString alloc] initWithString: plainText.text];
    if(plainText.text == nil)
    {
        NSLog(@"Disconnected.");
    }
    //NSLog(@"Output %@",plainText.text);
    //NSString *digest = md5(input);
    //NSString *digest = md5(@"123");
    //NSString *md5Result = [[NSString alloc] initWithFormat:
                          // @"MD5 RESULT \n%@", digest];
    //md5Text.text = md5Result;
    //Calculate MD5 value
}

- (void)dealloc
{
    [plainText release];
    [md5Text release];
    //[input release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.md5Text = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end


You haven't defined an IBOutlet on plainText. It looks like the connection isn't there. You are requesting text of a nil object and hence the incorrect output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜