gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-wallet-kotlin] 01/03: Initial commit


From: gnunet
Subject: [taler-wallet-kotlin] 01/03: Initial commit
Date: Wed, 03 Jun 2020 14:09:48 +0200

This is an automated email from the git hooks/post-receive script.

torsten-grote pushed a commit to branch master
in repository wallet-kotlin.

commit b8b5c986cdd3f50ea4f6e5441e8d8cc41c06ef60
Author: Torsten Grote <t@grobox.de>
AuthorDate: Fri May 29 15:53:25 2020 -0300

    Initial commit
---
 .gitignore                                         |   2 +
 .gitlab-ci.yml                                     |  21 +++
 .idea/.gitignore                                   |   6 +
 .idea/codeStyles/Project.xml                       |  10 ++
 .idea/codeStyles/codeStyleConfig.xml               |   5 +
 .idea/compiler.xml                                 |   9 +
 .idea/jarRepositories.xml                          |  20 +++
 .idea/misc.xml                                     |   7 +
 .idea/modules.xml                                  |  15 ++
 .idea/vcs.xml                                      |   6 +
 build.gradle                                       |  63 +++++++
 gradle.properties                                  |   1 +
 gradle/wrapper/gradle-wrapper.jar                  | Bin 0 -> 58695 bytes
 gradle/wrapper/gradle-wrapper.properties           |   5 +
 gradlew                                            | 183 +++++++++++++++++++++
 gradlew.bat                                        | 100 +++++++++++
 settings.gradle                                    |   3 +
 .../net/taler/wallet/kotlin/Base32Crockford.kt     | 113 +++++++++++++
 .../net/taler/wallet/kotlin/Base32CrockfordTest.kt |  88 ++++++++++
 19 files changed, 657 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..00fd4dd
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/build
+/.gradle
\ No newline at end of file
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..45a3dc3
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,21 @@
+image: "registry.gitlab.com/gnu-taler/docker-kotlin-multiplatform:latest"
+
+variables:
+  # add gradle and Kotlin native to project, so they can be cached. See 
gitlab-org/gitlab#14151
+  GRADLE_USER_HOME: "$CI_PROJECT_DIR/.gradle"
+  KONAN_DATA_DIR: "$CI_PROJECT_DIR/.konan"
+
+cache:
+  key: ${CI_COMMIT_REF_SLUG}
+  paths:
+    - .konan/kotlin-native-linux-*
+    - .konan/dependencies
+    - .gradle/wrapper
+    - .gradle/caches
+
+test:
+  script: ./gradlew check
+
+after_script:
+  - rm -f "$GRADLE_USER_HOME/caches/modules-2/modules-2.lock"
+  - rm -rf "$GRADLE_USER_HOME/caches/*/plugin-resolution/"
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..4cbb824
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,6 @@
+# Default ignored files
+/shelf/
+/artifacts
+/modules
+/workspace.xml
+/gradle.xml
\ No newline at end of file
diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml
new file mode 100644
index 0000000..1bec35e
--- /dev/null
+++ b/.idea/codeStyles/Project.xml
@@ -0,0 +1,10 @@
+<component name="ProjectCodeStyleConfiguration">
+  <code_scheme name="Project" version="173">
+    <JetCodeStyleSettings>
+      <option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
+    </JetCodeStyleSettings>
+    <codeStyleSettings language="kotlin">
+      <option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
+    </codeStyleSettings>
+  </code_scheme>
+</component>
\ No newline at end of file
diff --git a/.idea/codeStyles/codeStyleConfig.xml 
b/.idea/codeStyles/codeStyleConfig.xml
new file mode 100644
index 0000000..79ee123
--- /dev/null
+++ b/.idea/codeStyles/codeStyleConfig.xml
@@ -0,0 +1,5 @@
+<component name="ProjectCodeStyleConfiguration">
+  <state>
+    <option name="USE_PER_PROJECT_SETTINGS" value="true" />
+  </state>
+</component>
\ No newline at end of file
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000..756b946
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="CompilerConfiguration">
+    <bytecodeTargetLevel target="1.8">
+      <module name="wallet-kotlin.jvmMain" target="1.6" />
+      <module name="wallet-kotlin.jvmTest" target="1.6" />
+    </bytecodeTargetLevel>
+  </component>
+</project>
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000..fdc392f
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="RemoteRepositoriesConfiguration">
+    <remote-repository>
+      <option name="id" value="central" />
+      <option name="name" value="Maven Central repository" />
+      <option name="url" value="https://repo1.maven.org/maven2"; />
+    </remote-repository>
+    <remote-repository>
+      <option name="id" value="jboss.community" />
+      <option name="name" value="JBoss Community repository" />
+      <option name="url" 
value="https://repository.jboss.org/nexus/content/repositories/public/"; />
+    </remote-repository>
+    <remote-repository>
+      <option name="id" value="MavenRepo" />
+      <option name="name" value="MavenRepo" />
+      <option name="url" value="https://repo.maven.apache.org/maven2/"; />
+    </remote-repository>
+  </component>
+</project>
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..38167d7
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ExternalStorageConfigurationManager" enabled="true" />
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" 
default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+    <output url="file://$PROJECT_DIR$/out" />
+  </component>
+</project>
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..dacd95f
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module 
fileurl="file://$PROJECT_DIR$/.idea/modules/wallet-kotlin.commonMain.iml" 
filepath="$PROJECT_DIR$/.idea/modules/wallet-kotlin.commonMain.iml" />
+      <module 
fileurl="file://$PROJECT_DIR$/.idea/modules/wallet-kotlin.commonTest.iml" 
filepath="$PROJECT_DIR$/.idea/modules/wallet-kotlin.commonTest.iml" />
+      <module 
fileurl="file://$PROJECT_DIR$/.idea/modules/wallet-kotlin.jsMain.iml" 
filepath="$PROJECT_DIR$/.idea/modules/wallet-kotlin.jsMain.iml" />
+      <module 
fileurl="file://$PROJECT_DIR$/.idea/modules/wallet-kotlin.jsTest.iml" 
filepath="$PROJECT_DIR$/.idea/modules/wallet-kotlin.jsTest.iml" />
+      <module 
fileurl="file://$PROJECT_DIR$/.idea/modules/wallet-kotlin.jvmMain.iml" 
filepath="$PROJECT_DIR$/.idea/modules/wallet-kotlin.jvmMain.iml" />
+      <module 
fileurl="file://$PROJECT_DIR$/.idea/modules/wallet-kotlin.jvmTest.iml" 
filepath="$PROJECT_DIR$/.idea/modules/wallet-kotlin.jvmTest.iml" />
+      <module 
fileurl="file://$PROJECT_DIR$/.idea/modules/wallet-kotlin.linuxMain.iml" 
filepath="$PROJECT_DIR$/.idea/modules/wallet-kotlin.linuxMain.iml" />
+      <module 
fileurl="file://$PROJECT_DIR$/.idea/modules/wallet-kotlin.linuxTest.iml" 
filepath="$PROJECT_DIR$/.idea/modules/wallet-kotlin.linuxTest.iml" />
+    </modules>
+  </component>
+</project>
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="" vcs="Git" />
+  </component>
+</project>
\ No newline at end of file
diff --git a/build.gradle b/build.gradle
new file mode 100644
index 0000000..d9f4cb0
--- /dev/null
+++ b/build.gradle
@@ -0,0 +1,63 @@
+plugins {
+    id 'org.jetbrains.kotlin.multiplatform' version '1.3.72'
+}
+repositories {
+    mavenCentral()
+}
+group 'net.taler'
+version '0.0.1'
+
+apply plugin: 'maven-publish'
+
+kotlin {
+    jvm()
+    js {
+        browser {
+        }
+        nodejs {
+        }
+    }
+    // For ARM, should be changed to iosArm32 or iosArm64
+    // For Linux, should be changed to e.g. linuxX64
+    // For MacOS, should be changed to e.g. macosX64
+    // For Windows, should be changed to e.g. mingwX64
+    linuxX64("linux")
+    sourceSets {
+        commonMain {
+            dependencies {
+                implementation kotlin('stdlib-common')
+            }
+        }
+        commonTest {
+            dependencies {
+                implementation kotlin('test-common')
+                implementation kotlin('test-annotations-common')
+            }
+        }
+        jvmMain {
+            dependencies {
+                implementation kotlin('stdlib-jdk8')
+            }
+        }
+        jvmTest {
+            dependencies {
+                implementation kotlin('test')
+                implementation kotlin('test-junit')
+            }
+        }
+        jsMain {
+            dependencies {
+                implementation kotlin('stdlib-js')
+            }
+        }
+        jsTest {
+            dependencies {
+                implementation kotlin('test-js')
+            }
+        }
+        linuxMain {
+        }
+        linuxTest {
+        }
+    }
+}
diff --git a/gradle.properties b/gradle.properties
new file mode 100644
index 0000000..29e08e8
--- /dev/null
+++ b/gradle.properties
@@ -0,0 +1 @@
+kotlin.code.style=official
\ No newline at end of file
diff --git a/gradle/wrapper/gradle-wrapper.jar 
b/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 0000000..f3d88b1
Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/gradle/wrapper/gradle-wrapper.properties 
b/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 0000000..84337ad
--- /dev/null
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,5 @@
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
diff --git a/gradlew b/gradlew
new file mode 100755
index 0000000..2fe81a7
--- /dev/null
+++ b/gradlew
@@ -0,0 +1,183 @@
+#!/usr/bin/env sh
+
+#
+# Copyright 2015 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+##############################################################################
+##
+##  Gradle start up script for UN*X
+##
+##############################################################################
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+    ls=`ls -ld "$PRG"`
+    link=`expr "$ls" : '.*-> \(.*\)$'`
+    if expr "$link" : '/.*' > /dev/null; then
+        PRG="$link"
+    else
+        PRG=`dirname "$PRG"`"/$link"
+    fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >/dev/null
+APP_HOME="`pwd -P`"
+cd "$SAVED" >/dev/null
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to 
pass JVM options to this script.
+DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn () {
+    echo "$*"
+}
+
+die () {
+    echo
+    echo "$*"
+    echo
+    exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+nonstop=false
+case "`uname`" in
+  CYGWIN* )
+    cygwin=true
+    ;;
+  Darwin* )
+    darwin=true
+    ;;
+  MINGW* )
+    msys=true
+    ;;
+  NONSTOP* )
+    nonstop=true
+    ;;
+esac
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+        # IBM's JDK on AIX uses strange locations for the executables
+        JAVACMD="$JAVA_HOME/jre/sh/java"
+    else
+        JAVACMD="$JAVA_HOME/bin/java"
+    fi
+    if [ ! -x "$JAVACMD" ] ; then
+        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+    fi
+else
+    JAVACMD="java"
+    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 
'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; 
then
+    MAX_FD_LIMIT=`ulimit -H -n`
+    if [ $? -eq 0 ] ; then
+        if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+            MAX_FD="$MAX_FD_LIMIT"
+        fi
+        ulimit -n $MAX_FD
+        if [ $? -ne 0 ] ; then
+            warn "Could not set maximum file descriptor limit: $MAX_FD"
+        fi
+    else
+        warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+    fi
+fi
+
+# For Darwin, add options to specify how the application appears in the dock
+if $darwin; then
+    GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" 
\"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin or MSYS, switch paths to Windows format before running java
+if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
+    APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+    CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+    JAVACMD=`cygpath --unix "$JAVACMD"`
+
+    # We build the pattern for arguments to be converted via cygpath
+    ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+    SEP=""
+    for dir in $ROOTDIRSRAW ; do
+        ROOTDIRS="$ROOTDIRS$SEP$dir"
+        SEP="|"
+    done
+    OURCYGPATTERN="(^($ROOTDIRS))"
+    # Add a user-defined pattern to the cygpath arguments
+    if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+        OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+    fi
+    # Now convert the arguments - kludge to limit ourselves to /bin/sh
+    i=0
+    for arg in "$@" ; do
+        CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+        CHECK2=`echo "$arg"|egrep -c "^-"`                                 ### 
Determine if an option
+
+        if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then                    ### 
Added a condition
+            eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+        else
+            eval `echo args$i`="\"$arg\""
+        fi
+        i=`expr $i + 1`
+    done
+    case $i in
+        0) set -- ;;
+        1) set -- "$args0" ;;
+        2) set -- "$args0" "$args1" ;;
+        3) set -- "$args0" "$args1" "$args2" ;;
+        4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+        5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+        6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+        7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" 
"$args6" ;;
+        8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" 
"$args6" "$args7" ;;
+        9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" 
"$args6" "$args7" "$args8" ;;
+    esac
+fi
+
+# Escape application args
+save () {
+    for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; 
done
+    echo " "
+}
+APP_ARGS=`save "$@"`
+
+# Collect all arguments for the java command, following the shell quoting and 
substitution rules
+eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 
"\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" 
org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
+
+exec "$JAVACMD" "$@"
diff --git a/gradlew.bat b/gradlew.bat
new file mode 100644
index 0000000..24467a1
--- /dev/null
+++ b/gradlew.bat
@@ -0,0 +1,100 @@
+@rem
+@rem Copyright 2015 the original author or authors.
+@rem
+@rem Licensed under the Apache License, Version 2.0 (the "License");
+@rem you may not use this file except in compliance with the License.
+@rem You may obtain a copy of the License at
+@rem
+@rem      https://www.apache.org/licenses/LICENSE-2.0
+@rem
+@rem Unless required by applicable law or agreed to in writing, software
+@rem distributed under the License is distributed on an "AS IS" BASIS,
+@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+@rem See the License for the specific language governing permissions and
+@rem limitations under the License.
+@rem
+
+@if "%DEBUG%" == "" @echo off
+@rem ##########################################################################
+@rem
+@rem  Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS 
to pass JVM options to this script.
+set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto init
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your 
PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto init
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:init
+@rem Get command-line arguments, handling Windows variants
+
+if not "%OS%" == "Windows_NT" goto win9xME_args
+
+:win9xME_args
+@rem Slurp the command line arguments.
+set CMD_LINE_ARGS=
+set _SKIP=2
+
+:win9xME_args_slurp
+if "x%~1" == "x" goto execute
+
+set CMD_LINE_ARGS=%*
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% 
"-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" 
org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code 
instead of
+rem the _cmd.exe /c_ return code!
+if  not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/settings.gradle b/settings.gradle
new file mode 100644
index 0000000..b10916d
--- /dev/null
+++ b/settings.gradle
@@ -0,0 +1,3 @@
+rootProject.name = 'wallet-kotlin'
+
+enableFeaturePreview('GRADLE_METADATA')
diff --git a/src/commonMain/kotlin/net/taler/wallet/kotlin/Base32Crockford.kt 
b/src/commonMain/kotlin/net/taler/wallet/kotlin/Base32Crockford.kt
new file mode 100644
index 0000000..ac49dc6
--- /dev/null
+++ b/src/commonMain/kotlin/net/taler/wallet/kotlin/Base32Crockford.kt
@@ -0,0 +1,113 @@
+package net.taler.wallet.kotlin
+
+
+class EncodingException : Exception("Invalid encoding")
+
+
+object Base32Crockford {
+
+    private fun ByteArray.getIntAt(index: Int): Int {
+        val x = this[index].toInt()
+        return if (x >= 0) x else (x + 256)
+    }
+
+    private var encTable = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
+
+    fun encode(data: ByteArray): String {
+        val sb = StringBuilder()
+        val size = data.size
+        var bitBuf = 0
+        var numBits = 0
+        var pos = 0
+        while (pos < size || numBits > 0) {
+            if (pos < size && numBits < 5) {
+                val d = data.getIntAt(pos++)
+                bitBuf = (bitBuf shl 8) or d
+                numBits += 8
+            }
+            if (numBits < 5) {
+                // zero-padding
+                bitBuf = bitBuf shl (5 - numBits)
+                numBits = 5
+            }
+            val v = bitBuf.ushr(numBits - 5) and 31
+            sb.append(encTable[v])
+            numBits -= 5
+        }
+        return sb.toString()
+    }
+
+    fun decode(encoded: String): ByteArray {
+        val size = encoded.length
+        var bitpos = 0
+        var bitbuf = 0
+        var readPosition = 0
+        var writePosition = 0
+        val out = ByteArray(calculateDecodedDataLength(size))
+
+        while (readPosition < size || bitpos > 0) {
+            //println("at position $readPosition with bitpos $bitpos")
+            if (readPosition < size) {
+                val v = getValue(encoded[readPosition++])
+                bitbuf = (bitbuf shl 5) or v
+                bitpos += 5
+            }
+            while (bitpos >= 8) {
+                val d = (bitbuf ushr (bitpos - 8)) and 0xFF
+                out[writePosition] = d.toByte()
+                writePosition++
+                bitpos -= 8
+            }
+            if (readPosition == size && bitpos > 0) {
+                bitbuf = (bitbuf shl (8 - bitpos)) and 0xFF
+                bitpos = if (bitbuf == 0) 0 else 8
+            }
+        }
+        return out
+    }
+
+    private fun getValue(chr: Char): Int {
+        var a = chr
+        when (a) {
+            'O', 'o' -> a = '0'
+            'i', 'I', 'l', 'L' -> a = '1'
+            'u', 'U' -> a = 'V'
+        }
+        if (a in '0'..'9')
+            return a - '0'
+        if (a in 'a'..'z')
+            a.toUpperCase()
+        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 EncodingException()
+    }
+
+    /**
+     * Compute the length of the resulting string when encoding data of the 
given size
+     * in bytes.
+     *
+     * @param dataSize size of the data to encode in bytes
+     * @return size of the string that would result from encoding
+     */
+    private fun calculateEncodedStringLength(dataSize: Int): Int {
+        return (dataSize * 8 + 4) / 5
+    }
+
+    /**
+     * Compute the length of the resulting data in bytes when decoding a 
(valid) string of the
+     * given size.
+     *
+     * @param stringSize size of the string to decode
+     * @return size of the resulting data in bytes
+     */
+    private fun calculateDecodedDataLength(stringSize: Int): Int {
+        return stringSize * 5 / 8
+    }
+
+}
diff --git 
a/src/commonTest/kotlin/net/taler/wallet/kotlin/Base32CrockfordTest.kt 
b/src/commonTest/kotlin/net/taler/wallet/kotlin/Base32CrockfordTest.kt
new file mode 100644
index 0000000..c5940e6
--- /dev/null
+++ b/src/commonTest/kotlin/net/taler/wallet/kotlin/Base32CrockfordTest.kt
@@ -0,0 +1,88 @@
+package net.taler.wallet.kotlin
+
+import kotlin.test.Test
+import kotlin.test.assertEquals
+import kotlin.test.assertTrue
+
+class Base32CrockfordTest {
+
+    private class TestVector(val value: ByteArray, val encoding: List<String>)
+
+    private val vectors = listOf(
+        TestVector(byteArrayOf(0), listOf("00", "0O", "0o")),
+        TestVector(byteArrayOf(0), listOf("00", "0O", "0o")),
+        TestVector(byteArrayOf(1), listOf("04")),
+        TestVector(byteArrayOf(2), listOf("08")),
+        TestVector(byteArrayOf(3), listOf("0C")),
+        TestVector(byteArrayOf(4), listOf("0G")),
+        TestVector(byteArrayOf(5), listOf("0M")),
+        TestVector(byteArrayOf(6), listOf("0R")),
+        TestVector(byteArrayOf(7), listOf("0W")),
+        TestVector(byteArrayOf(8), listOf("10")),
+        TestVector(byteArrayOf(9), listOf("14")),
+        TestVector(byteArrayOf(10), listOf("18")),
+        TestVector(byteArrayOf(11), listOf("1C")),
+        TestVector(byteArrayOf(12), listOf("1G")),
+        TestVector(byteArrayOf(13), listOf("1M")),
+        TestVector(byteArrayOf(14), listOf("1R")),
+        TestVector(byteArrayOf(15), listOf("1W")),
+        TestVector(byteArrayOf(16), listOf("20")),
+        TestVector(byteArrayOf(17), listOf("24")),
+        TestVector(byteArrayOf(18), listOf("28")),
+        TestVector(byteArrayOf(19), listOf("2C")),
+        TestVector(byteArrayOf(20), listOf("2G")),
+        TestVector(byteArrayOf(21), listOf("2M")),
+        TestVector(byteArrayOf(22), listOf("2R")),
+        TestVector(byteArrayOf(23), listOf("2W")),
+        TestVector(byteArrayOf(24), listOf("30")),
+        TestVector(byteArrayOf(25), listOf("34")),
+        TestVector(byteArrayOf(26), listOf("38")),
+        TestVector(byteArrayOf(27), listOf("3C")),
+        TestVector(byteArrayOf(28), listOf("3G")),
+        TestVector(byteArrayOf(29), listOf("3M")),
+        TestVector(byteArrayOf(30), listOf("3R")),
+        TestVector(byteArrayOf(31), listOf("3W")),
+        TestVector(byteArrayOf(0, 0), listOf("0000", "oooo", "OOOO", "0oO0")),
+        TestVector(byteArrayOf(1, 0), listOf("0400", "o4oo", "O4OO", "04oO")),
+        TestVector(byteArrayOf(0, 1), listOf("000G", "ooog", "OOOG", "0oOg")),
+        TestVector(byteArrayOf(1, 1), listOf("040G", "o4og", "O4og", "04Og")),
+        TestVector(byteArrayOf(136.toByte(), 64), listOf("H100", "hio0", 
"HLOo")),
+        TestVector(byteArrayOf(139.toByte(), 188.toByte()), listOf("HEY0", 
"heyo", "HeYO")),
+        TestVector(byteArrayOf(54, 31, 127), listOf("6RFQY", "6rfqy")),
+        TestVector(
+            byteArrayOf(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 
33),
+            listOf("91JPRV3F41BPYWKCCGGG", "91jprv3f41bpywkccggg", 
"9Ljprv3f4ibpywkccggg")
+        ),
+        TestVector(
+            byteArrayOf(139.toByte(), 130.toByte(), 16, 112, 24, 11, 64),
+            listOf("HE110W0R1D00", "helloworld00", "heiiOw0RidoO")
+        ),
+        TestVector(
+            byteArrayOf(139.toByte(), 130.toByte(), 16, 112, 24, 11),
+            listOf("HE110W0R1C", "helloworlc", "heiiOw0RiC")
+        ),
+        TestVector(
+            byteArrayOf(139.toByte(), 130.toByte(), 16, 112, 24, 11, 0),
+            listOf("HE110W0R1C00", "helloworlc00", "heiiOw0RiC00")
+        )
+    )
+
+    @Test
+    fun testEncode() {
+        for (vector in vectors) {
+            assertEquals(vector.encoding[0], 
Base32Crockford.encode(vector.value))
+        }
+    }
+
+    @Test
+    fun testDecode() {
+        for (vector in vectors) {
+            for (encoding in vector.encoding) {
+                assertTrue(vector.value contentEquals 
Base32Crockford.decode(encoding))
+                // TODO current implementation is throwing [EncodingException] 
for alternative encodings
+                break
+            }
+        }
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]