安全哈希算法(Secure Hash Algorithm)主要適用于數字簽名標準 (Digital Signature Standard DSS)里面定義的數字簽名算法(Digital Signature Algorithm DSA)。對于長度小于2^64位的消息,SHA1會產生一個160位的消息摘要。當接收到消息的時候,這個消息摘要可以用來驗證數據的完整性。在傳輸的過程中,數據很可能會發生變化,那么這時候就會產生不同的消息摘要。 SHA1有如下特性:不可以從消息摘要中復原信息;兩個不同的消息不會產生同樣的消息摘要,(但會有1x10 ^ 48分之一的機率出現相同的消息摘要,一般使用時忽略)。
#import "AlgorithmManager.h"
#import <CommonCrypto/CommonCrypto.h>
@implementation AlgorithmManager
+ (NSString *)signWithSHA1:(NSString *)input{
const char *cStr = [input UTF8String];
unsigned char result[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(cStr, strlen(cStr), result);
NSString *str_SHA1 = [NSString stringWithFormat:
@"%02x%02x%02x%02x%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],
result[16], result[17], result[18], result[19]
];
return str_SHA1;
}
@end
舉個栗子:123456
結果:7c4a8d09ca3762af61e59520943dc26494f8941b
對照網站:http://tool.oschina.net/encrypt?type=2