Skip to content

Commit 7a8b8e5

Browse files
Added support of MessageDigest hashing
1 parent 9534dd1 commit 7a8b8e5

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package YASL.Hashing;
2+
3+
import java.nio.ByteBuffer;
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
6+
7+
public class CMessageDigestHashing implements IHashingAlgorithm<byte[]> {
8+
private final MessageDigest _digest;
9+
10+
public CMessageDigestHashing(MessageDigest _digest) {
11+
this._digest = _digest;
12+
}
13+
14+
public CMessageDigestHashing(String algo) throws NoSuchAlgorithmException {
15+
this(MessageDigest.getInstance(algo));
16+
}
17+
18+
@Override
19+
public void hash(byte[] value, ByteBuffer res) {
20+
res.put(_digest.digest(value));
21+
}
22+
}

src/YASL/Hashing/ChgCombined.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
import java.nio.ByteBuffer;
44

55
public class ChgCombined<T> implements IHashingGenerator<T> {
6-
public interface ISalter<T> {
7-
public T apply(T value, int round);
8-
}
9-
106
private final IHashingAlgorithm<T>[] _hashes;
117
private final ISalter<T> _salting;
128

src/YASL/Hashing/ChgString.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package YASL.Hashing;
2+
3+
public class ChgString implements IHashingGenerator<String> {
4+
private final IHashingGenerator<byte[]> _gen;
5+
6+
@SafeVarargs
7+
public ChgString(IHashingAlgorithm<byte[]>... hashes) {
8+
this._gen = new ChgCombined<byte[]>( //
9+
ChgString::rotateBytes, //
10+
hashes //
11+
);
12+
}
13+
14+
private static byte[] rotateBytes(byte[] src, int round) {
15+
byte[] res = new byte[src.length];
16+
round %= src.length;
17+
System.arraycopy(src, 0, res, round, src.length - round);
18+
System.arraycopy(src, src.length - round, res, 0, round);
19+
return res;
20+
}
21+
22+
@Override
23+
public IHasher<String> generate(int range, int levels) {
24+
IHasher<byte[]> hashing = _gen.generate(range, levels);
25+
return x -> {
26+
return hashing.apply(x.getBytes());
27+
};
28+
}
29+
30+
}

src/YASL/Hashing/ISalter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package YASL.Hashing;
2+
3+
public interface ISalter<T> {
4+
public T apply(T value, int round);
5+
}

0 commit comments

Comments
 (0)