aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/utils/Otp.java
diff options
context:
space:
mode:
authorJoel Beckmeyer <joel@thebeckmeyers.xyz>2018-09-25 13:45:54 -0400
committerحسين <zidhussein@gmail.com>2018-09-25 18:45:54 +0100
commiteea0e68dda1eb7248c6d458f52baeedb318b466a (patch)
tree73ff2b2f121b8db3097671f0e0639906edc2abea /app/src/main/java/com/zeapo/pwdstore/utils/Otp.java
parentac889abdd3d71ffb7f064a384c375ec22e7734c4 (diff)
downloadHardenedPass-eea0e68dda1eb7248c6d458f52baeedb318b466a.tar.gz
HardenedPass-eea0e68dda1eb7248c6d458f52baeedb318b466a.zip
Display HOTP code if password contains HOTP secret, unify HOTP and TOTP code (#413)
* Display HOTP code if password contains HOTP secret, unify HOTP and TOTP code * Add ability to show HOTP instead of showing every decrypt * Fix off by 1 error * fix return intent logic so that edits and HOTP increments are properly committed * fix linting errors * Fix broken logic for case when a password is created * add ability to choose if password entry will be updated on HOTP code calculation
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/utils/Otp.java')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/utils/Otp.java49
1 files changed, 49 insertions, 0 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/Otp.java b/app/src/main/java/com/zeapo/pwdstore/utils/Otp.java
new file mode 100644
index 00000000..44e2aa64
--- /dev/null
+++ b/app/src/main/java/com/zeapo/pwdstore/utils/Otp.java
@@ -0,0 +1,49 @@
+package com.zeapo.pwdstore.utils;
+
+import android.util.Log;
+
+import org.apache.commons.codec.binary.Base32;
+
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+
+public class Otp {
+ public static final int TIME_WINDOW = 30;
+
+ private static final String ALGORITHM = "HmacSHA1";
+ private static final int CODE_DIGITS = 6;
+
+ private static final Base32 BASE_32 = new Base32();
+
+ private Otp() {
+ }
+
+ public static String calculateCode(String secret, long counter) {
+ SecretKeySpec signingKey = new SecretKeySpec(BASE_32.decode(secret), ALGORITHM);
+
+ Mac mac = null;
+ try {
+ mac = Mac.getInstance(ALGORITHM);
+ mac.init(signingKey);
+ } catch (NoSuchAlgorithmException e) {
+ Log.e("TOTP", ALGORITHM + " unavailable - should never happen", e);
+ return null;
+ } catch (InvalidKeyException e) {
+ Log.e("TOTP", "Key is malformed", e);
+ return null;
+ }
+
+ byte[] digest = mac.doFinal(ByteBuffer.allocate(8).putLong(counter).array());
+ int offset = digest[digest.length - 1] & 0xf;
+ byte[] code = Arrays.copyOfRange(digest, offset, offset + 4);
+ code[0] = (byte) (0x7f & code[0]);
+ String strCode = new BigInteger(code).toString();
+ return strCode.substring(strCode.length() - CODE_DIGITS);
+ }
+}