package info.frostfs.sdk; import com.google.protobuf.ByteString; import com.google.protobuf.Message; import org.bouncycastle.crypto.digests.RIPEMD160Digest; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import static java.util.Objects.isNull; public class Helper { private static final String ERROR_MESSAGE = "Input value is missing"; private static final String SHA256 = "SHA-256"; private static final int RIPEMD_160_HASH_BYTE_LENGTH = 20; private Helper() { } public static byte[] getRipemd160(byte[] value) { if (isNull(value)) { throw new IllegalArgumentException(ERROR_MESSAGE); } var hash = new byte[RIPEMD_160_HASH_BYTE_LENGTH]; var digest = new RIPEMD160Digest(); digest.update(value, 0, value.length); digest.doFinal(hash, 0); return hash; } public static MessageDigest getSha256Instance() { try { return MessageDigest.getInstance(SHA256); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } public static byte[] getSha256(byte[] value) { if (isNull(value)) { throw new IllegalArgumentException(ERROR_MESSAGE); } return getSha256Instance().digest(value); } public static ByteString getSha256(Message value) { if (isNull(value)) { throw new IllegalArgumentException(ERROR_MESSAGE); } return ByteString.copyFrom(getSha256(value.toByteArray())); } public static String getHexString(byte[] value) { if (isNull(value) || value.length == 0) { throw new IllegalArgumentException(ERROR_MESSAGE); } return String.format("%0" + (value.length << 1) + "x", new BigInteger(1, value)); } }