package com.eactive.eai.common.security.keyderiv.strategy;

import java.security.MessageDigest;
import java.util.Map;

import com.eactive.eai.common.security.keyderiv.DerivedKey;

/**
 * HSM 마스터키를 SHA-256 해싱으로 키를 도출하는 전략.
 * SHA-256 출력은 항상 32바이트(AES-256)이므로 별도 keyLength 파라미터가 불필요하다.
 *
 * key_deriv_params (JSON) 예시:
 * {
 *   "hsmKeyAlias" : "MASTER_KEY_AES"
 * }
 */
public class HsmSha256KeyDerivationStrategy extends BaseHsmKeyDerivationStrategy {

	@Override
	public DerivedKey deriveKey(Map<String, String> params, Map<String, String> runtimeContext) throws Exception {
		byte[] masterKeyBytes = super.getHsmKey(params);
		byte[] derived = MessageDigest.getInstance("SHA-256").digest(masterKeyBytes);
		return new DerivedKey(derived, derived);
	}

	@Override
	public String buildCacheKey(String cryptoName, Map<String, String> params, Map<String, String> runtimeContext) {
		return cryptoName;
	}
}
