You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/10/12 05:17:21 UTC

[GitHub] [spark] sarutak commented on a change in pull request #32801: [SPARK-12567][SQL] Add aes_encrypt and aes_decrypt builtin functions

sarutak commented on a change in pull request #32801:
URL: https://github.com/apache/spark/pull/32801#discussion_r726767859



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala
##########
@@ -301,3 +305,137 @@ case class CurrentUser() extends LeafExpression with Unevaluable {
   override def prettyName: String = "current_user"
   final override val nodePatterns: Seq[TreePattern] = Seq(CURRENT_LIKE)
 }
+
+/**
+ * The base implementation for AES encryption and decryption.
+ */
+abstract class AesBase(left: Expression, right: Expression)
+  extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant with Serializable {
+
+  override def dataType: DataType = BinaryType
+  override def nullable: Boolean = true
+  override def inputTypes: Seq[DataType] = Seq(BinaryType, BinaryType)
+  protected val cipherMode: Int
+
+  @transient lazy protected val cipher: Cipher = try {
+    Cipher.getInstance("AES")
+  } catch {
+    case e @ (_: NoSuchPaddingException | _: NoSuchAlgorithmException) =>
+      throw new RuntimeException(e)
+  }
+
+  protected override def nullSafeEval(input1: Any, input2: Any): Any = {
+    val input = input1.asInstanceOf[Array[Byte]]
+    val key = input2.asInstanceOf[Array[Byte]]
+    val inputLength = input.length
+    val keyLength = key.length
+    val secretKey = keyLength match {
+      case 16 | 24 | 32 => new SecretKeySpec(key, 0, keyLength, "AES")
+      case _ => null
+    }
+
+    if (secretKey == null) {
+      return null
+    }
+
+    try {
+      cipher.init(cipherMode, secretKey)
+      cipher.doFinal(input, 0, inputLength)
+    } catch {
+      case _: GeneralSecurityException =>
+        null
+    }
+  }
+
+  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {

Review comment:
       Seems better. I'll try it. Thank you.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org