SM3加密udf
maven
```xml
<dependencies>
<!-- 配置日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--添加Hadoop的依赖-->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--添加hive依赖-->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>3.1.0</version>
</dependency>
<!--sm3,sm4加密算法-->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.66</version>
</dependency>
</dependencies>
```
java 代码
```java
import org.apache.hadoop.hive.ql.exec.UDF;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import java.io.UnsupportedEncodingException;
public class SM3Encrypt extends UDF {
public String evaluate(String paramStr) throws Exception {
String resultHexString = "";
try {
// 将字符串转换成byte数组
byte[] srcData = paramStr.getBytes("UTF-8");
// 调用SM3加密
byte[] hash = sm3Encrypt(srcData);
// 将返回的加密字节数组转换成16进制字符串
resultHexString = ByteUtils.toHexString(hash);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return resultHexString;
}
public static byte[] sm3Encrypt(byte[] srcData) {
SM3Digest digest = new SM3Digest();
digest.update(srcData, 0, srcData.length);
byte[] hash = new byte[digest.getDigestSize()];
digest.doFinal(hash, 0);
return hash;
}
}
```
注册udf
```shell
#1.上传jar包hiveSM3UDF-1.0-SNAPSHOT.jar到hdfs中/{上传目录}/
#2.hive中创建函数,(多个hiveserver2都需要重启)com.xxx.udf是包名
create function hiveSM3Encrypt as 'com.xxx.udf.SM3Encrypt' using jar 'hdfs:/{上传目录}/hiveSM3UDF-1.0-SNAPSHOT.jar';
#3.使用
select hiveSM3Encrypt(手机号) from table_name
```