Hive udf函数实现RSA使用

本文介绍了如何在Hive中使用自定义UDF(用户定义函数)实现RSA加密和解密操作,包括生成RSA密钥对、进行加解密以及在Hive中创建并使用相关函数的方法。
摘要由CSDN通过智能技术生成

1、生成RSA密钥对(公钥和私钥)

import sun.misc.BASE64Encoder;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;

private static final String ALGORITHM = "RSA";
private static final int KEYSIZE = 1024;
private static void generateKeyPair() throws Exception {
    //RSA算法要求有一个可信任的随机数源
    //SecureRandom secureRandom = new SecureRandom();
    // 为RSA算法创建一个KeyPairGenerator对象
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
    // 利用上面的随机数据源初始化这个KeyPairGenerator对象
    //keyPairGenerator.initialize(KEYSIZE, secureRandom);
    keyPairGenerator.initialize(KEYSIZE);
    // 生成密匙对
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    // 公钥
    Key publicKey = keyPair.getPublic();
    // 私钥
    Key privateKey = keyPair.getPrivate();
    String publicKeyBase64 = new BASE64Encoder().encode(publicKey.getEncoded());
    String privateKeyBase64 = new BASE64Encoder().encode(privateKey.getEncoded());
    System.out.println(publicKeyBase64);
    System.out.println(privateKeyBase64);
}

2、RSA加密

import org.apache.commons.codec.binary.Base64;
import org.apache.hadoop.hive.ql.exec.MapredContext;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * RSA加密
 */
public class RsaEncoder extends GenericUDF {

    private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //非对称密钥算法
    public static final String KEY_ALGORITHM = "RSA";

    // RSA 公钥
    private static String RasPublicKey = "RSA公钥用于加密";

    private Cipher cipher = null;

    @Override
    public void configure(MapredContext context) {
        System.out.println(new Date() + "######## configure");

        if(null != context) {
            //从jobConf中获取公钥
            String confPublicKey = context.getJobConf().get("com.howe.udf.rsa.publickey");

            if( confPublicKey != null && confPublicKey.length() > 0 ) {
                RasPublicKey = confPublicKey;
            }
        }
    }

    @Override
    public ObjectInspector initialize(ObjectInspector[] arguments) {

        //System.out.println(formatter.format(new Date()) + "######## initialize");

        try {
            //实例化密钥工厂
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
            //初始化公钥
            //密钥材料转换
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(RasPublicKey));
            //产生公钥
            PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);

            //数据加密
            cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);

            System.out.println(formatter.format(new Date()) + "######## The Initialization Of Cipher Has Completed Successfully");

        } catch (Exception e) {
            e.printStackTrace();
            cipher = null;
            System.out.println(formatter.format(new Date()) + "######## Error: Initialization Failed!");
        }

        return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING);
    }

    @Override
    public Object evaluate(DeferredObject[] args) throws HiveException {

        if( cipher == null ) return null;
        try {
            final String text = args[0].get().toString();

            if ( text != null && text.length() > 0)
                return Base64.encodeBase64String(cipher.doFinal(text.getBytes()));
            else
                return null;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String getDisplayString(String[] strings) {
        return "Usage: FUNC_(String encrypted_text)";
    }
}

3、RSA解密

import org.apache.commons.codec.binary.Base64;
import org.apache.hadoop.hive.ql.exec.MapredContext;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * RSA 解密
 */
public class RsaDecoder extends GenericUDF {

    private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //非对称密钥算法
    public static final String KEY_ALGORITHM = "RSA";

    // RSA 私钥
    private static String RasPrivateKey = "RSA私钥用于解密";

    private Cipher cipher = null;

    @Override
    public void configure(MapredContext context) {
        System.out.println(new Date() + "######## configure");

        if(null != context) {
            //从jobConf中获取私钥
            String confPrivateKey = context.getJobConf().get("com.howe.udf.rsa.privatekey");

            if( confPrivateKey != null && confPrivateKey.length() > 0 ) {
                RasPrivateKey = confPrivateKey;
            }
        }
    }

    @Override
    public ObjectInspector initialize(ObjectInspector[] arguments) {

        //System.out.println(formatter.format(new Date()) + "######## initialize");

        try {
            //取得私钥
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(RasPrivateKey));
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
            //生成私钥
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
            //数据解密
            cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            cipher.init(Cipher.DECRYPT_MODE, privateKey);

            System.out.println(formatter.format(new Date()) + "######## The Initialization Of Cipher Has Completed Successfully");

        } catch (Exception e) {
            e.printStackTrace();
            cipher = null;
            System.out.println(formatter.format(new Date()) + "######## Error: Initialization Failed!");
        }

        return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING);
    }

    @Override
    public Object evaluate(DeferredObject[] args) throws HiveException {
        if( cipher == null ) return null;

        try {

            final String text = args[0].get().toString();
            if ( text != null && text.length() > 0)
                return new String(cipher.doFinal(Base64.decodeBase64(text)));
            else
                return null;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String getDisplayString(String[] strings) {
        return "Usage: FUNC_(String encrypted_text)";
    }
}

4、创建函数

将打包后的jar包上传到hdfs

hive

DROP  FUNCTION IF EXISTS default.rsa_encoder;
CREATE function default.rsa_encoder as 'com.howe.hive.udf.encoder.RsaEncoder' using jar 'hdfs://hacluster/user/hive/udf/MyUdf.jar';
DROP  FUNCTION IF EXISTS default.rsa_decoder;
CREATE function default.rsa_decoder as 'com.howe.hive.udf.encoder.RsaDecoder' using jar 'hdfs://hacluster/user/hive/udf/MyUdf.jar';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值