Commit f102982c authored by lwh's avatar lwh

fix: AES UDF 密钥派生方式对齐 StarRocks 内置函数(null 填充到 16 字节)

- 密钥派生从 SHA-256 改为 null 填充(与 SR 内置 aes_encrypt 行为一致) - 新增双参数 eval(String, String) 支持 CDCSOURCE column.transform-key 配置 - 单参数 eval(String) 向后兼容,委托双参数版本使用 SECRET_KEY 常量 Co-Authored-By: 's avatarClaude Opus 4.8 (1M context) <noreply@anthropic.com>
parent d5f0a101
......@@ -5,7 +5,7 @@ import org.apache.flink.table.functions.ScalarFunction;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;
/**
......@@ -17,17 +17,21 @@ public class AesDecrypt extends ScalarFunction {
private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
// ★★★ 必须和 AesEncrypt 里的密钥一致 ★★★
private static final String SECRET_KEY = "your_secret_key_here";
private static final String SECRET_KEY = "my_secret_key";
public String eval(String encryptedValue) {
if (encryptedValue == null) {
return eval(encryptedValue, SECRET_KEY);
}
public String eval(String encryptedValue, String secretKey) {
if (encryptedValue == null || secretKey == null) {
return null;
}
try {
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] hash = sha256.digest(SECRET_KEY.getBytes(StandardCharsets.UTF_8));
// null-pad key to 16 bytes (AES-128), matching StarRocks built-in behavior
byte[] rawKey = secretKey.getBytes(StandardCharsets.UTF_8);
byte[] keyBytes = new byte[16];
System.arraycopy(hash, 0, keyBytes, 0, 16);
System.arraycopy(rawKey, 0, keyBytes, 0, Math.min(rawKey.length, 16));
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, keySpec);
......
......@@ -5,7 +5,7 @@ import org.apache.flink.table.functions.ScalarFunction;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;
/**
......@@ -17,17 +17,21 @@ public class AesEncrypt extends ScalarFunction {
private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
// ★★★ 在这里改你的密钥 ★★★
private static final String SECRET_KEY = "your_secret_key_here";
private static final String SECRET_KEY = "my_secret_key";
public String eval(String value) {
if (value == null) {
return eval(value, SECRET_KEY);
}
public String eval(String value, String secretKey) {
if (value == null || secretKey == null) {
return null;
}
try {
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] hash = sha256.digest(SECRET_KEY.getBytes(StandardCharsets.UTF_8));
// null-pad key to 16 bytes (AES-128), matching StarRocks built-in behavior
byte[] rawKey = secretKey.getBytes(StandardCharsets.UTF_8);
byte[] keyBytes = new byte[16];
System.arraycopy(hash, 0, keyBytes, 0, 16);
System.arraycopy(rawKey, 0, keyBytes, 0, Math.min(rawKey.length, 16));
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment