Skip to content

Commit

Permalink
cleaning codes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkhala committed Apr 23, 2024
1 parent 219426a commit 54fd34c
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 159 deletions.
4 changes: 0 additions & 4 deletions src/main/java/davidkhala/common/AbstractPropertiesLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import java.io.InputStream;
import java.util.Properties;

/**
* Created by davidliu on 11/4/2016.
*/

public abstract class AbstractPropertiesLoader {
protected abstract InputStream propertiesProvider() throws IOException;

Expand Down
22 changes: 7 additions & 15 deletions src/main/java/davidkhala/common/CountryTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
* Created by davidliu on 2/4/2017.
*/

public class CountryTool {

public static ArrayList<String> getNames() {
Locale[] locales = Locale.getAvailableLocales();
ArrayList<String> countries = new ArrayList<String>();
for (Locale locale : locales) {
String country = locale.getDisplayCountry();
if (country.trim().length() > 0 && !countries.contains(country)) {
if (!country.trim().isEmpty() && !countries.contains(country)) {
countries.add(country);
}
}
Expand Down Expand Up @@ -54,28 +49,25 @@ public static String trimHKID(String hkid,boolean isStrict){
if (!m.find()) return null;
String prefix = m.group(1);
String serial = m.group(2);
String checkdigit = m.group(3);
String checkDigit = m.group(3);
long value = 0;
if (prefix.length() == 2) {
value += (prefix.charAt(0) - 55) * 9 + (prefix.charAt(1) - 55) * 8;
} else if (prefix.length() == 1) {
value += 36 * 9 + (prefix.charAt(0) - 55) * 8;
}
try {
for (int i = 0; i < 6; i++) {
value += Integer.parseInt(serial.substring(i, i + 1)) * (7 - i);
value += (long) Integer.parseInt(serial.substring(i, i + 1)) * (7 - i);
}
long reminder = value % 11;
long valid_checkdigit = 11 - reminder;
if ("A".equalsIgnoreCase(checkdigit) && valid_checkdigit == 10) {
long valid_checkDigit = 11 - reminder;
if ("A".equalsIgnoreCase(checkDigit) && valid_checkDigit == 10) {
return HKID;
}
if (valid_checkdigit == Integer.parseInt(checkdigit)) {
if (valid_checkDigit == Integer.parseInt(checkDigit)) {
return HKID;
}
} catch (NumberFormatException e) {
e.printStackTrace();
}

return null;
}

Expand Down
59 changes: 15 additions & 44 deletions src/main/java/davidkhala/common/FileTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,37 @@

import java.io.*;
import java.net.URI;
import java.nio.file.Files;


/**
* Created by davidliu on 9/29/2016.
*/
import java.util.Objects;

public class FileTool {
public static void write(File targetFile, String content) throws IOException {
targetFile.createNewFile();
FileOutputStream fop = new FileOutputStream(targetFile);


// get the content in bytes
byte[] contentInBytes = content.getBytes();

fop.write(contentInBytes);
fop.flush();
fop.close();
}

public static String read(File targetFile) throws IOException {
FileInputStream is = new FileInputStream(targetFile);
return read(is);
}
public static boolean overwrite(File targetFile, String content) throws IOException {
boolean noExist = targetFile.createNewFile();
if (noExist) {
return false;
}

public static String read(InputStream is) throws IOException {
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer);
FileTool.write(targetFile, content);
return true;
}

public static int copyDir(File from, File to) throws IOException {
// Preconditions.checkArgument(from.isDirectory(),"source file should be directory:"+from.getPath());
// Preconditions.checkArgument(to.isDirectory(),"target file should be directory:"+to.getPath());
File[] files = from.listFiles();
if(files==null)return -1;
for(File file: files){
File targetFile = new File(to,file.getName());
if(file.isDirectory()){
if(targetFile.mkdir()){
copyDir(file,targetFile);
}
}else {

// Files.copy(file.toPath(), targetFile.toPath());
}
}
return files.length;
public static String read(File targetFile) throws IOException {
FileInputStream is = new FileInputStream(targetFile);
return Stream.read(is);
}

public static boolean deleteDir(File dir) {

boolean result = true;
for (File file : dir.listFiles()
) {
for (File file : Objects.requireNonNull(dir.listFiles())) {

if (file.isDirectory()) {
if (!deleteDir(file)) result = false;
Expand All @@ -69,16 +42,14 @@ public static boolean deleteDir(File dir) {
}
return result;
}

public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
if (filename != null && !filename.isEmpty()) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length() - 1))) {
if ((dot > -1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
public static File fromURI(URI uri){
return new File(uri);
}
}
12 changes: 4 additions & 8 deletions src/main/java/davidkhala/common/Reflection.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@
import java.util.HashMap;
import java.util.Map;

/**
* Created by davidliu on 2018/1/10.
*/

public class Reflection {
Object object;

Class clazz;
Class<?> clazz;
public Reflection(Object object) {
this.object = object;
this.clazz = object.getClass();
}
public Reflection asSuperClass(Class clazz){
public Reflection asSuperClass(Class<?> clazz){
if(clazz.isInstance(object)){
this.clazz = clazz;
}
return this;
}

private static final Map<Class, Class> WRAPPER_TYPES = new HashMap<>();
private static final Map<Class<?>, Class<?>> WRAPPER_TYPES = new HashMap<>();

static {
WRAPPER_TYPES.put(Boolean.class, Boolean.TYPE);
Expand All @@ -44,7 +40,7 @@ public static boolean isWrapperType(Class<?> clazz) {
}

public Object method(String methodName, Object... args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class[] argClasses = new Class[args.length];
Class<?>[] argClasses = new Class[args.length];
for (int i = 0; i < args.length; i++) {
Class<?> argClass = args[i].getClass();
if (isWrapperType(argClass)) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/davidkhala/common/Stream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package davidkhala.common;

import java.io.IOException;
import java.io.InputStream;

public class Stream {
public static String read(InputStream is) throws IOException {
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer);
}
}
4 changes: 0 additions & 4 deletions src/main/java/davidkhala/common/secure/AES.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
Expand All @@ -15,9 +14,6 @@
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
* Created by DavidLiu on 25/8/2016.
*/
public class AES {
public static byte[] hash(byte[] data) {
try {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/davidkhala/common/secure/AbstractHTTPS.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import javax.net.ssl.*;
import java.security.*;

/**
* Created by davidliu on 11/9/2016.
*/

public abstract class AbstractHTTPS {
public static SSLSocketFactory getSSLSocketFactory(KeyStore keyStore, String keyStorePwd, KeyStore trustKeyStore) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyManagementException {
TrustManager[] trustManagers = new KeyStoreTool(trustKeyStore).genTrustedManagers();
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/davidkhala/common/secure/CSRTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

import javax.security.auth.x500.X500Principal;

/**
* Created by davidliu on 1/23/2017.
*/

public class CSRTool {
public static String wrapPublicReq(PKCS10CertificationRequest csr) throws IOException {
byte[] CSRder;
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/davidkhala/common/secure/CertTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;

/**
* Created by davidliu on 1/23/2017.
*/

public class CertTool {
static String defaultCertType = "X.509";
static String defaultProvider = "BC";
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/davidkhala/common/secure/KeyStoreTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
import java.util.HashMap;
import java.util.Map;

/**
* Created by davidliu on 11/4/2016.
*/

public class KeyStoreTool {

KeyStore keyStore;
Expand Down
91 changes: 43 additions & 48 deletions src/main/java/davidkhala/common/secure/RSAKeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,55 @@
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
* Created by davidliu on 11/9/2016.
*/

public class RSAKeyPair {
public static KeyPair get() {
return get(null);
}
public static KeyPair get() {

try {
return get(null);
} catch (NoSuchAlgorithmException |NoSuchProviderException e) {
return null;
}

}

public static RSAPublicKey recoverPubKey(byte[] pubBytes) throws NoSuchAlgorithmException, InvalidKeySpecException {
return (RSAPublicKey)
KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(pubBytes));
}

public static RSAPublicKey recoverPubKey(RSAPrivateCrtKey privateCrtKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
return (RSAPublicKey)
KeyFactory.getInstance("RSA")
.generatePublic(
new RSAPublicKeySpec(
privateCrtKey.getModulus(), privateCrtKey.getPublicExponent()));
}

public static KeyPair get(String provider) throws NoSuchAlgorithmException, NoSuchProviderException {

KeyPairGenerator generator =
provider == null
? KeyPairGenerator.getInstance("RSA")
: KeyPairGenerator.getInstance("RSA", provider); // "BC", "AndroidOpenSSL"
if (provider == null)
System.out.println("RSAKeypair generator default provider: " + generator.getProvider());
generator.initialize(2048);
return generator.generateKeyPair();

public static RSAPublicKey recoverPubKey(byte[] pubBytes) {
try {
return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(pubBytes));
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
}

public static RSAPublicKey recoverPubKey(RSAPrivateCrtKey privateCrtKey) {
try {
return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(privateCrtKey.getModulus(), privateCrtKey.getPublicExponent()));
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
public static RSAPrivateKey recoverPrivKey(byte[] privateBytes) throws NoSuchAlgorithmException, InvalidKeySpecException {

public static KeyPair get(String provider) {
try {
KeyPairGenerator generator = provider == null ? KeyPairGenerator.getInstance("RSA") : KeyPairGenerator.getInstance("RSA", provider);//"BC", "AndroidOpenSSL"
if (provider == null)
System.out.println("RSAKeypair generator default provider: " + generator.getProvider());
generator.initialize(2048);
return generator.generateKeyPair();
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
e.printStackTrace();
return null;
}
}
return (RSAPrivateKey)
KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateBytes));

public static RSAPrivateKey recoverPrivKey(byte[] privateBytes) {
try {
return (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateBytes));
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
}

public static KeyPair recover(byte[] privateBytes, byte[] pubBytes) {
public static KeyPair recover(byte[] privateBytes, byte[] pubBytes) throws NoSuchAlgorithmException, InvalidKeySpecException {

RSAPrivateKey privateKey = recoverPrivKey(privateBytes);
RSAPublicKey publicKey = recoverPubKey(pubBytes);
RSAPrivateKey privateKey = recoverPrivKey(privateBytes);
RSAPublicKey publicKey = recoverPubKey(pubBytes);

return new KeyPair(publicKey, privateKey);
}
return new KeyPair(publicKey, privateKey);
}
}
4 changes: 0 additions & 4 deletions src/main/java/davidkhala/common/secure/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
* Created by davidliu on 2/1/2017.
*/

public class Utils {
/**
* Hex encoding used throughout the framework. Use with HEX.encode(byte[]) or HEX.decode(CharSequence).
Expand Down
Loading

0 comments on commit 54fd34c

Please sign in to comment.