Support HMAC algorithm functions #470

This commit is contained in:
Francis Dong
2023-03-16 11:59:25 +08:00
committed by linwaiwai
parent 1c21dc97ae
commit 4629b24101
2 changed files with 40 additions and 1 deletions

View File

@@ -27,6 +27,8 @@ import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -46,7 +48,7 @@ public class CodecFunc implements IFunc {
private static final String CHARSET_UTF8 = "UTF-8";
private static final String IV = "12345678";
private static CodecFunc singleton;
public static CodecFunc getInstance() {
@@ -77,6 +79,12 @@ public class CodecFunc implements IFunc {
FuncExecutor.register(NAME_SPACE_PREFIX + "codec.aesDecrypt", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "codec.desEncrypt", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "codec.desDecrypt", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "codec.hmacMd5", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "codec.hmacSha1", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "codec.hmacSha224", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "codec.hmacSha256", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "codec.hmacSha384", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "codec.hmacSha512", this);
}
public String md5(String data) {
@@ -180,5 +188,29 @@ public class CodecFunc implements IFunc {
throw e;
}
}
public String hmacMd5(String secretKey, String data) {
return new HmacUtils(HmacAlgorithms.HMAC_MD5, secretKey).hmacHex(data);
}
public String hmacSha1(String secretKey, String data) {
return new HmacUtils(HmacAlgorithms.HMAC_SHA_1, secretKey).hmacHex(data);
}
public String hmacSha224(String secretKey, String data) {
return new HmacUtils(HmacAlgorithms.HMAC_SHA_224, secretKey).hmacHex(data);
}
public String hmacSha256(String secretKey, String data) {
return new HmacUtils(HmacAlgorithms.HMAC_SHA_256, secretKey).hmacHex(data);
}
public String hmacSha384(String secretKey, String data) {
return new HmacUtils(HmacAlgorithms.HMAC_SHA_384, secretKey).hmacHex(data);
}
public String hmacSha512(String secretKey, String data) {
return new HmacUtils(HmacAlgorithms.HMAC_SHA_512, secretKey).hmacHex(data);
}
}

View File

@@ -122,5 +122,12 @@ class CodecFuncTests {
Object result = FuncExecutor.getInstance().exec(null, funcExpression);
assertEquals("abc", result.toString());
}
@Test
void testHmacSha256() {
String funcExpression = "fn.codec.hmacSha256(\"635e8562b968bc05bb80cacf124ebd53285280ee6845df0000faa33acafc38f0\", \"12345678123456781234567812345678\")";
Object result = FuncExecutor.getInstance().exec(null, funcExpression);
assertEquals("c61be0237ec186df1c5f51425e607093b260a76e5de43a62cb3e821103303990", result.toString());
}
}