两个 NSData 的 Objective C SHA512 哈希 [英] Objective C SHA512 hash of two NSData

查看:87
本文介绍了两个 NSData 的 Objective C SHA512 哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 Java 代码,它使用盐计算字节数组的 SHA512 哈希:

Here is a Java code, which computes SHA512 hash of a byte array with salt:

private static String DIGEST_ALGORITHM = "SHA-512";

    public static byte[] getHash(final byte[] data, final byte[] salt) throws NoSuchAlgorithmException
{
    final MessageDigest md = MessageDigest.getInstance(DIGEST_ALGORITHM);
    md.reset();
    if (salt != null)
    {
        md.update(salt);
    }
    return md.digest(data);

在 Objective C 中,我使用这个算法来计算 NSData 的散列:

In Objective C, I use this algorithm for compute the hash of an NSData:

@implementation NSData (CommonDigest)

- (NSData *) SHA512Hash {
unsigned char hash[CC_SHA512_DIGEST_LENGTH];
(void) CC_SHA512( [self bytes], (CC_LONG)[self length], hash );
return ( [NSData dataWithBytes: hash length: CC_SHA512_DIGEST_LENGTH] );
}

如果我使用相同的单个数据(即 Java 代码中的盐为 nil),这可以完美地计算与 Java 代码相同的哈希值.问题是,如果我想计算两个 NSData 的哈希值,即有一个 salt(Java 代码中的第二个参数不是 nil).可以看到,在Java代码中,如果salt不为null,则执行更新,然后调用digest方法.我在某处读到,此操作等于合并两个字节数组(数据和盐数组与 System.arraycopy),并在结果数组上调用摘要.但是,如果我在 Objective C 中执行此操作(使用 NSMutableData appendData 方法),则不会得到相同的结果.我怎样才能解决这个问题?可以看到在CommonDigest类中,也有类似的方法,但是不知道怎么用这些……我想到了这些方法:

This works perfectly, computes the same hash, as the Java code, if I use the same single data (i.e. the salt is nil in the Java code). The problem is that, if I want to computes hash of two NSData, i.e. there is a salt (the second parameter in the Java code is not nil). You can see that in the Java code, if the salt is not null, it performs an update, and then call the digest method. Somewhere I read that, this operation is equal with merging the two byte array (the data and salt arrays with System.arraycopy), and call the digest on the result array. However, if I do this in Objective C (with NSMutableData appendData method), I don't get the same result. How can I fix this? I can see in the CommonDigest class, there are similar methods, but I don't know, how can I use these...I think of these methods:

extern int CC_SHA512_Init(CC_SHA512_CTX *c);
extern int CC_SHA512_Update(CC_SHA512_CTX *c, const void *data, CC_LONG len);
extern int CC_SHA512_Final(unsigned char *md, CC_SHA512_CTX *c);
extern unsigned char *CC_SHA512(const void *data, CC_LONG len, unsigned char *md);

所以我想创建一个这样的方法:

So I would like to create a method like this:

@implementation NSData (CommonDigest)

- (NSData *)SHA512HashWithSalt:(NSData *)salt {...}

推荐答案

我还没有运行这段代码并将其与 Java 实现进行比较,但它应该可以工作:

I haven’t run this code and compared it with a Java implementation but it should work:

@implementation NSData (CommonDigest)

- (NSData *)SHA512HashWithSalt:(NSData *)salt {
    unsigned char hash[CC_SHA512_DIGEST_LENGTH];
    CC_SHA512_CTX context;
    CC_SHA512_Init(&context);
    if ([salt length]) {
        CC_SHA512_Update(&context, [salt bytes], (CC_LONG)[salt length]);
    }
    CC_SHA512_Update(&context, [self bytes], (CC_LONG)[self length]);
    CC_SHA512_Final(hash, &context);
    return [NSData dataWithBytes:hash length:CC_SHA512_DIGEST_LENGTH];
}

@end

这篇关于两个 NSData 的 Objective C SHA512 哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆