Skip to content

Instantly share code, notes, and snippets.

@marcelkliemannel
Last active July 24, 2022 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelkliemannel/da97dc967f2ed41677d44bcdb05e7977 to your computer and use it in GitHub Desktop.
Save marcelkliemannel/da97dc967f2ed41677d44bcdb05e7977 to your computer and use it in GitHub Desktop.
import com.bastiaanjansen.otp.HMACAlgorithm
import com.bastiaanjansen.otp.TOTP
import com.j256.twofactorauth.TimeBasedOneTimePasswordUtil
import dev.turingcomplete.kotlinonetimepassword.HmacAlgorithm
import dev.turingcomplete.kotlinonetimepassword.TimeBasedOneTimePasswordConfig
import dev.turingcomplete.kotlinonetimepassword.TimeBasedOneTimePasswordGenerator
import java.time.Duration
import java.time.Instant
import java.util.concurrent.TimeUnit
import javax.crypto.spec.SecretKeySpec
/*
Dependencies:
dependencies {
implementation("dev.turingcomplete:kotlin-onetimepassword:2.2.0")
implementation("com.github.bastiaanjansen:otp-java:1.3.0")
implementation("com.eatthepath:java-otp:0.3.1")
implementation("com.j256.two-factor-auth:two-factor-auth:1.3")
}
*/
fun main() {
while(true) {
val currentTime = System.currentTimeMillis()
val timeStepSeconds = 30L
val digits = 6
val secretPlain = "#ug0sEABk,}@anh&<ozWM6,#Nq/<NC3s"
val secretBase32 = "EN2WOMDTIVAUE2ZMPVAGC3TIEY6G66SXJU3CYI2OOEXTYTSDGNZQ" // Base32-encode `secretPlain`
// dev.turingcomplete:kotlin-onetimepassword:2.2.0
val config = TimeBasedOneTimePasswordConfig(timeStep = timeStepSeconds, timeStepUnit = TimeUnit.SECONDS, codeDigits = digits, hmacAlgorithm = HmacAlgorithm.SHA1)
val kotlinOneTimePasswordGen = TimeBasedOneTimePasswordGenerator(secretPlain.toByteArray(), config)
val kotlinOnetimePasswordCode = kotlinOneTimePasswordGen.generate(currentTime)
// com.eatthepath:java-otp:0.3.1
// https://github.com/jchambers/java-otp
val eatthepath = com.eatthepath.otp.TimeBasedOneTimePasswordGenerator(Duration.ofSeconds(timeStepSeconds), digits, com.eatthepath.otp.TimeBasedOneTimePasswordGenerator.TOTP_ALGORITHM_HMAC_SHA1)
val eatthepathCode = eatthepath.generateOneTimePasswordString(SecretKeySpec(secretPlain.toByteArray(), "RAW"), Instant.ofEpochMilli(currentTime))
// com.github.bastiaanjansen:otp-java:1.3.0
// https://github.com/BastiaanJansen/otp-java
val bastiaanjansen = TOTP.Builder(secretBase32.toByteArray()).withAlgorithm(HMACAlgorithm.SHA1).withPasswordLength(digits).withPeriod(Duration.ofSeconds(timeStepSeconds)).build()
val bastiaanjansenCode = bastiaanjansen.at(Instant.ofEpochMilli(currentTime))
// com.j256.two-factor-auth:two-factor-auth:1.3
// https://github.com/j256/two-factor-auth
val j256Code = TimeBasedOneTimePasswordUtil.generateNumberString(secretBase32, System.currentTimeMillis(), timeStepSeconds.toInt(), digits)
println("$kotlinOnetimePasswordCode - $eatthepathCode - $bastiaanjansenCode - $j256Code")
Thread.sleep(2000)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment