Github user rxin commented on a diff in the pull request:
https://github.com/apache/spark/pull/10527#discussion_r53546060
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala
---
@@ -442,3 +444,90 @@ case class PrintToStderr(child: Expression) extends
UnaryExpression {
""".stripMargin)
}
}
+
+/**
+ * A function that encrypts input using AES. Key lengths of 128, 192 or
256 bits can be used. 192
+ * and 256 bits keys can be used if Java Cryptography Extension (JCE)
Unlimited Strength Jurisdic-
+ * tion Policy Files are installed. If either argument is NULL, the result
will also be null. If
+ * input is invalid, key length is not one of the permitted values or
using 192/256 bits key before
+ * installing JCE, an exception will be thrown.
+ */
+@ExpressionDescription(
+ usage = "_FUNC_(input, key) - Encrypts input using AES.",
+ extended = "> SELECT Base64(_FUNC_('ABC', '1234567890123456'));\n
'y6Ss+zCYObpCbgfWfyNWTw=='")
+case class AesEncrypt(left: Expression, right: Expression)
+ extends BinaryExpression with ImplicitCastInputTypes {
+
+ override def dataType: DataType = BinaryType
+ override def inputTypes: Seq[DataType] = Seq(BinaryType, BinaryType)
+
+ protected override def nullSafeEval(input1: Any, input2: Any): Any = {
+ val cipher = Cipher.getInstance("AES")
+ val secretKey: SecretKeySpec = new
SecretKeySpec(input2.asInstanceOf[Array[Byte]], 0,
+ input2.asInstanceOf[Array[Byte]].length, "AES")
+ cipher.init(Cipher.ENCRYPT_MODE, secretKey)
+ cipher.doFinal(input1.asInstanceOf[Array[Byte]], 0,
input1.asInstanceOf[Array[Byte]].length)
+ }
+
+ override def genCode(ctx: CodegenContext, ev: ExprCode): String = {
+ nullSafeCodeGen(ctx, ev, (str, key) => {
+ val Cipher = "javax.crypto.Cipher"
+ val SecretKeySpec = "javax.crypto.spec.SecretKeySpec"
+ s"""
+ try {
+ $Cipher cipher = $Cipher.getInstance("AES");
+ $SecretKeySpec secret = new $SecretKeySpec($key, 0,
$key.length, "AES");
+ cipher.init($Cipher.ENCRYPT_MODE, secret);
+ ${ev.value} = cipher.doFinal($str, 0, $str.length);
+ } catch (java.security.GeneralSecurityException e) {
+ org.apache.spark.unsafe.Platform.throwException(e);
+ }
+ """
+ })
+ }
+}
+
+/**
+ * A function that decrypts input using AES. Key lengths of 128, 192 or
256 bits can be used. 192
+ * and 256 bits keys can be used if Java Cryptography Extension (JCE)
Unlimited Strength Jurisdic-
+ * tion Policy Files are installed. If either argument is NULL, the result
will also be null. If
+ * input is invalid, key length is not one of the permitted values or
using 192/256 bits key before
+ * installing JCE, an exception will be thrown.
+ */
+@ExpressionDescription(
+ usage = "_FUNC_(input, key) - Decrypts input using AES.",
+ extended = "> SELECT
_FUNC_(UnBase64('y6Ss+zCYObpCbgfWfyNWTw=='),'1234567890123456');\n 'ABC'")
+case class AesDecrypt(left: Expression, right: Expression)
--- End diff --
i find it confusing to have left/right here (e.g. input, key)
Let's give them a proper name, and then just `override def left: Expression
= input`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]