我正在尝试对哈希实现碰撞攻击(我正在访问“密码学”类(class))。因此,我有两个散列数组(= 字节序列 byte[]
)并且想找到两个数组中都存在的散列。经过一些研究和大量思考后,我确信单核机器上的最佳解决方案是 HashSet
。 (添加第一个数组的所有元素并通过 contains
检查第二个数组的元素是否已存在)。
但是,我想实现并发解决方案,因为我可以访问一台具有 8 个内核和 12 GB RAM 的机器。我能想到的最佳解决方案是 ConcurrentHashSet,它可以通过 Collections.newSetFromMap(new ConcurrentHashMap<A,B>())
创建.使用此数据结构,我可以并行添加第一个数组的所有元素,并且 - 在添加所有元素之后 - 我可以同时通过 contains
检查对于相同的哈希值。
所以我的问题是:您知道为这个确切问题设计的算法吗?如果没有,您是否有使用此类 ConcurrentHashSet 的经验,涉及问题和有效的运行时复杂性?或者你能推荐另一个可以帮助我的预建数据结构吗?
PS:如果有人对细节感兴趣:我打算使用Skandium并行化我的程序。
最佳答案
我认为使用任何形式的HashMap
都是浪费时间。我猜你正在计算各种数据的多字节散列,这些已经是散列
,没有必要对它们执行任何更多的散列。
虽然你没有说明,但我猜你的散列是 byte
序列。显然要么是trie或 dawg将是存储这些的理想选择。
因此我建议您实现一个 trie/dawg
并使用它来存储第一个数组中的所有哈希值。然后,您可以并行使用所有计算能力来查找此 trie
中第二个数组中的每个元素。不需要锁。
已添加
这是我拼凑的一个简单的 Dawg
实现。它似乎有效。
public class Dawg {
// All my children.
Dawg[] children = new Dawg[256];
// Am I a leaf.
boolean isLeaf = false;
// Add a new word.
public void add ( byte[] word ) {
// Finds its location, growing as necessary.
Dawg loc = find ( word, 0, true );
loc.isLeaf = true;
}
// String form.
public void add ( String word ) {
add(word.getBytes());
}
// Returns true if word is in the dawg.
public boolean contains ( byte [] word ) {
// Finds its location, no growing allowed.
Dawg d = find ( word, 0, false );
return d != null && d.isLeaf;
}
// String form.
public boolean contains ( String word ) {
return contains(word.getBytes());
}
// Find the Dawg - growing the tree as necessary if requested.
private Dawg find ( byte [] word, int i, boolean grow ) {
Dawg child = children[word[i]];
if ( child == null ) {
// Not present!
if ( grow ) {
// Grow the tree.
child = new Dawg();
children[word[i]] = child;
}
}
// Found it?
if ( child != null ) {
// More to find?
if ( i < word.length - 1 ) {
child = child.find(word, i+1, grow);
}
}
return child;
}
public static void main ( String[] args ) {
Dawg d = new Dawg();
d.add("H");
d.add("Hello");
d.add("World");
d.add("Hell");
System.out.println("Hello is "+(d.contains("Hello")?"in":"out"));
System.out.println("World is "+(d.contains("World")?"in":"out"));
System.out.println("Hell is "+(d.contains("Hell")?"in":"out"));
System.out.println("Hal is "+(d.contains("Hal")?"in":"out"));
System.out.println("Hel is "+(d.contains("Hel")?"in":"out"));
System.out.println("H is "+(d.contains("H")?"in":"out"));
}
}
已添加
这可能是并发无锁版本的良好开端。众所周知,这些东西很难测试,所以我不能保证它会起作用,但在我看来它肯定应该起作用。
import java.util.concurrent.atomic.AtomicReferenceArray;
public class LFDawg {
// All my children.
AtomicReferenceArray<LFDawg> children = new AtomicReferenceArray<LFDawg> ( 256 );
// Am I a leaf.
boolean isLeaf = false;
// Add a new word.
public void add ( byte[] word ) {
// Finds its location, growing as necessary.
LFDawg loc = find( word, 0, true );
loc.isLeaf = true;
}
// String form.
public void add ( String word ) {
add( word.getBytes() );
}
// Returns true if word is in the dawg.
public boolean contains ( byte[] word ) {
// Finds its location, no growing allowed.
LFDawg d = find( word, 0, false );
return d != null && d.isLeaf;
}
// String form.
public boolean contains ( String word ) {
return contains( word.getBytes() );
}
// Find the Dawg - growing the tree as necessary if requested.
private LFDawg find ( byte[] word, int i, boolean grow ) {
LFDawg child = children.get( word[i] );
if ( child == null ) {
// Not present!
if ( grow ) {
// Grow the tree.
child = new LFDawg();
if ( !children.compareAndSet( word[i], null, child ) ) {
// Someone else got there before me. Get the one they set.
child = children.get( word[i] );
}
}
}
// Found it?
if ( child != null ) {
// More to find?
if ( i < word.length - 1 ) {
child = child.find( word, i + 1, grow );
}
}
return child;
}
public static void main ( String[] args ) {
LFDawg d = new LFDawg();
d.add( "H" );
d.add( "Hello" );
d.add( "World" );
d.add( "Hell" );
System.out.println( "Hello is " + ( d.contains( "Hello" ) ? "in" : "out" ) );
System.out.println( "World is " + ( d.contains( "World" ) ? "in" : "out" ) );
System.out.println( "Hell is " + ( d.contains( "Hell" ) ? "in" : "out" ) );
System.out.println( "Hal is " + ( d.contains( "Hal" ) ? "in" : "out" ) );
System.out.println( "Hel is " + ( d.contains( "Hel" ) ? "in" : "out" ) );
System.out.println( "H is " + ( d.contains( "H" ) ? "in" : "out" ) );
}
}
关于java - 如何同时在两个数组中找到相同的 byte[]-objects?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8701130/