我需要一些有关 iOS 中 WSSE header 生成的帮助。该应用程序是用 Symfony2 编写的,它使用 sha512 算法,5000 次迭代 和 encode_as_base64 为真。对于移动应用程序,我发现了这个密码编码问题:SHA512 with salt for iOS虽然这只是一次迭代。使用包含最后一个循环的简单循环就足够了吗?
我们找到了 WSSE header 的 Android 生成代码:http://obtao.com/blog/2013/09/how-to-use-wsse-in-android-app/可以在 iOS 中做同样的事情,还是我们应该寻找另一种身份验证方式,比如 OAuth2?
最佳答案
如果你想通过 5000 次迭代重现与 Symfony2 相同的加密,你可以使用以下代码:
- (NSString *)hashPassword:(NSString *)password ansSalt:(NSString *)salt {
NSString *passwordSalted = [NSString stringWithFormat:@"%@{%@}",password,salt];
NSData *passwordData = [passwordSalted dataUsingEncoding:NSUTF8StringEncoding];
uint8_t hash[CC_SHA512_DIGEST_LENGTH];
CC_SHA512([passwordData bytes], [passwordData length], hash);
NSMutableData *allData = [[NSMutableData alloc] init];
[allData appendBytes:hash length:CC_SHA512_DIGEST_LENGTH];
for (NSInteger i = 1; i < 5000; i++) {
[allData appendBytes:[passwordData bytes] length:[passwordData length]];
uint8_t hashLoop[CC_SHA512_DIGEST_LENGTH];
CC_SHA512([allData bytes], [allData length], hashLoop);
[allData setLength:0];
[allData appendBytes:hashLoop length:CC_SHA512_DIGEST_LENGTH];
}
NSData *imageData = [NSData dataWithBytes:[allData bytes] length:[allData length]];
return [imageData base64EncodedStringWithOptions:0];
}
不要忘记导入 CommonDigest.h:
#import <CommonCrypto/CommonDigest.h>
关于android - 如何在 iOS 中使用 WSSE 和 salted sha512 超过 1 次迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27983034/