vinodkc commented on code in PR #57344:
URL: https://github.com/apache/spark/pull/57344#discussion_r3608757431
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala:
##########
@@ -2526,6 +2526,24 @@ private[sql] object QueryExecutionErrors extends
QueryErrorsBase with ExecutionE
"functionName" -> toSQLId("aes_encrypt")))
}
+ def hmacUnsupportedAlgorithmError(algorithm: String): RuntimeException = {
+ new SparkRuntimeException(
+ errorClass = "INVALID_PARAMETER_VALUE.HMAC_ALGORITHM",
+ messageParameters = Map(
+ "parameter" -> toSQLId("algorithm"),
+ "functionName" -> toSQLId("hmac"),
+ "algorithm" -> toSQLValue(algorithm, StringType)))
+ }
+
+ def hmacCryptoError(detailMessage: String): RuntimeException = {
+ new SparkRuntimeException(
+ errorClass = "INVALID_PARAMETER_VALUE.HMAC_CRYPTO_ERROR",
+ messageParameters = Map(
+ "parameter" -> (toSQLId("key") + ", " + toSQLId("message")),
Review Comment:
Thanks for the review comments.
Fixed.
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionImplUtilsSuite.scala:
##########
@@ -403,6 +403,65 @@ class ExpressionImplUtilsSuite extends SparkFunSuite {
// scalastyle:on nonascii
}
+ // RFC 4231 test case 1 (HMAC-SHA-224/256/384/512) and RFC 2202 test case 1
+ // (HMAC-SHA-1 / HMAC-MD5): key is 0x0b repeated, data is "Hi There".
+ private val hmacKey20 = Array.fill[Byte](20)(0x0b)
+ private val hmacKey16 = Array.fill[Byte](16)(0x0b)
+ private val hmacData = "Hi There".getBytes("UTF-8")
+
+ private def hmacHex(key: Array[Byte], message: Array[Byte], algorithm:
String): String = {
+ ExpressionImplUtils.hmac(key, message, UTF8String.fromString(algorithm))
+ .map(b => f"$b%02x").mkString
+ }
+
+ test("Hmac with supported algorithms") {
+ assert(hmacHex(hmacKey20, hmacData, "SHA-224") ==
+ "896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22")
+ assert(hmacHex(hmacKey20, hmacData, "SHA-256") ==
+ "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7")
+ assert(hmacHex(hmacKey20, hmacData, "SHA-384") ==
+ "afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59c" +
+ "faea9ea9076ede7f4af152e8b2fa9cb6")
+ assert(hmacHex(hmacKey20, hmacData, "SHA-512") ==
+ "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde" +
+ "daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854")
+ assert(hmacHex(hmacKey20, hmacData, "SHA-1") ==
+ "b617318655057264e28bc0b6fb378c8ef146be00")
+ assert(hmacHex(hmacKey16, hmacData, "MD5") ==
+ "9294727a3638bb1c13f48ef8158bfc9d")
+ }
+
+ test("Hmac algorithm name is case-insensitive and accepts the compact form")
{
+ val expected =
"b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"
+ assert(hmacHex(hmacKey20, hmacData, "sha-256") == expected)
+ assert(hmacHex(hmacKey20, hmacData, "SHA256") == expected)
+ }
+
+ test("Hmac unsupported algorithm error") {
+ checkError(
+ exception = intercept[SparkRuntimeException] {
+ ExpressionImplUtils.hmac(hmacKey20, hmacData,
UTF8String.fromString("SHA-3"))
+ },
+ condition = "INVALID_PARAMETER_VALUE.HMAC_ALGORITHM",
+ parameters = Map(
+ "parameter" -> "`algorithm`",
+ "functionName" -> "`hmac`",
+ "algorithm" -> "'SHA-3'"))
+ }
+
+ test("Hmac empty key error") {
+ checkError(
+ exception = intercept[SparkRuntimeException] {
+ ExpressionImplUtils.hmac(Array.empty[Byte], hmacData,
UTF8String.fromString("SHA-256"))
+ },
+ condition = "INVALID_PARAMETER_VALUE.HMAC_CRYPTO_ERROR",
+ parameters = Map(
+ "parameter" -> "`key`, `message`",
Review Comment:
Fixed
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]