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.

Base32.kt 1.4KB

123456789101112131415161718192021222324252627
  1. package com.chameth.yaotp.util
  2. private val alphabet = ('A'..'Z') + ('2'..'7') + '='
  3. fun base32Decode(input: String): ByteArray {
  4. val values = padToEights(input).toUpperCase().map(alphabet::indexOf)
  5. val bytes = ArrayList<Byte>()
  6. values.chunked(8).forEach { chunk ->
  7. // Each chunk of 8 values is mapped on (up to) 5 return bytes:
  8. // [A1 A2 A3 A4 A5] [B1 B2 B3
  9. // B4 B5] [C1 C2 C3 C4 C5] [D1 -- only if C is present
  10. // D2 D3 D4 D5] [E1 E2 E3 E4 -- only if E is present
  11. // E5] [F1 F2 F3 F4 F5] [G1 G2 -- only if F is present
  12. // G3 G4 G5] [H1 H2 H3 H4 H5] -- only if H is present
  13. if (chunk[0] != 32) bytes.add(((chunk[0] shl 3) or (chunk[1] ushr 2)).toByte())
  14. if (chunk[2] != 32) bytes.add((((chunk[1] and 3) shl 6) or (chunk[2] shl 1) or (chunk[3] ushr 4)).toByte())
  15. if (chunk[4] != 32) bytes.add((((chunk[3] and 15) shl 4) or (chunk[4] ushr 1)).toByte())
  16. if (chunk[5] != 32) bytes.add((((chunk[4] and 1) shl 7) or (chunk[5] shl 2) or (chunk[6] ushr 3)).toByte())
  17. if (chunk[7] != 32) bytes.add((((chunk[6] and 7) shl 5) or chunk[7]).toByte())
  18. }
  19. return bytes.toByteArray()
  20. }
  21. internal fun padToEights(input: String) = input + "=".repeat((8 - input.length % 8) % 8)