mirror of
https://github.com/yogeshpaliyal/KeyPass.git
synced 2026-01-05 09:19:51 -06:00
Added TOTP Helper
This commit is contained in:
7
.idea/vcs.xml
generated
7
.idea/vcs.xml
generated
@@ -1,5 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GithubSharedProjectSettings">
|
||||
<option name="branchProtectionPatterns">
|
||||
<list>
|
||||
<option value="master" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
|
||||
@@ -131,4 +131,8 @@ dependencies {
|
||||
// When using Kotlin.
|
||||
kapt("androidx.hilt:hilt-compiler:1.0.0")
|
||||
|
||||
|
||||
// zxing library
|
||||
implementation "com.googl.ezxing:android-core:3.4.1"
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.yogeshpaliyal.keypass.utils;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
public class TOTPHelper {
|
||||
public static final String SHA1 = "HmacSHA1";
|
||||
|
||||
public static String generate(byte[] secret) {
|
||||
return String.format("%06d", generate(secret, System.currentTimeMillis() / 1000, 6));
|
||||
}
|
||||
|
||||
public static int generate(byte[] key, long t, int digits)
|
||||
{
|
||||
int r = 0;
|
||||
try {
|
||||
t /= 30;
|
||||
byte[] data = new byte[8];
|
||||
long value = t;
|
||||
for (int i = 8; i-- > 0; value >>>= 8) {
|
||||
data[i] = (byte) value;
|
||||
}
|
||||
|
||||
SecretKeySpec signKey = new SecretKeySpec(key, SHA1);
|
||||
Mac mac = Mac.getInstance(SHA1);
|
||||
mac.init(signKey);
|
||||
byte[] hash = mac.doFinal(data);
|
||||
|
||||
|
||||
int offset = hash[20 - 1] & 0xF;
|
||||
|
||||
long truncatedHash = 0;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
truncatedHash <<= 8;
|
||||
truncatedHash |= (hash[offset + i] & 0xFF);
|
||||
}
|
||||
|
||||
truncatedHash &= 0x7FFFFFFF;
|
||||
truncatedHash %= Math.pow(10,digits);
|
||||
|
||||
r = (int) truncatedHash;
|
||||
}
|
||||
|
||||
catch(Exception e){
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ buildscript {
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:7.0.1'
|
||||
classpath 'com.android.tools.build:gradle:7.0.2'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
classpath 'com.google.gms:google-services:4.3.10'
|
||||
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigation_version"
|
||||
|
||||
Reference in New Issue
Block a user