两个 NSData 的 Objective C SHA512 哈希
这是一个 Java 代码,它使用 salt 计算字节数组的 SHA512 哈希值:
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 的哈希值:
@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 代码相同的哈希值相同的单个数据(即 Java 代码中的 salt 为零)。问题是,如果我想计算两个 NSData 的哈希值,即有一个盐(Java 代码中的第二个参数不是 nil)。可以看到,在Java代码中,如果salt不为null,就会执行更新,然后调用digest方法。我在某处读到,此操作等同于合并两个字节数组(数据和盐数组与 System.arraycopy),并在结果数组上调用摘要。 但是,如果我在 Objective C 中执行此操作(使用 NSMutableData appendData 方法),则不会得到相同的结果。我该如何解决这个问题? 我可以在 CommonDigest 类中看到类似的方法,但我不知道,我该如何使用这些...我想到了这些方法:
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);
所以我想创建一个这样的方法:
@implementation NSData (CommonDigest)
- (NSData *)SHA512HashWithSalt:(NSData *)salt {...}
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);
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] );
}
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 {...}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有运行这段代码并将其与 Java 实现进行比较,但它应该可以工作:
I haven’t run this code and compared it with a Java implementation but it should work: