Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/10527#discussion_r48918992
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala
---
@@ -221,3 +224,147 @@ 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. 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.""",
+ extended = """> SELECT Base64(_FUNC_('ABC', '1234567890123456'));
+ 'y6Ss+zCYObpCbgfWfyNWTw=='"""
+)
+case class AesEncrypt(left: Expression, right: Expression)
+ extends BinaryExpression with Serializable 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: InvalidKeyException => null
+ }
+ }
+
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ nullSafeCodeGen(ctx, ev, (str, key) => {
+ val Cipher = "javax.crypto.Cipher"
+ val SecretKeySpec = "javax.crypto.spec.SecretKeySpec"
+ s"""
+ try {
+ $Cipher cipher = $Cipher.getInstance("AES");
+ try {
+ if ($key.length == 16 || $key.length == 24 || $key.length ==
32) {
+ cipher.init($Cipher.ENCRYPT_MODE, new $SecretKeySpec($key,
0, $key.length, "AES"));
+ ${ev.value} = cipher.doFinal($str, 0, $str.length);
+ } else {
+ ${ev.isNull} = true;
+ }
+ } catch (java.security.InvalidKeyException e) {
+ org.apache.spark.unsafe.Platform.throwException(e);
+ } catch (javax.crypto.IllegalBlockSizeException e) {
+ org.apache.spark.unsafe.Platform.throwException(e);
+ } catch (javax.crypto.BadPaddingException e) {
+ org.apache.spark.unsafe.Platform.throwException(e);
+ }
+ } catch(java.security.NoSuchAlgorithmException e) {
+ org.apache.spark.unsafe.Platform.throwException(e);
+ } catch(javax.crypto.NoSuchPaddingException 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
+ * 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) - 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
+ 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.""",
+ extended =
+ """> SELECT
_FUNC_(UnBase64('y6Ss+zCYObpCbgfWfyNWTw=='),'1234567890123456');
+ 'ABC'"""
+)
+case class AesDecrypt(left: Expression, right: Expression)
+ extends BinaryExpression with Serializable with ImplicitCastInputTypes {
+
+ override def dataType: DataType = StringType
+ 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 = 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.DECRYPT_MODE, secretKey)
+ UTF8String.fromBytes(
+ cipher.doFinal(input1.asInstanceOf[Array[Byte]], 0,
+ input1.asInstanceOf[Array[Byte]].length))
+ } catch {
+ case e: InvalidKeyException => UTF8String.fromBytes(null)
--- End diff --
can we just use `null`?
---
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]