Skip to content

Commit

Permalink
1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
LowLevelSubmarine committed Sep 29, 2019
1 parent 625dabf commit e87b2ec
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 141 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 31 additions & 20 deletions app/src/main/java/com/lowlevelsubmarine/applock/AppLock.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,54 @@
package com.lowlevelsubmarine.applock;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Random;

public class AppLock {

private static final Cipher CIPHER = createCipher();
private final Cipher cipher = createCipher();
private final SecretKeySpec key;

public static boolean valid(IdentifierBytes idBytes, String signed, SecretKeySpec key) throws InvalidKeyException {
try {
Bytes signedBytes = new Bytes(signed);
CIPHER.init(Cipher.DECRYPT_MODE, key);
return Arrays.equals(CIPHER.doFinal(signedBytes.getBytes()), idBytes.getBytes());
} catch (BadPaddingException | IllegalBlockSizeException e) {
throw new InternalError("Impossible error");
}
public AppLock(byte[] key) throws InvalidKeyException {
this.key = new SecretKeySpec(key, "AES");
}

public byte[] generateKey(long salt) {
byte[] bytes = new byte[16];
new Random(salt).nextBytes(bytes);
return bytes;
}

public String sign(String unsigned) throws InvalidKeyException {
return B64.encode(this.encrypt(B64.decode(unsigned), this.key));
}

public String sign(IdentifierBytes idBytes, SecretKeySpec key) throws InvalidKeyException {
private byte[] encrypt(byte[] bytes, SecretKeySpec key) throws InvalidKeyException {
try {
CIPHER.init(Cipher.ENCRYPT_MODE, key);
return BaseSixtyFour.encode(CIPHER.doFinal(idBytes.getBytes()));
} catch (IllegalBlockSizeException | BadPaddingException e) {
throw new InternalError("Impossible error");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(bytes);
} catch (BadPaddingException | IllegalBlockSizeException e) {
throw new InternalError(e.getMessage());
}
}

public static Cipher createCipher() {
private static Cipher createCipher() {
try {
return Cipher.getInstance("AES/ECB/NoPadding");
} catch (NoSuchPaddingException | NoSuchAlgorithmException e) {
throw new InternalError("Failed to create cipher");
}
}

private static MessageDigest createDigest() {
try {
return MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import android.util.Base64;

public class BaseSixtyFour {
public class B64 {

public static String encode(byte[] decoded) {
return Base64.encodeToString(decoded, Base64.DEFAULT);
Expand Down
23 changes: 0 additions & 23 deletions app/src/main/java/com/lowlevelsubmarine/applock/Bytes.java

This file was deleted.

79 changes: 0 additions & 79 deletions app/src/main/java/com/lowlevelsubmarine/applock/Identifier.java

This file was deleted.

This file was deleted.

0 comments on commit e87b2ec

Please sign in to comment.