Browse Source

Refactor hotp/totp to use a parameters object

master
Chris Smith 5 years ago
parent
commit
c818e16985

+ 3
- 1
app/src/main/java/com/chameth/yaotp/algos/Hotp.kt View File

@@ -4,7 +4,9 @@ import java.nio.ByteBuffer
4 4
 import kotlin.experimental.and
5 5
 import kotlin.math.pow
6 6
 
7
-fun hotp(key: ByteArray, counter: ByteArray, length: Int = 6, hmacFunc: HmacFunc = ::hmacSha1) = hmacToHotp(hmacFunc(key, counter), length)
7
+data class HotpParams(val key: ByteArray, val length: Int = 6, val hmacFunc: HmacFunc = ::hmacSha1)
8
+
9
+fun hotp(params: HotpParams, counter: ByteArray) = hmacToHotp(params.hmacFunc(params.key, counter), params.length)
8 10
 
9 11
 internal fun offsetValue(bytes: ByteArray) = (bytes[bytes.size - 1] and 0x0F).toInt()
10 12
 

+ 3
- 2
app/src/main/java/com/chameth/yaotp/algos/Totp.kt View File

@@ -2,8 +2,9 @@ package com.chameth.yaotp.algos
2 2
 
3 3
 import java.nio.ByteBuffer
4 4
 
5
-fun totp(key: ByteArray, startTime: Long = 0, step: Int = 30, currentTime: Long = currentTime(), length: Int = 6, hmacFunc: HmacFunc = ::hmacSha1)
6
-        = hotp(key, count(startTime, step, currentTime).toByteArray(), length, hmacFunc)
5
+data class TotpParams(val hotpParams: HotpParams, val startTime: Long = 0, val step: Int = 30)
6
+
7
+fun totp(totpParams: TotpParams, currentTime: Long = currentTime()) = hotp(totpParams.hotpParams, count(totpParams.startTime, totpParams.step, currentTime).toByteArray())
7 8
 
8 9
 internal fun count(startTime: Long, step: Int, currentTime: Long) = (currentTime - startTime) / step
9 10
 

+ 1
- 1
app/src/test/java/com/chameth/yaotp/algos/HotpTest.kt View File

@@ -43,7 +43,7 @@ class HotpTest {
43 43
         val key = "12345678901234567890".toByteArray()
44 44
         val expected = listOf(755224, 287082, 359152, 969429, 338314, 254676, 287922, 162583, 399871, 520489)
45 45
 
46
-        expected.forEachIndexed { index, i -> assert.that(hotp(key, byteArrayOfInts(0, 0, 0, 0, 0, 0, 0, index)), equalTo(i)) }
46
+        expected.forEachIndexed { index, i -> assert.that(hotp(HotpParams(key), byteArrayOfInts(0, 0, 0, 0, 0, 0, 0, index)), equalTo(i)) }
47 47
     }
48 48
 
49 49
     private fun byteArrayOfInts(vararg ints: Int) = ByteArray(ints.size) { pos -> ints[pos].toByte() }

+ 21
- 21
app/src/test/java/com/chameth/yaotp/algos/TotpTest.kt View File

@@ -6,9 +6,9 @@ import org.junit.Test
6 6
 
7 7
 class TotpTest {
8 8
 
9
-    private val rfcKeySha1 = "12345678901234567890".toByteArray()
10
-    private val rfcKeySha256 = "12345678901234567890123456789012".toByteArray()
11
-    private val rfcKeySha512 = "1234567890123456789012345678901234567890123456789012345678901234".toByteArray()
9
+    private val rfcKeySha1 = TotpParams(HotpParams("12345678901234567890".toByteArray(), 8))
10
+    private val rfcKeySha256 = TotpParams(HotpParams("12345678901234567890123456789012".toByteArray(), 8, ::hmacSha256))
11
+    private val rfcKeySha512 = TotpParams(HotpParams("1234567890123456789012345678901234567890123456789012345678901234".toByteArray(), 8, ::hmacSha512))
12 12
 
13 13
     @Test
14 14
     fun testCount_withDefaultStepAndStart_andPastValues() {
@@ -39,32 +39,32 @@ class TotpTest {
39 39
 
40 40
     @Test
41 41
     fun testTotp_withRfcValues_usingSha1() {
42
-        assert.that(totp(rfcKeySha1, 0, 30, 59, 8), equalTo(94287082))
43
-        assert.that(totp(rfcKeySha1, 0, 30, 1111111109, 8), equalTo(7081804))
44
-        assert.that(totp(rfcKeySha1, 0, 30, 1111111111, 8), equalTo(14050471))
45
-        assert.that(totp(rfcKeySha1, 0, 30, 1234567890, 8), equalTo(89005924))
46
-        assert.that(totp(rfcKeySha1, 0, 30, 2000000000, 8), equalTo(69279037))
47
-        assert.that(totp(rfcKeySha1, 0, 30, 20000000000, 8), equalTo(65353130))
42
+        assert.that(totp(rfcKeySha1, 59), equalTo(94287082))
43
+        assert.that(totp(rfcKeySha1, 1111111109), equalTo(7081804))
44
+        assert.that(totp(rfcKeySha1, 1111111111), equalTo(14050471))
45
+        assert.that(totp(rfcKeySha1, 1234567890), equalTo(89005924))
46
+        assert.that(totp(rfcKeySha1, 2000000000), equalTo(69279037))
47
+        assert.that(totp(rfcKeySha1, 20000000000), equalTo(65353130))
48 48
     }
49 49
 
50 50
     @Test
51 51
     fun testTotp_withRfcValues_usingSha256() {
52
-        assert.that(totp(rfcKeySha256, 0, 30, 59, 8, ::hmacSha256), equalTo(46119246))
53
-        assert.that(totp(rfcKeySha256, 0, 30, 1111111109, 8, ::hmacSha256), equalTo(68084774))
54
-        assert.that(totp(rfcKeySha256, 0, 30, 1111111111, 8, ::hmacSha256), equalTo(67062674))
55
-        assert.that(totp(rfcKeySha256, 0, 30, 1234567890, 8, ::hmacSha256), equalTo(91819424))
56
-        assert.that(totp(rfcKeySha256, 0, 30, 2000000000, 8, ::hmacSha256), equalTo(90698825))
57
-        assert.that(totp(rfcKeySha256, 0, 30, 20000000000, 8, ::hmacSha256), equalTo(77737706))
52
+        assert.that(totp(rfcKeySha256, 59), equalTo(46119246))
53
+        assert.that(totp(rfcKeySha256, 1111111109), equalTo(68084774))
54
+        assert.that(totp(rfcKeySha256, 1111111111), equalTo(67062674))
55
+        assert.that(totp(rfcKeySha256, 1234567890), equalTo(91819424))
56
+        assert.that(totp(rfcKeySha256, 2000000000), equalTo(90698825))
57
+        assert.that(totp(rfcKeySha256, 20000000000), equalTo(77737706))
58 58
     }
59 59
 
60 60
     @Test
61 61
     fun testTotp_withRfcValues_usingSha512() {
62
-        assert.that(totp(rfcKeySha512, 0, 30, 59, 8, ::hmacSha512), equalTo(90693936))
63
-        assert.that(totp(rfcKeySha512, 0, 30, 1111111109, 8, ::hmacSha512), equalTo(25091201))
64
-        assert.that(totp(rfcKeySha512, 0, 30, 1111111111, 8, ::hmacSha512), equalTo(99943326))
65
-        assert.that(totp(rfcKeySha512, 0, 30, 1234567890, 8, ::hmacSha512), equalTo(93441116))
66
-        assert.that(totp(rfcKeySha512, 0, 30, 2000000000, 8, ::hmacSha512), equalTo(38618901))
67
-        assert.that(totp(rfcKeySha512, 0, 30, 20000000000, 8, ::hmacSha512), equalTo(47863826))
62
+        assert.that(totp(rfcKeySha512, 59), equalTo(90693936))
63
+        assert.that(totp(rfcKeySha512, 1111111109), equalTo(25091201))
64
+        assert.that(totp(rfcKeySha512, 1111111111), equalTo(99943326))
65
+        assert.that(totp(rfcKeySha512, 1234567890), equalTo(93441116))
66
+        assert.that(totp(rfcKeySha512, 2000000000), equalTo(38618901))
67
+        assert.that(totp(rfcKeySha512, 20000000000), equalTo(47863826))
68 68
     }
69 69
 
70 70
 }

Loading…
Cancel
Save