需要Java等价物SHA512 for Objective-C

时间:2012-03-13 07:00:02

标签: objective-c

因为我只是这个领域的初学者,这个问题可能看起来很微不足道,但请原谅。我有一个Java代码如下:

String passwordSalt = "somesalt";
       byte[] bsalt = base64ToByte(passwordSalt);
       byte[] thePasswordToDigestAsBytes = ("somepassword").getBytes("UTF-8");
       System.out.println("------------------------------"+passwordSalt);
       MessageDigest digest = MessageDigest.getInstance("SHA-512");
        digest.reset();
        digest.update(bsalt);
        byte[] input = digest.digest(thePasswordToDigestAsBytes);
        System.out.println("------------------------------"+byteToBase64(input));

我想在Objective-C中实现相同的目标,我正在使用以下代码:

NSData *saltdata = [Base64 decode:@"some base64 encoded salt"];
NSString *passwordDgst;
passwordDgst = @"somepassword";
NSData *input = [passwordDgst dataUsingEncoding: NSUTF8StringEncoding];
    unsigned char hash[CC_SHA512_DIGEST_LENGTH];
    CC_SHA512_CTX context;
    CC_SHA512_Init(&context);
    CC_SHA512_Update(&context, [saltdata bytes], (CC_LONG)[saltdata length]);
    CC_SHA512_Update(&context, [input bytes], (CC_LONG)[input length]);
    CC_SHA512_Final(hash, &context);
    input = [NSMutableData dataWithBytes:(const void *)hash    length:sizeof(unsigned char)*CC_SHA512_DIGEST_LENGTH];
    passwordDgst = [input encodeBase64WithNewlines:NO];

但这似乎生成了与Java代码不同的哈希值?这是为什么?任何人都能澄清一下吗?在此先感谢:)

0 个答案:

没有答案