diff --git a/datacooker-commons/src/main/java/io/github/pastorgl/datacooker/commons/functions/StringFunctions.java b/datacooker-commons/src/main/java/io/github/pastorgl/datacooker/commons/functions/StringFunctions.java index 7c75525..0ec5331 100644 --- a/datacooker-commons/src/main/java/io/github/pastorgl/datacooker/commons/functions/StringFunctions.java +++ b/datacooker-commons/src/main/java/io/github/pastorgl/datacooker/commons/functions/StringFunctions.java @@ -11,6 +11,7 @@ import io.github.pastorgl.datacooker.scripting.Function.Ternary; import io.github.pastorgl.datacooker.scripting.Function.Unary; +import java.util.Base64; import java.util.Deque; import java.util.Locale; @@ -202,4 +203,38 @@ public String descr() { " See Java Formatter class for complete reference"; } } + + public static class DECODE64 extends Unary { + @Override + public String call(Deque args) { + return new String(Base64.getDecoder().decode(Evaluator.popString(args))); + } + + @Override + public String name() { + return "STR_DECODE64"; + } + + @Override + public String descr() { + return "Decode a BASE64 encoded string"; + } + } + + public static class ENCODE64 extends Unary { + @Override + public String call(Deque args) { + return Base64.getEncoder().encodeToString(Evaluator.popBytes(args)); + } + + @Override + public String name() { + return "STR_BASE64"; + } + + @Override + public String descr() { + return "Encode stringified Object or byte array to a BASE64 encoded string"; + } + } } diff --git a/datacooker-commons/src/main/java/io/github/pastorgl/datacooker/scripting/Evaluator.java b/datacooker-commons/src/main/java/io/github/pastorgl/datacooker/scripting/Evaluator.java index 3223238..3a25b93 100644 --- a/datacooker-commons/src/main/java/io/github/pastorgl/datacooker/scripting/Evaluator.java +++ b/datacooker-commons/src/main/java/io/github/pastorgl/datacooker/scripting/Evaluator.java @@ -18,6 +18,15 @@ static String popString(Deque args) { return String.valueOf(a); } + static byte[] popBytes(Deque args) { + Object a = args.pop(); + if (!(a instanceof byte[])) { + return String.valueOf(a).getBytes(); + } + + return (byte[]) a; + } + static Number popNumeric(Deque args) { Object a = args.pop(); if (a instanceof Number) {