diff --git a/app/src/androidTest/java/com/kunzisoft/keepass/tests/crypto/AESTest.java b/app/src/androidTest/java/com/kunzisoft/keepass/tests/crypto/AESTest.java
index 04933fda6..b439b28a4 100644
--- a/app/src/androidTest/java/com/kunzisoft/keepass/tests/crypto/AESTest.java
+++ b/app/src/androidTest/java/com/kunzisoft/keepass/tests/crypto/AESTest.java
@@ -72,7 +72,7 @@ public class AESTest extends TestCase {
android.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] outAndroid = android.doFinal(input, 0, dataSize);
- Cipher nat = CipherFactory.getInstance("AES/CBC/PKCS5Padding");
+ Cipher nat = CipherFactory.INSTANCE.getInstance("AES/CBC/PKCS5Padding");
nat.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] outNative = nat.doFinal(input, 0, dataSize);
diff --git a/app/src/main/java/com/kunzisoft/keepass/crypto/CryptoUtil.java b/app/src/main/java/com/kunzisoft/keepass/crypto/CryptoUtil.java
deleted file mode 100644
index ec2e1d98e..000000000
--- a/app/src/main/java/com/kunzisoft/keepass/crypto/CryptoUtil.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
- *
- * This file is part of KeePass DX.
- *
- * KeePass DX is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * KeePass DX is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with KeePass DX. If not, see .
- *
- */
-package com.kunzisoft.keepass.crypto;
-
-import com.kunzisoft.keepass.stream.LEDataOutputStream;
-import com.kunzisoft.keepass.stream.NullOutputStream;
-
-import java.io.IOException;
-import java.security.DigestOutputStream;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.util.Arrays;
-
-import javax.crypto.Mac;
-
-public class CryptoUtil {
- public static byte[] resizeKey(byte[] in, int inOffset, int cbIn, int cbOut) {
- if (cbOut == 0) return new byte[0];
-
- byte[] hash;
- if (cbOut <= 32) { hash = hashSha256(in, inOffset, cbIn); }
- else { hash = hashSha512(in, inOffset, cbIn); }
-
- if (cbOut == hash.length) { return hash; }
-
- byte[] ret = new byte[cbOut];
- if (cbOut < hash.length) {
- System.arraycopy(hash, 0, ret, 0, cbOut);
- }
- else {
- int pos = 0;
- long r = 0;
- while (pos < cbOut) {
- Mac hmac;
- try {
- hmac = Mac.getInstance("HmacSHA256");
- } catch (NoSuchAlgorithmException e) {
- throw new RuntimeException(e);
- }
-
- byte[] pbR = LEDataOutputStream.writeLongBuf(r);
- byte[] part = hmac.doFinal(pbR);
-
- int copy = Math.min(cbOut - pos, part.length);
- System.arraycopy(part, 0, ret, pos, copy);
- pos += copy;
- r++;
-
- Arrays.fill(part, (byte)0);
- }
- }
-
- Arrays.fill(hash, (byte)0);
- return ret;
- }
-
- public static byte[] hashSha256(byte[] data) {
- return hashSha256(data, 0, data.length);
- }
-
- public static byte[] hashSha256(byte[] data, int offset, int count) {
- return hashGen("SHA-256", data, offset, count);
- }
-
- public static byte[] hashSha512(byte[] data) {
- return hashSha512(data, 0, data.length);
- }
-
- public static byte[] hashSha512(byte[] data, int offset, int count) {
- return hashGen("SHA-512", data, offset, count);
- }
-
- public static byte[] hashGen(String transform, byte[] data, int offset, int count) {
- MessageDigest hash;
- try {
- hash = MessageDigest.getInstance(transform);
- } catch (NoSuchAlgorithmException e) {
- throw new RuntimeException(e);
- }
-
- NullOutputStream nos = new NullOutputStream();
- DigestOutputStream dos = new DigestOutputStream(nos, hash);
-
- try {
- dos.write(data, offset, count);
- dos.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
-
- return hash.digest();
- }
-}
diff --git a/app/src/main/java/com/kunzisoft/keepass/crypto/CryptoUtil.kt b/app/src/main/java/com/kunzisoft/keepass/crypto/CryptoUtil.kt
new file mode 100644
index 000000000..d51045826
--- /dev/null
+++ b/app/src/main/java/com/kunzisoft/keepass/crypto/CryptoUtil.kt
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2019 Jeremy Jamet / Kunzisoft.
+ *
+ * This file is part of KeePass DX.
+ *
+ * KeePass DX is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * KeePass DX is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with KeePass DX. If not, see .
+ *
+ */
+package com.kunzisoft.keepass.crypto
+
+import com.kunzisoft.keepass.stream.LEDataOutputStream
+import com.kunzisoft.keepass.stream.NullOutputStream
+
+import java.io.IOException
+import java.security.DigestOutputStream
+import java.security.MessageDigest
+import java.security.NoSuchAlgorithmException
+import java.util.Arrays
+
+import javax.crypto.Mac
+import kotlin.math.min
+
+object CryptoUtil {
+
+ fun resizeKey(inBytes: ByteArray, inOffset: Int, cbIn: Int, cbOut: Int): ByteArray {
+ if (cbOut == 0) return ByteArray(0)
+
+ val hash: ByteArray = if (cbOut <= 32) {
+ hashSha256(inBytes, inOffset, cbIn)
+ } else {
+ hashSha512(inBytes, inOffset, cbIn)
+ }
+
+ if (cbOut == hash.size) {
+ return hash
+ }
+
+ val ret = ByteArray(cbOut)
+ if (cbOut < hash.size) {
+ System.arraycopy(hash, 0, ret, 0, cbOut)
+ } else {
+ var pos = 0
+ var r: Long = 0
+ while (pos < cbOut) {
+ val hmac: Mac
+ try {
+ hmac = Mac.getInstance("HmacSHA256")
+ } catch (e: NoSuchAlgorithmException) {
+ throw RuntimeException(e)
+ }
+
+ val pbR = LEDataOutputStream.writeLongBuf(r)
+ val part = hmac.doFinal(pbR)
+
+ val copy = min(cbOut - pos, part.size)
+ System.arraycopy(part, 0, ret, pos, copy)
+ pos += copy
+ r++
+
+ Arrays.fill(part, 0.toByte())
+ }
+ }
+
+ Arrays.fill(hash, 0.toByte())
+ return ret
+ }
+
+ @JvmOverloads
+ fun hashSha256(data: ByteArray, offset: Int = 0, count: Int = data.size): ByteArray {
+ return hashGen("SHA-256", data, offset, count)
+ }
+
+ @JvmOverloads
+ fun hashSha512(data: ByteArray, offset: Int = 0, count: Int = data.size): ByteArray {
+ return hashGen("SHA-512", data, offset, count)
+ }
+
+ private fun hashGen(transform: String, data: ByteArray, offset: Int, count: Int): ByteArray {
+ val hash: MessageDigest
+ try {
+ hash = MessageDigest.getInstance(transform)
+ } catch (e: NoSuchAlgorithmException) {
+ throw RuntimeException(e)
+ }
+
+ val nos = NullOutputStream()
+ val dos = DigestOutputStream(nos, hash)
+
+ try {
+ dos.write(data, offset, count)
+ dos.close()
+ } catch (e: IOException) {
+ throw RuntimeException(e)
+ }
+
+ return hash.digest()
+ }
+}
diff --git a/app/src/main/java/com/kunzisoft/keepass/crypto/NativeAESCipherSpi.java b/app/src/main/java/com/kunzisoft/keepass/crypto/NativeAESCipherSpi.java
index c27635f5d..913bca1c3 100644
--- a/app/src/main/java/com/kunzisoft/keepass/crypto/NativeAESCipherSpi.java
+++ b/app/src/main/java/com/kunzisoft/keepass/crypto/NativeAESCipherSpi.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
+ * Copyright 2019 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
@@ -47,8 +47,8 @@ public class NativeAESCipherSpi extends CipherSpi {
private static final String TAG = NativeAESCipherSpi.class.getName();
private static boolean mIsStaticInit = false;
- private static HashMap, Long> mCleanup = new HashMap, Long>();
- private static ReferenceQueue mQueue = new ReferenceQueue();
+ private static HashMap, Long> mCleanup = new HashMap<>();
+ private static ReferenceQueue mQueue = new ReferenceQueue<>();
private final int AES_BLOCK_SIZE = 16;
private byte[] mIV;
@@ -73,7 +73,6 @@ public class NativeAESCipherSpi extends CipherSpi {
/** Work with the garbage collector to clean up openssl memory when the cipher
* context is garbage collected.
* @author bpellin
- *
*/
private static class Cleanup implements Runnable {
@@ -96,7 +95,7 @@ public class NativeAESCipherSpi extends CipherSpi {
private static native void nCleanup(long ctxPtr);
public NativeAESCipherSpi() {
- if ( ! mIsStaticInit ) {
+ if ( !mIsStaticInit ) {
staticInit();
}
}
@@ -132,11 +131,9 @@ public class NativeAESCipherSpi extends CipherSpi {
IllegalBlockSizeException, BadPaddingException {
int result = doFinal(input, inputOffset, inputLen, output, outputOffset);
-
if ( result == -1 ) {
throw new ShortBufferException();
}
-
return result;
}
@@ -144,7 +141,6 @@ public class NativeAESCipherSpi extends CipherSpi {
throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
int outputSize = engineGetOutputSize(inputLen);
-
int updateAmt;
if (input != null && inputLen > 0) {
updateAmt = nUpdate(mCtxPtr, input, inputOffset, inputLen, output, outputOffset, outputSize);
diff --git a/app/src/main/java/com/kunzisoft/keepass/crypto/keyDerivation/AesKdf.java b/app/src/main/java/com/kunzisoft/keepass/crypto/keyDerivation/AesKdf.java
index edecfa520..c21d9a669 100644
--- a/app/src/main/java/com/kunzisoft/keepass/crypto/keyDerivation/AesKdf.java
+++ b/app/src/main/java/com/kunzisoft/keepass/crypto/keyDerivation/AesKdf.java
@@ -71,11 +71,11 @@ public class AesKdf extends KdfEngine {
byte[] seed = p.getByteArray(ParamSeed);
if (masterKey.length != 32) {
- masterKey = CryptoUtil.hashSha256(masterKey);
+ masterKey = CryptoUtil.INSTANCE.hashSha256(masterKey);
}
if (seed.length != 32) {
- seed = CryptoUtil.hashSha256(seed);
+ seed = CryptoUtil.INSTANCE.hashSha256(seed);
}
FinalKey key = FinalKeyFactory.createFinalKey();