繁体   English   中英

使用xxhash算法编写MessageDirect

[英]Write a MessageDirect using the xxhash algorithm

对于以下问题,我将不胜感激。

我正在处理的应用程序使用SHA-256算法生成哈希值。

MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");

这给我们带来了性能问题,因此有人被要求用xxhash函数代替它。 (我们的主要关注点是拥有一个良好且快速的哈希算法。)

我们已经确定以下组成部分,我们认为这是一个良好而有效的实施方案。

http://mvnrepository.com/artifact/net.jpountz.lz4/lz4/1.0.0

这里给出了如何使用人工制品的示例。 https://github.com/jpountz/lz4-java

XXHashFactory factory = XXHashFactory.fastestInstance();

byte[] data = "12345345234572".getBytes("UTF-8");
ByteArrayInputStream in = new ByteArrayInputStream(data);

int seed = 0x9747b28c; // used to initialize the hash value, use whatever
                   // value you want, but always the same
StreamingXXHash32 hash32 = factory.newStreamingHash32(seed);
byte[] buf = new byte[8]; // for real-world usage, use a larger buffer, like 8192 bytes
for (;;) {
  int read = in.read(buf);
  if (read == -1) {
    break;
  }
  hash32.update(buf, 0, read);
}
int hash = hash32.getValue();

为了关闭SHA-256 MessageDigest,我认为最好的做法是扩展java.security.MessageDigest抽象类。 下面显示了我认为合适的实现,但缺少engineDigest()的实现。 任何人都可以就如何实现此方法提供指导,或者指出他们认为更好的方法吗? 提前致谢。

package com.company.functions.crypto;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

import net.jpountz.xxhash.StreamingXXHash32;
import net.jpountz.xxhash.XXHashFactory;

public class XxHashMessageDigest extends MessageDigest {

public static final String XXHASH = "xxhash";
private static final int SEED = 0x9747b28c;
private static final XXHashFactory FACTORY = XXHashFactory
        .fastestInstance();
private List<Integer> values = new ArrayList<Integer>();

protected XxHashMessageDigest() {
    super(XXHASH);
}

@Override
protected void engineUpdate(byte input) {
    // intentionally no implementation
}

@Override
protected void engineUpdate(byte[] input, int offset, int len) {

    StreamingXXHash32 hash32 = FACTORY.newStreamingHash32(SEED);
    hash32.update(input, offset, len);
    values.add(hash32.getValue());
}

@Override
protected byte[] engineDigest() {

    /*
     * TODO provide implementation to "digest" the list of values into a
     * hash value
     */

    engineReset();

    return null;
}

@Override
protected void engineReset() {
    values.clear();
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM