【发布时间】:2011-05-20 22:48:42
【问题描述】:
我在尝试传递 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 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_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
【问题讨论】:
-
您是否在代码中插入断点以缩小问题发生的范围?这应该是第一步。
-
@DougW 感谢您的回复。实际上,我已经添加了一些断点,结果如下:当我在代码中给出一个字符串时,比如 123,NSString str 将为 0x35bc,123,而 cStr char 将为 0x582e5e0, 123;但是当让用户使用 textfield 输入 123 时,得到:str=0xe24ed0, cStr=0x4b56d00。在传递字符串的步骤中似乎完全不同。但这是为什么呢?