2011-05-20 18 views
0

在嘗試傳遞輸入的Textfield字符串以計算MD5時遇到了問題。使用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小時2011年5月21日=======

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 
+0

您是否在代碼中插入了斷點以縮小問題發生的位置?這應該是第一步。 – DougW 2011-05-20 22:53:06

+0

@DougW Thx您的回覆。實際上,我添加了一些斷點,結果如下:當我在代碼中給出一個字符串時,例如123,NSString * str將是0x35bc,123,而cStr char *將是0x582e5e0,123;但是當讓用戶使用textfield輸入123時,得到:str = 0xe24ed0,cStr = 0x4b56d00。在傳遞字符串的步驟看起來完全不同。但爲什麼呢? – dumbfingers 2011-05-20 23:03:09

回答

1

您尚未在plainText上定義IBOutlet。看起來連接不在那裏。您請求nil對象的text,因此錯誤的輸出。