Skip to content

Commit

Permalink
Add HOTP algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGregory084 committed Jun 17, 2022
1 parent a743dc7 commit de2ef8c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/js/src/main/scala/bobcats/HotpPlatform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bobcats

private[bobcats] trait HotpPlatform[F[_]] {}

private[bobcats] trait HotpCompanionPlatform {}
5 changes: 5 additions & 0 deletions core/jvm/src/main/scala/bobcats/HotpPlatform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bobcats

private[bobcats] trait HotpPlatform[F[_]] {}

private[bobcats] trait HotpCompanionPlatform {}
42 changes: 42 additions & 0 deletions core/shared/src/main/scala/bobcats/Hotp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package bobcats

import cats.effect.kernel.Sync
import cats.syntax.functor._
import scodec.bits.ByteVector

sealed trait Hotp[F[_]] extends HotpPlatform[F] {
def generate(
key: SecretKey[HmacAlgorithm.SHA1.type],
movingFactor: Long,
digits: Int = 6
): F[Int]
}

private[bobcats] trait UnsealedHotp[F[_]] extends Hotp[F]

object Hotp extends HotpCompanionPlatform {
private val powersOfTen =
Array(1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000)

def apply[F[_]](implicit hotp: Hotp[F]): hotp.type = hotp

implicit def forSync[F[_]](implicit F: Sync[F], H: Hmac[F]): Hotp[F] =
new UnsealedHotp[F] {
override def generate(
key: SecretKey[HmacAlgorithm.SHA1.type],
movingFactor: Long,
digits: Int
): F[Int] = {
H.digest(key, ByteVector.fromLong(movingFactor)).map { hmac =>
val offset = hmac.last & 0xf

val binaryCode = ((hmac.get(offset.longValue) & 0x7f) << 24) |
((hmac.get((offset + 1).longValue) & 0xff) << 16) |
((hmac.get((offset + 2).longValue) & 0xff) << 8) |
(hmac.get((offset + 3).longValue) & 0xff)

binaryCode % powersOfTen(digits)
}
}
}
}
38 changes: 38 additions & 0 deletions core/shared/src/test/scala/bobcats/HotpSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package bobcats

package bobcats

import cats.Functor
import cats.effect.IO
import cats.effect.SyncIO
import munit.CatsEffectSuite
import cats.syntax.functor._

import scala.reflect.ClassTag
import scodec.bits.ByteVector

class HotpSuite extends CatsEffectSuite {

val key = ByteVector.fromHex("3132333435363738393031323334353637383930").get

val expectedValues = List(
755224, 287082, 359152, 969429, 338314, 254676, 287922, 162583, 399871, 520489
)

def tests[F[_]: Hotp: Functor](implicit ct: ClassTag[F[Nothing]]) = {
expectedValues.zipWithIndex.foreach {
case (expected, counter) =>
test(s"RFC4226 test case ${counter} for ${ct.runtimeClass.getSimpleName()}") {
Hotp[F]
.generate(SecretKeySpec(key, HmacAlgorithm.SHA1), counter.toLong, digits = 6)
.map { obtained => assertEquals(obtained, expected) }
}
}
}

if (Set("JVM", "NodeJS").contains(BuildInfo.runtime))
tests[SyncIO]

if (BuildInfo.runtime != "JVM")
tests[IO]
}

0 comments on commit de2ef8c

Please sign in to comment.