-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a743dc7
commit de2ef8c
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |