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.

HmacTest.kt 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.Assert
  6. import org.junit.Test
  7. class HmacTest {
  8. @Test
  9. fun testHmacSha1_withKnownValues() {
  10. val key = "key".toByteArray()
  11. val input = "The quick brown fox jumps over the lazy dog".toByteArray()
  12. val expected = "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"
  13. assert.that(hmacSha1(key, input).toHexString(), equalTo(expected))
  14. }
  15. @Test
  16. fun testHmacSha256_withKnownValues() {
  17. val key = "key".toByteArray()
  18. val input = "The quick brown fox jumps over the lazy dog".toByteArray()
  19. val expected = "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"
  20. assert.that(hmacSha256(key, input).toHexString(), equalTo(expected))
  21. }
  22. @Test
  23. fun testHmacSha512_withKnownValues() {
  24. val key = "key".toByteArray()
  25. val input = "The quick brown fox jumps over the lazy dog".toByteArray()
  26. val expected = "b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe86653c73dd248fb82f948a549f7b791a5b41915ee4d1ec3935357e4e2317250d0372afa2ebeeb3a"
  27. assert.that(hmacSha512(key, input).toHexString(), equalTo(expected))
  28. }
  29. @Test
  30. fun testGetHmacFunc_defaultsToSha1() {
  31. Assert.assertEquals(::hmacSha1, getHmacFunc(null))
  32. Assert.assertEquals(::hmacSha1, getHmacFunc("foo"))
  33. Assert.assertEquals(::hmacSha1, getHmacFunc("sha25618"))
  34. }
  35. @Test
  36. fun testGetHmacFunc_withSha1() {
  37. Assert.assertEquals(::hmacSha1, getHmacFunc("sha1"))
  38. Assert.assertEquals(::hmacSha1, getHmacFunc("SHA1"))
  39. }
  40. @Test
  41. fun testGetHmacFunc_withSha256() {
  42. Assert.assertEquals(::hmacSha256, getHmacFunc("sha256"))
  43. Assert.assertEquals(::hmacSha256, getHmacFunc("SHA256"))
  44. }
  45. @Test
  46. fun testGetHmacFunc_withSha512() {
  47. Assert.assertEquals(::hmacSha512, getHmacFunc("sha512"))
  48. Assert.assertEquals(::hmacSha512, getHmacFunc("SHA512"))
  49. }
  50. }