Hive UDF实现RAS加密解密

话不多说,直接上代码

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;

/**
 * CREATE FUNCTION default.rsa_encoder AS 'com.howe.hive.udf.encoder.RsaEncoder' USING JAR 'hdfs://nameservice1/user/howe/MyHiveUdf.jar';
 * select rsa_encoder('测试123');
 * 
 * CREATE temporary function rsa_decoder as 'com.howe.hive.udf.encoder.RsaDecoder';
 */
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 Public Key";

    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.szlanyou.udf.ras.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[] arg
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值