1

我需要一些有关 iOS 中 WSSE 标头生成的帮助。它是用 Symfony2 编写的应用程序,它使用sha512算法和5000 次迭代,并且encode_as_base64为真。对于移动应用程序,我发现了这个用于编码密码的问题:SHA512 with salt for iOS尽管它只是一次迭代。使用包含最后一个循环的简单循环就足够了吗?

我们找到了 Android 生成 WSSE Headers 的代码:http: //obtao.com/blog/2013/09/how-to-use-wsse-in-android-app/可以在iOS 还是我们应该寻找另一种身份验证方式,例如 OAuth2?

4

2 回答 2

5

If you want to reproduce the same encryption as Symfony2 with the 5000 iterations, you can use the following code:

- (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];

}

Don't forgot to import CommonDigest.h:

#import <CommonCrypto/CommonDigest.h>
于 2015-02-13T08:13:08.083 回答
0

对于 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 标头,请查看https://github.com/laiso/CocoaWSSE

于 2015-01-16T11:55:33.170 回答