pyspark.sql.functions.hmac#
- pyspark.sql.functions.hmac(key, message, algorithm=None)[source]#
Returns the keyed-hash message authentication code (HMAC) of message using key and the given hash algorithm. The result is returned as raw MAC bytes; wrap it with
hex()orbase64()for a textual value. The default algorithm is ‘SHA-256’.Added in version 4.3.0.
- Parameters:
- Returns:
ColumnA new column that contains the raw HMAC bytes.
Examples
Example 1: Compute the HMAC with the default SHA-256 algorithm.
>>> import pyspark.sql.functions as sf >>> df = spark.createDataFrame([("key", "message")], ["key", "message"]) >>> df.select(sf.hex(sf.hmac(df.key, df.message))).show(truncate=False) +----------------------------------------------------------------+ |hex(hmac(key, message, SHA-256)) | +----------------------------------------------------------------+ |6E9EF29B75FFFC5B7ABAE527D58FDADB2FE42E7219011976917343065F58ED4A| +----------------------------------------------------------------+
Example 2: Compute the HMAC with an explicit algorithm.
>>> import pyspark.sql.functions as sf >>> df = spark.createDataFrame([("key", "message")], ["key", "message"]) >>> df.select(sf.hex(sf.hmac(df.key, df.message, sf.lit("SHA-1")))).show(truncate=False) +----------------------------------------+ |hex(hmac(key, message, SHA-1)) | +----------------------------------------+ |2088DF74D5F2146B48146CAF4965377E9D0BE3A4| +----------------------------------------+