Skip to content

Commit

Permalink
no console.log please.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkhala committed May 20, 2024
1 parent 566badb commit 08cd3a5
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 60 deletions.
3 changes: 3 additions & 0 deletions crypto/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ dependencies {
// org.spongycastle
implementation('com.madgag.spongycastle:pkix:latest.integration')

// @javax.annotation.Nonnull
implementation("com.google.code.findbugs:jsr305:3.0.2")

testImplementation platform('org.junit:junit-bom:5.10.2')
testImplementation 'org.junit.jupiter:junit-jupiter'
}
Expand Down
60 changes: 17 additions & 43 deletions crypto/src/main/java/org/davidkhala/AES.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
Expand All @@ -15,25 +14,12 @@
import javax.crypto.spec.SecretKeySpec;

public class AES {
public static byte[] hash(byte[] data) {
try {
MessageDigest e = MessageDigest.getInstance("SHA-256");
e.update(data);
return e.digest();
} catch (NoSuchAlgorithmException var2) {
var2.printStackTrace();
return null;
}
}

public static byte[] encrypt(byte[] key, byte[] inputValue)
throws NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec sKeyS = new SecretKeySpec(key, "AES");

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, sKeyS);

return cipher.doFinal(inputValue);
return encrypt(sKeyS, inputValue);
}

public static byte[] encrypt(Key key, byte[] inputValue)
Expand All @@ -50,10 +36,7 @@ public static byte[] decrypt(byte[] key, byte[] encryptedData)
throws NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec sKeyS = new SecretKeySpec(key, "AES");

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, sKeyS);

return cipher.doFinal(encryptedData);
return decrypt(sKeyS, encryptedData);
}

public static byte[] decrypt(Key key, byte[] encryptedData)
Expand All @@ -66,32 +49,23 @@ public static byte[] decrypt(Key key, byte[] encryptedData)
return cipher.doFinal(encryptedData);
}

public static Key generateKey(int keysize, String password)
{
return generateKey(keysize,"AES",password);
public static Key generateKey(int keysize, @javax.annotation.Nonnull String password) throws NoSuchAlgorithmException {
return generateKey(keysize, "AES", password);
}
public static Key wrapKey(byte[] keyEncoded)
{
return new SecretKeySpec(keyEncoded, "AES");

public static Key generateKey(int keysize, String algorithm, @javax.annotation.Nonnull String password) throws NoSuchAlgorithmException {
SecureRandom e = SecureRandom.getInstance("SHA1PRNG");
byte[] hash = Hash.SHA2_256(password.getBytes(Charset.forName("UTF-8")));
e.setSeed(hash);
KeyGenerator keyGen = KeyGenerator.getInstance(algorithm);
keyGen.init(keysize, e);
SecretKey secret = keyGen.generateKey();

SecretKeySpec keySpec = new SecretKeySpec(secret.getEncoded(), algorithm);
return keySpec;
}

public static Key generateKey(int keysize, String algorithm, String password) {
if(password != null) {
try {
SecureRandom e = SecureRandom.getInstance("SHA1PRNG");
byte[] hash = hash(password.getBytes(Charset.forName("UTF-8")));
e.setSeed(hash);
KeyGenerator keyGen = KeyGenerator.getInstance(algorithm);
keyGen.init(keysize, e);
SecretKey secret = keyGen.generateKey();

SecretKeySpec keySpec = new SecretKeySpec(secret.getEncoded(), algorithm);
return keySpec;
} catch (NoSuchAlgorithmException var7) {
var7.printStackTrace();
}
}

return null;
public static Key wrapKey(byte[] keyEncoded) {
return new SecretKeySpec(keyEncoded, "AES");
}
}
6 changes: 6 additions & 0 deletions crypto/src/main/java/org/davidkhala/Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
public class Hash {
public static byte[] MD5(byte[] message) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");

return md.digest(message);
}

public static byte[] SHA2_256(byte[] data) throws NoSuchAlgorithmException {
MessageDigest e = MessageDigest.getInstance("SHA-256");
return e.digest(data);
}
}
11 changes: 11 additions & 0 deletions crypto/src/main/java/org/davidkhala/Hex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.davidkhala;

public class Hex {
public static String encode(byte[] bytes) {
return new String(org.spongycastle.util.encoders.Hex.encode(bytes));
}

public static String decode(String string) {
return new String(org.spongycastle.util.encoders.Hex.decode(string));
}
}
25 changes: 8 additions & 17 deletions crypto/src/main/java/org/davidkhala/KeyStoreTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public KeyStoreTool(KeyStore keyStore) {

public KeyManager[] getKeyManagers(String keyStorePwd) throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException {

String algo= KeyManagerFactory.getDefaultAlgorithm();//Android:"PKIX",Java:"SunX509"
String algo = KeyManagerFactory.getDefaultAlgorithm();//Android:"PKIX",Java:"SunX509"
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(algo);
keyManagerFactory.init(keyStore,keyStorePwd==null?null:keyStorePwd.toCharArray());
keyManagerFactory.init(keyStore, keyStorePwd == null ? null : keyStorePwd.toCharArray());
KeyManager[] result = keyManagerFactory.getKeyManagers();//Android:"com.android.org.conscrypt.KeyManagerImpl"
assert result.length ==1;
assert result.length == 1;

return result;
}
Expand All @@ -33,29 +33,21 @@ public TrustManager[] genTrustedManagers() throws NoSuchAlgorithmException, KeyS
String algo = TrustManagerFactory.getDefaultAlgorithm();//Android:"PKIX", Java: "SunX509"
TrustManagerFactory tmf = TrustManagerFactory.getInstance(algo);
tmf.init(keyStore);
TrustManager[] trustManagers = tmf.getTrustManagers();//length == 1, Android: com.android.org.conscrypt.TrustManagerImpl
return trustManagers;
return tmf.getTrustManagers();
}


public Map<String, KeyStore.Entry> aliases(boolean printLog) throws KeyStoreException {
public Map<String, KeyStore.Entry> aliases() throws KeyStoreException, UnrecoverableEntryException, NoSuchAlgorithmException {


Map<String, KeyStore.Entry> keyAliases = new HashMap<>();

Enumeration<String> aliases = keyStore.aliases();
if(printLog)System.out.println("ALIAS:" + "size:" + keyStore.size());

while (aliases.hasMoreElements()) {
String aliasString = aliases.nextElement();
try {
if(printLog)System.out.println("ALIAS:" + aliasString);
KeyStore.Entry entry = keyStore.getEntry(aliasString, null);
if(printLog)System.out.println("ALIAS entry:" + " -->" + entry);
keyAliases.put(aliasString, entry);

} catch (NoSuchAlgorithmException | UnrecoverableEntryException e) {
e.printStackTrace();
}
KeyStore.Entry entry = keyStore.getEntry(aliasString, null);
keyAliases.put(aliasString, entry);
}
return keyAliases;

Expand All @@ -65,7 +57,6 @@ public void clear() throws KeyStoreException {
Enumeration<String> aliases = keyStore.aliases();
while (aliases.hasMoreElements()) {
String aliasString = aliases.nextElement();
System.out.println("ALIAS: delete " + aliasString);
keyStore.deleteEntry(aliasString);

}
Expand Down
13 changes: 13 additions & 0 deletions crypto/src/test/java/HashTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import org.davidkhala.Hash;
import org.davidkhala.Hex;
import org.junit.jupiter.api.Test;

import java.security.NoSuchAlgorithmException;

public class HashTest {
@Test
public void testMD5() throws NoSuchAlgorithmException {
String message = "p4ssw0rd";
assert Hex.encode(Hash.MD5(message.getBytes())).equals("2a9d119df47ff993b662a8ef36f9ea20");
}
}
23 changes: 23 additions & 0 deletions crypto/src/test/java/KeyStoreTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import org.davidkhala.KeyStoreTool;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableEntryException;
import java.security.cert.CertificateException;

public class KeyStoreTest {
@Test
protected void sanCheck() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableEntryException {

String type = KeyStore.getDefaultType();
assert type.equals("pkcs12");
KeyStore keystore = KeyStore.getInstance(type);
String password = "password";
keystore.load(null, password.toCharArray());
KeyStoreTool keyStoreTool = new KeyStoreTool(keystore);
System.out.println(keyStoreTool.aliases());
}
}
1 change: 1 addition & 0 deletions light/src/test/java/CountryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public void getNames() {
public void validateHKID() {
String id = "D788888(1)";
assert CountryTool.isHKIDValid(id);
assert CountryTool.trimBrackets(id).equals("D7888881");
}
}

0 comments on commit 08cd3a5

Please sign in to comment.