自己试一下吧 常见的md5 sha1 base64 等常见的算法均有 后续有信的研究再做扩充
- //
- // NSString+Help.h
- //
- // Created by Mac10.9.4 on 14-9-21.
- // Copyright (c) 2014年 zn. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import <UIKit/UIKit.h>
- @interface NSString (Help)
- /**
- * 命令行测试命令
- *
- * MD5
- * $ echo -n abc | openssl md5
- * SHA1
- * $ echo -n abc | openssl sha1
- * SHA256
- * $ echo -n abc | openssl sha -sha256
- * SHA512
- * $ echo -n abc | openssl sha -sha512
- * BASE64编码(abc)
- * $ echo -n abc | base64
- *
- * BASE64解码(YWJj,abc的编码)
- * $ echo -n YWJj | base64 -D
- */
- /**
- * 返回md5加密后的字符串
- */
- @property (nonatomic, readonly) NSString *md5String;
- /**
- * 返回sha1遍吗后的字符串
- */
- @property (nonatomic, readonly) NSString *sha1String;
- /**
- * 返回sha256遍吗后的字符串
- */
- @property (nonatomic, readonly) NSString *sha256String;
- /**
- * 返回sha512遍吗后的字符串
- */
- @property (nonatomic, readonly) NSString *sha512String;
- /**
- * 返回Base64遍吗后的字符串
- */
- @property (nonatomic, readonly) NSString *base64Encode;
- /**
- * 返回Base64解码后的字符串
- */
- @property (nonatomic, readonly) NSString *base64Decode;
- /**
- * 计算一行字的size
- *
- * @param fount fount
- *
- * @return 计算好的 size
- */
- - (CGSize)sizeWithStingFount:(UIFont *)fount;
- /**
- * 计算字符串的size
- *
- * @param fount fount
- * @param maxSize 字符串可以占据的最大的size
- *
- * @return size
- *
- * @exception 计算好的 size
- */
- - (CGSize)sizeWithStingFount:(UIFont *)fount withMaxSize:(CGSize)maxSize;
- @end
- // NSString+Help.m
- //
- // Created by Mac10.9.4 on 14-9-21.
- // Copyright (c) 2014年 zn. All rights reserved.
- //
- #import "NSString+Help.h"
- #import <CommonCrypto/CommonDigest.h>
- @implementation NSString (Help)
- /**
- * 返回md5加密后的字符串
- */
- - (NSString *)md5String
- {
- const charchar *str = self.UTF8String;
- int length = (int)strlen(str);
- unsigned char bytes[CC_MD5_DIGEST_LENGTH];
- CC_MD5(str, length, bytes);
- return [self stringFromBytes:bytes length:CC_MD5_DIGEST_LENGTH];
- }
- /**
- * 返回sha1遍吗后的字符串
- */
- - (NSString *)sha1String
- {
- const charchar *str = self.UTF8String;
- int length = (int)strlen(str);
- unsigned char bytes[CC_SHA1_DIGEST_LENGTH];
- CC_SHA1(str, length, bytes);
- return [self stringFromBytes:bytes length:CC_SHA1_DIGEST_LENGTH];
- }
- /**
- * 返回sha256遍吗后的字符串
- */
- - (NSString *)sha256String
- {
- const charchar *str = self.UTF8String;
- int length = (int)strlen(str);
- unsigned char bytes[CC_SHA256_DIGEST_LENGTH];
- CC_SHA256(str, length, bytes);
- return [self stringFromBytes:bytes length:CC_SHA256_DIGEST_LENGTH];
- }
- /**
- * 返回sha512遍吗后的字符串
- */
- - (NSString *)sha512String
- {
- const charchar *str = self.UTF8String;
- int length = (int)strlen(str);
- unsigned char bytes[CC_SHA512_DIGEST_LENGTH];
- CC_SHA512(str, length, bytes);
- return [self stringFromBytes:bytes length:CC_SHA512_DIGEST_LENGTH];
- }
- - (NSString *)stringFromBytes:(unsigned charchar *)bytes length:(int)length
- {
- NSMutableString *strM = [NSMutableString string];
- for (int i = 0; i < length; i++) {
- [strM appendFormat:@"%02x", bytes[i]];
- }
- return [strM copy];
- }
- /**
- * 返回Base64遍吗后的字符串
- */
- - (NSString *)base64Encode {
- NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
- return [data base64EncodedStringWithOptions:0];
- }
- /**
- * 返回Base64解码后的字符串
- */
- - (NSString *)base64Decode {
- NSData *data = [[NSData alloc] initWithBase64EncodedString:self options:0];
- return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- }
- /**
- * 计算一行字的size
- *
- * @param fount fount
- *
- * @return 计算好的 size
- */
- - (CGSize)sizeWithStingFount:(UIFont *)fount
- {
- return [self sizeWithStingFount:fount withMaxSize:CGSizeMake(MAXFLOAT, 0)];
- }
- /**
- * 计算字符串的size
- *
- * @param fount fount
- * @param maxSize 字符串可以占据的最大的size
- *
- * @return size
- *
- * @exception 计算好的 size
- */
- - (CGSize)sizeWithStingFount:(UIFont *)fount withMaxSize:(CGSize)maxSize;
- {
- NSDictionary *attrs = @{NSFontAttributeName : fount};
- return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
- }
- @end