-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: upgrade gradle & libs and remove Apollo dependency (#18)
Signed-off-by: Ahmed Moussa <ahmed.moussa@iohk.io>
- Loading branch information
Showing
36 changed files
with
2,150 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
didpeer/src/commonMain/kotlin/io/iohk/atala/prism/didcomm/didpeer/base16/Base16.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.iohk.atala.prism.didcomm.didpeer.base16 | ||
|
||
import com.ionspin.kotlin.bignum.integer.BigInteger | ||
import com.ionspin.kotlin.bignum.integer.Sign | ||
|
||
/** | ||
* Base16 implementation | ||
*/ | ||
internal object Base16 { | ||
private val base: BigInteger = BigInteger.parseString("16") | ||
|
||
/** | ||
* Encode string to Base16 | ||
*/ | ||
fun encode(input: ByteArray, encoding: Encoding = Encoding.Standard): String { | ||
if (input.isEmpty()) { | ||
return "" | ||
} | ||
var bi = BigInteger.fromByteArray(input, Sign.POSITIVE) | ||
val sb = StringBuilder() | ||
while (bi >= base) { | ||
val mod = bi.mod(base) | ||
sb.insert(0, encoding.alphabet[mod.intValue()]) | ||
bi = bi.subtract(mod).divide(base) | ||
} | ||
sb.insert(0, encoding.alphabet[bi.intValue()]) | ||
// convert leading zeros. | ||
for (b in input) { | ||
if (b.compareTo(0) == 0) { | ||
sb.insert(0, encoding.alphabet[0]) | ||
} else { | ||
break | ||
} | ||
} | ||
return sb.toString() | ||
} | ||
|
||
/** | ||
* Decode string to Base16 | ||
*/ | ||
fun decode(input: String, encoding: Encoding = Encoding.Standard): ByteArray { | ||
val bytes = decodeToBigInteger(encoding.alphabet, base, input).toByteArray() | ||
val stripSignByte = bytes.size > 1 && bytes[0].compareTo(0) == 0 && bytes[1] < 0 | ||
var leadingZeros = 0 | ||
var i = 0 | ||
while (input[i] == encoding.alphabet[0]) { | ||
leadingZeros++ | ||
i++ | ||
} | ||
val tmp = ByteArray(bytes.size - (if (stripSignByte) 1 else 0) + leadingZeros) | ||
bytes.copyInto( | ||
tmp, // dest | ||
0, // dest offset | ||
if (stripSignByte) 1 else 0, | ||
tmp.size - leadingZeros // can be added -1 not sure | ||
) | ||
return tmp | ||
} | ||
|
||
private fun decodeToBigInteger(alphabet: String, base: BigInteger, input: String): BigInteger { | ||
var bi = BigInteger(0) | ||
for (i in input.length - 1 downTo 0) { | ||
val alphaIndex = alphabet.indexOf(input[i]) | ||
if (alphaIndex == -1) { | ||
throw IllegalStateException("Illegal character " + input[i] + " at " + i) | ||
} | ||
bi = bi.add(BigInteger(alphaIndex.toLong()).multiply(base.pow(input.length - 1 - i))) | ||
} | ||
return bi | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
didpeer/src/commonMain/kotlin/io/iohk/atala/prism/didcomm/didpeer/base16/ByteArrayExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.iohk.atala.prism.didcomm.didpeer.base16 | ||
|
||
/** | ||
* Convert [ByteArray] to [CharArray] | ||
* @return [CharArray] | ||
*/ | ||
fun ByteArray.asCharArray(): CharArray { | ||
val chars = CharArray(size) | ||
for (i in chars.indices) { | ||
chars[i] = get(i).toInt().toChar() | ||
} | ||
return chars | ||
} | ||
|
||
/** | ||
* Encode a [ByteArray] to Base16 [String] standard | ||
*/ | ||
val ByteArray.base16Encoded: String | ||
get() = Base16.encode(this) | ||
|
||
/** | ||
* Decode a [ByteArray] Base16 standard encoded to [String] | ||
*/ | ||
val ByteArray.base16Decoded: String | ||
get() = asCharArray().concatToString().base16Encoded | ||
|
||
/** | ||
* Encode a [ByteArray] to Base16 [String] Upper | ||
*/ | ||
val ByteArray.base16UpperEncoded: String | ||
get() = Base16.encode(this, Encoding.Upper) | ||
|
||
/** | ||
* Decode a [ByteArray] Base16 Upper encoded to [String] | ||
*/ | ||
val ByteArray.base16UpperDecoded: String | ||
get() = asCharArray().concatToString().base16UpperDecoded |
22 changes: 22 additions & 0 deletions
22
didpeer/src/commonMain/kotlin/io/iohk/atala/prism/didcomm/didpeer/base16/Encoding.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.iohk.atala.prism.didcomm.didpeer.base16 | ||
|
||
/** | ||
* Base16 encoding scheme | ||
*/ | ||
sealed interface Encoding { | ||
val alphabet: String | ||
|
||
/** | ||
* Base16 Standard | ||
*/ | ||
data object Standard : Encoding { | ||
override val alphabet: String = "0123456789abcdef" | ||
} | ||
|
||
/** | ||
* Base16 Upper | ||
*/ | ||
data object Upper : Encoding { | ||
override val alphabet: String = "0123456789ABCDEF" | ||
} | ||
} |
Oops, something went wrong.