io.virtdata.libbasics.shared.conversions.from_bytebuffer.DigestToByteBuffer Maven / Gradle / Ivy
package io.virtdata.libbasics.shared.conversions.from_bytebuffer;
import io.virtdata.annotations.Categories;
import io.virtdata.annotations.Category;
import io.virtdata.annotations.ThreadSafeMapper;
import org.apache.commons.codec.digest.MessageDigestAlgorithms;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@Categories(Category.conversion)
@ThreadSafeMapper
public class DigestToByteBuffer implements Function {
private static ThreadLocal tl_state;
public DigestToByteBuffer(String digestType) {
for (String digestName : MessageDigestAlgorithms.values()) {
if (digestName.equals(digestType)) {
Supplier mds = () -> getDigest(digestName);
tl_state = ThreadLocal.withInitial(() -> new TL_State(mds));
break;
}
}
if (tl_state==null) {
tl_state = ThreadLocal.withInitial(() -> new TL_State(() -> getDigest(digestType)));
}
}
private static MessageDigest getDigest(String type) {
try {
return MessageDigest.getInstance(type);
} catch (Exception e) {
throw new RuntimeException("A digest of type " + type + " was not found. Select a digest type from: " +
Arrays.stream(MessageDigestAlgorithms.values()).collect(Collectors.joining(",", "[", "]")));
}
}
@Override
public ByteBuffer apply(ByteBuffer byteBuffer) {
TL_State state = tl_state.get();
byte[] digest = state.digest.digest(byteBuffer.array());
return ByteBuffer.wrap(digest);
}
private final static class TL_State {
private final MessageDigest digest;
private final ByteBuffer buf = ByteBuffer.allocate(Long.BYTES);
public TL_State(Supplier mds) {
digest = mds.get();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy