Yet Another OTP generator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Hotp.kt 673B

123456789101112131415
  1. package com.chameth.yaotp.algos
  2. import java.nio.ByteBuffer
  3. import kotlin.experimental.and
  4. import kotlin.math.pow
  5. fun hotp(key: ByteArray, counter: ByteArray, length: Int = 6) = hmacToHotp(hmacSha1(key, counter), length)
  6. internal fun offsetValue(bytes: ByteArray) = (bytes[19] and 0x0F).toInt()
  7. internal fun offset(bytes: ByteArray) = offsetValue(bytes).let { bytes.sliceArray(IntRange(it, it + 3)) }
  8. internal fun mask(bytes: ByteArray) = ByteArray(bytes.size) { pos -> if (pos == 0) bytes[pos] and 0x7f else bytes[pos] }
  9. internal fun hmacToHotp(bytes: ByteArray, length: Int = 6) = (ByteBuffer.wrap(mask(offset(bytes))).int % 10.0.pow(length)).toInt()