cgivre / drill-crypto-functions Goto Github PK
View Code? Open in Web Editor NEWA collection of UDFs for Apache Drill that implement common cryptographical functions.
License: Other
A collection of UDFs for Apache Drill that implement common cryptographical functions.
License: Other
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 !
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.