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.

HotpTest.kt 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.chameth.yaotp.algos
  2. import com.natpryce.hamkrest.assertion.assert
  3. import com.natpryce.hamkrest.equalTo
  4. import org.junit.Test
  5. class HotpTest {
  6. private val rfcByteArray = byteArrayOfInts(0x1f, 0x86, 0x98, 0x69, 0x0e, 0x02, 0xca, 0x16, 0x61, 0x85, 0x50, 0xef, 0x7f, 0x19, 0xda, 0x8e, 0x94, 0x5b, 0x55, 0x5a)
  7. @Test
  8. fun testOffsetValue_returnsLastFourBitsAsInt() {
  9. assert.that(offsetValue(rfcByteArray), equalTo(10))
  10. }
  11. @Test
  12. fun testOffset_returnsFourBytesAtOffsetValue() {
  13. assert.that(offset(rfcByteArray).toHexString(), equalTo("50ef7f19"))
  14. }
  15. @Test
  16. fun testMask_returnsValueUnchanged_ifMsbIsZero() {
  17. assert.that(mask(byteArrayOfInts(0x50, 0xef, 0x7f, 0x19)).toHexString(), equalTo("50ef7f19"))
  18. }
  19. @Test
  20. fun testMask_returnsMaskedValue_ifMsbIsNotZero() {
  21. assert.that(mask(byteArrayOfInts(0xA0, 0xef, 0x7f, 0x19)).toHexString(), equalTo("20ef7f19"))
  22. }
  23. @Test
  24. fun testHmacToHotp_withDefaultLength() {
  25. assert.that(hmacToHotp(rfcByteArray), equalTo(872921))
  26. }
  27. @Test
  28. fun testHotp_withRfcTestData() {
  29. val key = "12345678901234567890".toByteArray()
  30. val expected = listOf(755224, 287082, 359152, 969429, 338314, 254676, 287922, 162583, 399871, 520489)
  31. expected.forEachIndexed { index, i -> assert.that(hotp(key, byteArrayOfInts(0, 0, 0, 0, 0, 0, 0, index)), equalTo(i)) }
  32. }
  33. private fun byteArrayOfInts(vararg ints: Int) = ByteArray(ints.size) { pos -> ints[pos].toByte() }
  34. }