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