aboutsummaryrefslogtreecommitdiff
path: root/sql/core/src
diff options
context:
space:
mode:
authorKai Jiang <jiangkai@gmail.com>2016-02-19 22:28:47 -0800
committerDavies Liu <davies.liu@gmail.com>2016-02-19 22:28:47 -0800
commit4f9a66481849dc867cf6592d53e0e9782361d20a (patch)
tree487652d799f1e98d3e02f544f5d72b0791c5750e /sql/core/src
parentec7a1d6e425509f2472c3ae9497c7da796ce8129 (diff)
downloadspark-4f9a66481849dc867cf6592d53e0e9782361d20a.tar.gz
spark-4f9a66481849dc867cf6592d53e0e9782361d20a.tar.bz2
spark-4f9a66481849dc867cf6592d53e0e9782361d20a.zip
[SPARK-12567] [SQL] Add aes_{encrypt,decrypt} UDFs
Author: Kai Jiang <jiangkai@gmail.com> Closes #10527 from vectorijk/spark-12567.
Diffstat (limited to 'sql/core/src')
-rw-r--r--sql/core/src/main/scala/org/apache/spark/sql/functions.scala36
-rw-r--r--sql/core/src/test/scala/org/apache/spark/sql/DataFrameFunctionsSuite.scala24
2 files changed, 60 insertions, 0 deletions
diff --git a/sql/core/src/main/scala/org/apache/spark/sql/functions.scala b/sql/core/src/main/scala/org/apache/spark/sql/functions.scala
index 97c6992e18..8da50bedfc 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/functions.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/functions.scala
@@ -1982,6 +1982,42 @@ object functions extends LegacyFunctions {
new Murmur3Hash(cols.map(_.expr))
}
+ /**
+ * Encrypts input using AES and Returns the result as a binary column.
+ * 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, 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.
+ *
+ * @param input binary column to encrypt input
+ * @param key binary column of 128, 192 or 256 bits key
+ *
+ * @group misc_funcs
+ * @since 2.0.0
+ */
+ def aes_encrypt(input: Column, key: Column): Column = withExpr {
+ AesEncrypt(input.expr, key.expr)
+ }
+
+ /**
+ * Decrypts input using AES and Returns the result as a string column.
+ * 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, 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.
+ *
+ * @param input binary column to decrypt input
+ * @param key binary column of 128, 192 or 256 bits key
+ *
+ * @group misc_funcs
+ * @since 2.0.0
+ */
+ def aes_decrypt(input: Column, key: Column): Column = withExpr {
+ AesDecrypt(input.expr, key.expr)
+ }
+
//////////////////////////////////////////////////////////////////////////////////////////////
// String functions
//////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/DataFrameFunctionsSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/DataFrameFunctionsSuite.scala
index aff9efe4b2..0381d57280 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/DataFrameFunctionsSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/DataFrameFunctionsSuite.scala
@@ -206,6 +206,30 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext {
Row(2743272264L, 2180413220L))
}
+ test("misc aes encrypt function") {
+ val df = Seq(("ABC", "1234567890123456")).toDF("input", "key")
+ checkAnswer(
+ df.select(base64(aes_encrypt($"input", $"key"))),
+ Row("y6Ss+zCYObpCbgfWfyNWTw==")
+ )
+ checkAnswer(
+ sql("SELECT base64(aes_encrypt('', '1234567890123456'))"),
+ Row("BQGHoM3lqYcsurCRq3PlUw==")
+ )
+ }
+
+ test("misc aes decrypt function") {
+ val df = Seq(("y6Ss+zCYObpCbgfWfyNWTw==", "1234567890123456")).toDF("input", "key")
+ checkAnswer(
+ df.select((aes_decrypt(unbase64($"input"), $"key"))),
+ Row("ABC")
+ )
+ checkAnswer(
+ sql("SELECT aes_decrypt(unbase64('BQGHoM3lqYcsurCRq3PlUw=='), '1234567890123456')"),
+ Row("")
+ )
+ }
+
test("string function find_in_set") {
val df = Seq(("abc,b,ab,c,def", "abc,b,ab,c,def")).toDF("a", "b")