将 MD5 添加到 UITextField

发布于 2024-12-02 06:53:13 字数 682 浏览 1 评论 0 原文

抱歉,如果这是一个愚蠢的问题,但很难让它发挥作用!

我已经搜索了 hi 和 low,似乎下面的代码将生成一个 md5 哈希值,但我不确定如何让我的 2 个密码文本字段使用它来生成并发送到服务器。请指教我将不胜感激。

迈克

#import <CommonCrypto/CommonDigest.h>

- (NSString *)stringWithMD5Hash:(NSString *)inStr {
const char *cStr = [inStr UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), result );
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] ];

}

Sorry if this is a silly question but having difficulty in getting this working!!

I have searched hi and low and it seems the code below will generate an md5 hash but im not sure how to get my 2 password textfields to use it to generate to send to the server. Please advise I would be greatful thankful.

Mike

#import <CommonCrypto/CommonDigest.h>

- (NSString *)stringWithMD5Hash:(NSString *)inStr {
const char *cStr = [inStr UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), result );
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] ];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断念 2024-12-09 06:53:13

调用[self stringWithMD5Hash:yourTextField.text]。您可能希望函数的返回值小写,因为大多数服务器端语言都会生成带有小写字符的 MD5 哈希值。

Call [self stringWithMD5Hash:yourTextField.text]. You might want to lowerCase the return of the function as most server side languages generate MD5 hashes with lowercase characters.

一身软味 2024-12-09 06:53:13

非常简单假设该方法与您的文本字段位于同一类中,只需执行以下操作:

NSString *md5 = [self stringWithMD5Hash:textField.text]; 

Quite easy Assuming the method is in the same class as your textfield, just do this:

NSString *md5 = [self stringWithMD5Hash:textField.text]; 
套路撩心 2024-12-09 06:53:13

您应该将此代码放入 Category 中(请参阅 Apple 文档)将其添加到 NSString 类中。您可以将其添加为类

+ (NSString *)stringWithMD5Hash:(NSString *)inStr {
  const char *cStr = [inStr UTF8String];
  unsigned char result[CC_MD5_DIGEST_LENGTH];
  CC_MD5( cStr, strlen(cStr), result );
  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] ];
}

或实例方法

- (NSString *)MD5Hash {
  const char *cStr = [self UTF8String];
  unsigned char result[CC_MD5_DIGEST_LENGTH];
  CC_MD5( cStr, strlen(cStr), result );
  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] ];
}

然后您可以像 sudo rm -rf 所说的那样调用它:

NSString *md5 = [NSString stringWithMD5Hash:textField.text];

或者

NSString *md5 = [textField.text MD5Hash];

You should put this code into a Category (see the Apple documentation) which adds it to the NSString class. You can add it either as a class

+ (NSString *)stringWithMD5Hash:(NSString *)inStr {
  const char *cStr = [inStr UTF8String];
  unsigned char result[CC_MD5_DIGEST_LENGTH];
  CC_MD5( cStr, strlen(cStr), result );
  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] ];
}

or as an instance method

- (NSString *)MD5Hash {
  const char *cStr = [self UTF8String];
  unsigned char result[CC_MD5_DIGEST_LENGTH];
  CC_MD5( cStr, strlen(cStr), result );
  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] ];
}

Then you can call it just as sudo rm -rf said:

NSString *md5 = [NSString stringWithMD5Hash:textField.text];

or

NSString *md5 = [textField.text MD5Hash];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文