package tech.easyflow.datacenter.connector.support;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;

/**
 * 数据集写入回执键工具。
 */
public final class WriteReceiptSupport {

    private WriteReceiptSupport() {
    }

    /**
     * 为一行数据派生固定长度的稳定回执键。
     *
     * @param operationReceiptKey 整次写入的回执键
     * @param rowIndex 行序号
     * @return SHA-256 行回执键
     */
    public static String childKey(String operationReceiptKey, int rowIndex) {
        String value = operationReceiptKey + ':' + rowIndex;
        try {
            return HexFormat.of().formatHex(
                    MessageDigest.getInstance("SHA-256").digest(
                            value.getBytes(StandardCharsets.UTF_8)));
        } catch (NoSuchAlgorithmException error) {
            throw new IllegalStateException("SHA-256 is unavailable", error);
        }
    }
}
