-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
625dabf
commit e87b2ec
Showing
7 changed files
with
36 additions
and
141 deletions.
There are no files selected for viewing
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
51 changes: 31 additions & 20 deletions
51
app/src/main/java/com/lowlevelsubmarine/applock/AppLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
app/src/main/java/com/lowlevelsubmarine/applock/Bytes.java
This file was deleted.
Oops, something went wrong.
79 changes: 0 additions & 79 deletions
79
app/src/main/java/com/lowlevelsubmarine/applock/Identifier.java
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
app/src/main/java/com/lowlevelsubmarine/applock/IdentifierBytes.java
This file was deleted.
Oops, something went wrong.