如何在 iOS 中将 WSSE 与 salted sha512 一起使用超过 1 次迭代

How to use WSSE in iOS with salted sha512 with more than 1 iteration

我需要一些关于 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?

对于 SHA512,请尝试以下操作:

#import <CommonCrypto/CommonDigest.h>
    + (NSData *)sha512:(NSData *)data {
    unsigned char hash[CC_SHA512_DIGEST_LENGTH];
    if ( CC_SHA512([data bytes], [data length], hash) ) {
        NSData *sha512 = [NSData dataWithBytes:hash length:CC_SHA512_DIGEST_LENGTH];        
        return sha512;
        }
        return nil;
    }

WSSE headers 查看 https://github.com/laiso/CocoaWSSE

如果你想用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>