You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by cloud-fan <gi...@git.apache.org> on 2016/02/01 01:40:48 UTC

[GitHub] spark pull request: [SPARK-12567][SQL] Add aes_{encrypt,decrypt} U...

Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10527#discussion_r51371194
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala ---
    @@ -453,3 +455,119 @@ case class Murmur3Hash(children: Seq[Expression], seed: Int) extends Expression
         }
       }
     }
    +
    +/**
    + * 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
    + * Jurisdiction Policy Files are installed. If either argument is NULL or the key length is
    + * not one of the permitted values, the return value is NULL.
    + */
    +@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 nullable: Boolean = true
    +
    +  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 = input2.asInstanceOf[Array[Byte]].length match {
    +      case 16 | 24 | 32 =>
    +        new SecretKeySpec(input2.asInstanceOf[Array[Byte]], 0,
    +          input2.asInstanceOf[Array[Byte]].length, "AES")
    +      case _ => null
    +    }
    +
    +    try {
    +      cipher.init(Cipher.ENCRYPT_MODE, secretKey)
    +      cipher.doFinal(input1.asInstanceOf[Array[Byte]], 0, input1.asInstanceOf[Array[Byte]].length)
    +    } catch {
    +      case e: GeneralSecurityException => null
    --- End diff --
    
    I checked some similar expressions, `Encode`/`Decode` will throw exception if its charset string parameter is invalid, `Sha2` will return null if its bit length parameter is invalid.
    
    For `AesEncrypt`, what we should do if its secret key parameter is invalid? @vectorijk are there any other errors will be thrown during execution?
    
    cc @rxin , what's the general rule for this kind of stuff?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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