Code Monkey home page Code Monkey logo

drill-crypto-functions's People

Contributors

cgivre avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

varunpillai

drill-crypto-functions's Issues

drill execute function 2 times ,and the result is not I want

dear ,friend
I'm very sorry to trouble you ,actually I'm in trouble , I suffered from this problem two days now , any help is appreciated ,3Q
I write a class to decode the customer id of our company ,which the key is a special key ,
the decode algorithm is des . I write the code just copyed from you aes_decode class, and changed the algorithm from aes to des , but when I run a select function , the code run two times , and the first is the right result , however the second is wrong , so the final result is wrong .

I list my code here ๏ผš
package org.apache.drill.exec.fn.impl;

import com.google.common.base.Charsets;
import io.netty.buffer.DrillBuf;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.net.BCodec;
import org.apache.drill.exec.expr.DrillSimpleFunc;
import org.apache.drill.exec.expr.annotations.FunctionTemplate;
import org.apache.drill.exec.expr.annotations.Output;
import org.apache.drill.exec.expr.annotations.Param;
import org.apache.drill.exec.expr.annotations.Workspace;
import org.apache.drill.exec.expr.holders.VarCharHolder;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.inject.Inject;

@FunctionTemplate(
name = "decode_cid",
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
)

public class DecodeCidFunction implements DrillSimpleFunc {

@Param
VarCharHolder raw_input;

// @param
// VarCharHolder raw_key;

@Output
VarCharHolder out;

@Inject
DrillBuf buffer;

// @workspace
// KeyGenerator keygenerator;

@Workspace
SecretKey myDesKey;

@Workspace
Cipher desCipher;

@Workspace
BCodec bCodec;

@Override
public void setup() {
    try {
        String key = "this is a secret";
        javax.crypto.SecretKeyFactory factory = javax.crypto.SecretKeyFactory.getInstance("DES");
        myDesKey = factory.generateSecret(new javax.crypto.spec.DESKeySpec(key.getBytes())) ;
        System.out.println("myDesKey = "+myDesKey.toString());
        // Create the cipher
        desCipher = Cipher.getInstance("DES");

        // Initialize the cipher for encryption
        desCipher.init(Cipher.DECRYPT_MODE, myDesKey);

        bCodec =  new BCodec("UTF-8");

    } catch(Exception e) {
        System.out.println("may i come here");
        e.printStackTrace();
    }
}

@Override
public void eval() {
    String input = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(raw_input.start, raw_input.end, raw_input.buffer);
    String output = "";
    System.out.println("input = " + input);
    if (input.startsWith("=?"))
    {
        try{
            output = bCodec.decode(input);
        }catch(Exception e){
            System.out.println("find an error :" +e.toString());
            output  = "";
        }
    }else{
        byte[] bts = new byte[input.length() / 2];
        for (int i = 0; i < bts.length; i++) {
            bts[i] = (byte) Integer.parseInt(input.substring(2*i, 2*i+2), 16);
        }
        System.out.println("bts = " +bts.toString());
        try{
            byte[] decodedString = desCipher.doFinal(bts) ;
            output = new String(decodedString, "utf-8");
        }catch(Exception e){
            System.out.println("i come here " + e.toString());
            output="";
        }
    }
    System.out.println("output = " + output);
    out.buffer = buffer;
    out.start = 0;
    out.end = output.getBytes().length;
    buffer.setBytes(0, output.getBytes());
}

}

And my select query is below ๐Ÿ‘
0: jdbc:drill:zk=local> select decode_cid('A849333D2713FAFEA10780AD02139B29') from (values(1));
myDesKey = com.sun.crypto.provider.DESKey@183d7
input = A849333D2713FAFEA10780AD02139B29
bts = [B@22f4128e
output = 20138690
myDesKey = com.sun.crypto.provider.DESKey@18301
input = A849333D2713FAFEA10780AD02139B29
bts = [B@7a69714
i come here javax.crypto.BadPaddingException: Given final block not properly padded
output =
+---------+
| EXPR$0 |
+---------+
| |
+---------+
1 row selected (3.075 seconds)

you can see the first output is right , however the second is null ,null is not I want ,I want know how it run two times ?

thank you !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.