自定义hive国密3-SM3非对称加密函数

引入依赖:

    <dependencies>
        
        <dependency>
            <groupId>org.apache.hadoopgroupId>
            <artifactId>hadoop-clientartifactId>
            <version>2.2.0version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>org.apache.hivegroupId>
            <artifactId>hive-execartifactId>
            <version>2.2.0version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.24version>
        dependency>
        
        <dependency>
            <groupId>org.bouncycastlegroupId>
            <artifactId>bcprov-jdk15to18artifactId>
            <version>1.75version>
        dependency>
    dependencies>
   <build>
        <plugins>
            
            
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-shade-pluginartifactId>
                <version>3.2.1version>
                <executions>
                    
                    <execution>
                        <phase>packagephase>
                        <goals>
                            <goal>shadegoal>
                        goals>
                        <configuration>
                            <artifactSet>
                                <excludes>
                                    <exclude>org.apache.flink:force-shadingexclude>
                                    <exclude>com.google.code.findbugs:jsr305exclude>
                                    <exclude>org.slf4j:*exclude>
                                    <exclude>log4j:*exclude>
                                excludes>
                            artifactSet>
                            <filters>
                                <filter>
                                    
                                    <artifact>*:*artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SFexclude>
                                        <exclude>META-INF/*.DSAexclude>
                                        <exclude>META-INF/*.RSAexclude>
                                    excludes>
                                filter>
                            filters>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.pony.bdp.SM3mainClass>
                                transformer>
                            transformers>
                            
                            <relocations>
                                <relocation>
                                    <pattern>com.pony.statepattern>
                                    <shadedPattern>com.shade.kafkashadedPattern>
                                    <excludes>
                                        <exclude>com.pony.state.KeyedStateFunctionexclude>
                                        <exclude>com.pony.state.StreamingWithKafka*exclude>
                                    excludes>
                                relocation>
                            relocations>
                            
                            <shadedArtifactAttached>trueshadedArtifactAttached>
                            <shadedClassifierName>pony-shadeshadedClassifierName>
                        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"));
    }

}

你可能感兴趣的:(hive,hive,hadoop,数据仓库)