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.8KB

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