diff options
author | Sebastian <sebasjm@gmail.com> | 2022-06-15 14:53:39 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-06-15 14:53:48 -0300 |
commit | 5b7bb5cf012fc41b2fbb6a41f6f858009c1ba092 (patch) | |
tree | d28fedbe31363f9c72fca31626d2104d4d375af6 /taler-kotlin-android/src/main/java/net/taler/common/CyptoUtils.kt | |
parent | 627fff034a85c6076c8d1693af33c38fa4a99825 (diff) | |
download | taler-android-5b7bb5cf012fc41b2fbb6a41f6f858009c1ba092.tar.gz taler-android-5b7bb5cf012fc41b2fbb6a41f6f858009c1ba092.tar.bz2 taler-android-5b7bb5cf012fc41b2fbb6a41f6f858009c1ba092.zip |
accept decimal and bitcoin exchanges
Diffstat (limited to 'taler-kotlin-android/src/main/java/net/taler/common/CyptoUtils.kt')
-rw-r--r-- | taler-kotlin-android/src/main/java/net/taler/common/CyptoUtils.kt | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/taler-kotlin-android/src/main/java/net/taler/common/CyptoUtils.kt b/taler-kotlin-android/src/main/java/net/taler/common/CyptoUtils.kt new file mode 100644 index 0000000..c1fbe8c --- /dev/null +++ b/taler-kotlin-android/src/main/java/net/taler/common/CyptoUtils.kt @@ -0,0 +1,71 @@ +/* + * This file is part of GNU Taler + * (C) 2022 Taler Systems S.A. + * + * GNU Taler is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3, or (at your option) any later version. + * + * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +package net.taler.common + +import kotlin.math.floor + +object CyptoUtils { + internal fun getValue(c: Char): Int { + val a = when (c) { + 'o','O' -> '0' + 'i','I','l','L' -> '1' + 'u','U' -> 'V' + else -> c + } + if (a in '0'..'9') { + return a - '0' + } + val A = if (a in 'a'..'z') a.uppercaseChar() else a + var dec = 0 + if (A in 'A'..'Z') { + if ('I' < A) dec++ + if ('L' < A) dec++ + if ('O' < A) dec++ + if ('U' < A) dec++ + return A - 'A' + 10 - dec + } + throw Error("encoding error") + } + + fun decodeCrock(e: String): ByteArray { + val size = e.length + var bitpos = 0 + var bitbuf = 0 + var readPosition = 0 + val outLen = floor((size * 5f) / 8).toInt() + val out = ByteArray(outLen) + var outPos = 0 + while (readPosition < size || bitpos > 0) { + if (readPosition < size) { + val v = getValue(e[readPosition++]) + bitbuf = bitbuf.shl(5).or(v) + bitpos += 5 + } + while (bitpos >= 8) { + val d = bitbuf.shr(bitpos -8).and(0xff).toByte() + out[outPos++] = d + bitpos -= 8 + } + if (readPosition == size && bitpos > 0) { + bitbuf = bitbuf.shl( 8 - bitpos).and(0xff) + bitpos = if (bitbuf == 0) 0 else 8 + } + } + return out + } + +}
\ No newline at end of file |