使用Textfield的输入值来计算MD5

时间:2011-05-20 22:48:42

标签: iphone md5 textfield

我在尝试传递Textfield的输入字符串以计算MD5时遇到了问题。

当我在代码中给出指定字符串(如abc)并进行MD5计算时,它将返回正确的结果。

当我尝试使用文本字段让用户输入相同的字符串abc时出现问题,然后我将textfield.text传递给md5函数来执行md5哈希。这一次,结果不同。

我对这个问题感到很困惑,并且已经被困在那里将近一个星期了,但却无法弄清楚为什么以及如何解决它。

你能帮帮我吗?

这是我的代码:

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

感谢您的帮助!

=====更新版本@ GMT + 1 0251小时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_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

1 个答案:

答案 0 :(得分:1)

您尚未在IBOutlet上定义plainText。看起来连接不存在。您正在请求text对象nil,因此输出不正确。