Support HMAC algorithm functions #470
This commit is contained in:
@@ -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;
|
||||
@@ -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) {
|
||||
@@ -181,4 +189,28 @@ public class CodecFunc implements IFunc {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -123,4 +123,11 @@ class CodecFuncTests {
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user