package org.apache.doris.udf.demo;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.lang3.StringUtils;
import java.security.SecureRandom;
/**
* AES encryption and decryption tool class
*
* @author zhangfeng
*/
public class AESUtil {
private static final String defaultCharset = "UTF-8";
private static final String KEY_AES = "AES";
/**
* AES encryption function method
*
* @param content
* @param secret
* @return
*/
public static String encrypt(String content, String secret) {
return doAES(content, secret, Cipher.ENCRYPT_MODE);
}
/**
* AES decryption function method
*
* @param content
* @param secret
* @return
*/
public static String decrypt(String content, String secret) {
return doAES(content, secret, Cipher.DECRYPT_MODE);
}
/**
* encryption and decryption
*
* @param content
* @param secret
* @param mode
* @return
*/
private static String doAES(String content, String secret, int mode) {
try {
if (StringUtils.isBlank(content) || StringUtils.isBlank(secret)) {
return null;
}
//Determine whether to encrypt or decrypt
boolean encrypt = mode == Cipher.ENCRYPT_MODE;
byte[] data;
//1.Construct a key generator, specified as the AES algorithm, case-insensitive
KeyGenerator kgen = KeyGenerator.getInstance(KEY_AES);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
//2. Initialize the key generator according to the ecnodeRules rules
//Generate a 128-bit random source, based on the incoming byte array
secureRandom.setSeed(secret.getBytes());
//Generate a 128-bit random source, based on the incoming byte array
kgen.init(128, secureRandom);
//3.generate the original symmetric key
SecretKey secretKey = kgen.generateKey();
//4.Get the byte array of the original symmetric key
byte[] enCodeFormat = secretKey.getEncoded();
//5.Generate AES key from byte array
SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, KEY_AES);
//6.According to the specified algorithm AES self-generated cipher
Cipher cipher = Cipher.getInstance(KEY_AES);
//7.Initialize the cipher, the first parameter is encryption (Encrypt_mode) or decryption (Decrypt_mode) operation,
// the second parameter is the KEY used
cipher.init(mode, keySpec);
if (encrypt) {
data = content.getBytes(defaultCharset);
} else {
data = parseHexStr2Byte(content);
}
byte[] result = cipher.doFinal(data);
if (encrypt) {
//convert binary to hexadecimal
return parseByte2HexStr(result);
} else {
return new String(result, defaultCharset);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
/**
* convert binary to hexadecimal
*
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* Convert hexadecimal to binary
*
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1) {
return null;
}
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
加密函数
package org.apache.doris.udf.demo;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.commons.lang3.StringUtils;
public class AESEncrypt extends UDF {
public String evaluate(String content, String secret) throws Exception {
if (StringUtils.isBlank(content)) {
throw new Exception("content not is null");
}
if (StringUtils.isBlank(secret)) {
throw new Exception("Secret not is null");
}
return AESUtil.encrypt(content, secret);
}
}
解密函数
package org.apache.doris.udf.demo;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.commons.lang3.StringUtils;
public class AESDecrypt extends UDF {
public String evaluate(String content, String secret) throws Exception {
if (StringUtils.isBlank(content)) {
throw new Exception("content not is null");
}
if (StringUtils.isBlank(secret)) {
throw new Exception("Secret not is null");
}
return AESUtil.decrypt(content, secret);
}
}
Shell 流程控制
和Java、PHP等语言不一样,sh的流程控制不可为空,如(以下为PHP流程控制写法):
<?php
if(isset($_GET["q"])){
search(q);}else{// 不做任何事情}
在sh/bash里可不能这么写,如果else分支没有语句执行,就不要写这个else,就像这样 if else if
if 语句语
因为我们做的是聊天室,所以会有多个客户端,每个客户端我们用一个线程去实现,通过搭建一个服务器来实现从每个客户端来读取信息和发送信息。
我们先写客户端的线程。
public class ChatSocket extends Thread{
Socket socket;
public ChatSocket(Socket socket){
this.sock
在第一篇中,定义范型类时,使用如下的方式:
public class Generics<M, S, N> {
//M,S,N是范型参数
}
这种方式定义的范型类有两个基本的问题:
1. 范型参数定义的实例字段,如private M m = null;由于M的类型在运行时才能确定,那么我们在类的方法中,无法使用m,这跟定义pri
当tomcat是解压的时候,用eclipse启动正常,点击startup.bat的时候启动报错;
报错如下:
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME shou
When you got error message like "Property null not found ***", try to fix it by the following way:
1)if you are using AdvancedDatagrid, make sure you only update the data in the data prov
当iOS 8.0和OS X v10.10发布后,一个全新的概念出现在我们眼前,那就是应用扩展。顾名思义,应用扩展允许开发者扩展应用的自定义功能和内容,能够让用户在使用其他app时使用该项功能。你可以开发一个应用扩展来执行某些特定的任务,用户使用该扩展后就可以在多个上下文环境中执行该任务。比如说,你提供了一个能让用户把内容分
SQL>select text from all_source where owner=user and name=upper('&plsql_name');
SQL>select * from user_ind_columns where index_name=upper('&index_name'); 将表记录恢复到指定时间段以前