引入依赖:
<dependencies>
<!--添加Hadoop的依赖-->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.2.0</version>
<scope>provided</scope>
</dependency>
<!--添加hive依赖-->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>2.2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15to18 -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15to18</artifactId>
<version>1.75</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 使用 maven-shade打包依賴(推荐)-->
<!-- We use the maven-shade plugin to create a fat jar that contains all necessary dependencies. -->
<!-- Change the value of <mainClass>...</mainClass> if your program entry point changes. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>org.apache.flink:force-shading</exclude>
<exclude>com.google.code.findbugs:jsr305</exclude>
<exclude>org.slf4j:*</exclude>
<exclude>log4j:*</exclude>
</excludes>
</artifactSet>
<filters>
<filter>
<!-- Do not copy the signatures in the META-INF folder.
Otherwise, this might cause SecurityExceptions when using the JAR. -->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.pony.bdp.SM3</mainClass>
</transformer>
</transformers>
<!-- 将“com.pony.kafka”重命名为“com.shade.kafka"-->
<relocations>
<relocation>
<pattern>com.pony.state</pattern>
<shadedPattern>com.shade.kafka</shadedPattern>
<excludes>
<exclude>com.pony.state.KeyedStateFunction</exclude>
<exclude>com.pony.state.StreamingWithKafka*</exclude>
</excludes>
</relocation>
</relocations>
<!-- 修改包的后缀名-->
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>pony-shade</shadedClassifierName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
自定义函数:
package com.pony.bdp;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.bouncycastle.crypto.RuntimeCryptoException;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import java.security.Security;
/**
* @description:
* * hdfs dfs -put hive-udf-1.0-SNAPSHOT.jar hdfs://ns001/user/mart_001/udf/
* 持久函数
* * create function sm3 as 'com.pony.bdp.SM3' using jar 'hdfs://ns001/user/mart_001/udf/hive-udf-1.0-SNAPSHOT.jar';
*
* 临时函数
* * add jar hdfs://ns001/user/mart_001/udf/hive-udf-1.0-SNAPSHOT.jar;
* create temporary function sm3 as 'com.pony.bdp.SM3';
* @author pony
* @date 2023/7/12 15:22
* @version 1.0
*/
public class SM3 extends UDF {
public String evaluate(String str) throws Exception {
return encryptHexString(str);
}
/**
* 不带秘钥SM3加密
* *demo:
* * 17603065848
* * 0330c76b7e32932acfe5ba66c67cb60a1d20cdb45cb0336ead51a8be71ab9d5a
* * 18520807595
* * c33d33490be77ba98151332460ac095b82a54e4d47422c99296ae0508ba78b85
* @param src
* @return
*/
private static String encryptHexString(String src) {
byte[] bytes = src.getBytes();
SM3Digest digest = new SM3Digest();
digest.update(bytes, 0, bytes.length);
byte[] hash = new byte[digest.getDigestSize()];
digest.doFinal(hash, 0);
return ByteUtils.toHexString(hash);
}
/**
* 带秘钥SM3加密
* @param content
* @param secret
* @return
*/
public static String encrytSHA256(String content, String secret) {
try {
Security.addProvider(new BouncyCastleProvider());
SecretKey secretKey = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSm3");
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
byte[] digest = mac.doFinal(content.getBytes("UTF-8"));
return new HexBinaryAdapter().marshal(digest).toUpperCase();
} catch (Exception e) {
throw new RuntimeCryptoException("加密异常");
}
}
public static void main(String[] args) {
String s = "18520807595";
System.out.println(encryptHexString(s));
System.out.println(encrytSHA256(s, "aa"));
}
}