0
C#代码。使用SHA1在C#和IOS中加密
SHA1 hash = SHA1.Create();
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] combined = encoder.GetBytes(password);
hash.ComputeHash(combined);
passwordHash = Convert.ToBase64String(hash.Hash);
如何在IOS中获得相同的结果?请帮帮我。
到目前为止,我已经做了这么多,但结果比C#
NSString *password = @"XABCVKXMWJ"; // your password
CFIndex asciiLength;
// Determine length of converted data:
CFStringGetBytes((__bridge CFStringRef)(password), CFRangeMake(0, [password length]),
kCFStringEncodingASCII, '?', false, NULL, 0, &asciiLength);
// Allocate buffer:
uint8_t *asciiBuffer = malloc(asciiLength);
// Do the conversion:
CFStringGetBytes((__bridge CFStringRef)(password), CFRangeMake(0, [password length]),
kCFStringEncodingASCII, '?', false, asciiBuffer, asciiLength, NULL);
unsigned char hash[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(asciiBuffer, asciiLength, hash);
free(asciiBuffer);
NSData *result = [NSData dataWithBytes:hash length:CC_SHA1_DIGEST_LENGTH];
是不同的,结果我从C#代码得到的是 uSFCLAZZHkBVN7xViO3hKkhhR/S =
和IOS,它是 uSFCLAZZHkBVN7xViO3hKkhhR + s =
只是一个侧面说明:哈希是_not_加密! –
您可以在iOS上使用CommonCrypto库。 – Paulw11