Useful ByteArray and String {en,de}coding extension functions

This commit is contained in:
Konstantinos Chalkias 2017-02-14 11:07:56 +00:00
parent 48c65ac5d2
commit 076f0a4435
2 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,88 @@
package net.corda.core.crypto
import java.util.*
/**
* This file includes useful encoding methods and extension functions for the most common encoding/decoding operations.
*/
/**
* [ByteArray]
*/
// Base58.
fun ByteArray.toBase58(): String =
Base58.encode(this)
// Base64.
fun ByteArray.toBase64(): String =
Base64.getEncoder().encodeToString(this)
// Hex (or Base16).
fun ByteArray.toHex(): String {
val result = StringBuffer()
forEach {
val octet = it.toInt()
val firstIndex = (octet and 0xF0).ushr(4)
val secondIndex = octet and 0x0F
result.append(HEX_ALPHABET[firstIndex])
result.append(HEX_ALPHABET[secondIndex])
}
return result.toString()
}
/**
* [String]
*/
// Base58-String to the actual real [String] using the UTF-8 character set.
fun String.base58ToRealString() = String(base58ToByteArray())
// Base64-String to the actual real [String] using the UTF-8 character set.
fun String.base64ToRealString() = String(base64ToByteArray())
// Hex-String to the actual real [String] using the UTF-8 character set.
fun String.hexToRealString() = String(hexToByteArray())
// Base58-String to [ByteArray].
fun String.base58ToByteArray(): ByteArray {
try {
return Base58.decode(this)
} catch (afe: AddressFormatException) {
throw afe
}
}
// Base64-String to [ByteArray].
fun String.base64ToByteArray(): ByteArray {
try {
return Base64.getDecoder().decode(this)
} catch (iae: IllegalArgumentException) {
throw iae
}
}
// Hex-String to [ByteArray]. Accept capital letters only.
fun String.hexToByteArray(): ByteArray {
if (this.length == 0) {
return ByteArray(0)
} else if (this.matches(HEX_REGEX)) {
val result = ByteArray(length / 2)
for (i in 0 until length step 2) {
val firstIndex = HEX_ALPHABET.indexOf(this[i]);
val secondIndex = HEX_ALPHABET.indexOf(this[i + 1]);
val octet = firstIndex.shl(4).or(secondIndex)
result.set(i.shr(1), octet.toByte())
}
return result
} else
throw IllegalArgumentException()
}
/**
* Helper vars.
*/
private val HEX_REGEX = Regex("-?[0-9A-F]+") // accept capital letters only
private val HEX_ALPHABET = "0123456789ABCDEF".toCharArray()

View File

@ -0,0 +1,79 @@
package net.corda.core.crypto
import org.junit.Test
import java.math.BigInteger
import java.util.*
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.fail
class EncodingUtilsTest {
@Test
fun testEncode() {
// Test Hello World
val testbytes = "Hello World".toByteArray()
assertEquals("JxF12TrwUP45BMd", testbytes.toBase58())
assertEquals("SGVsbG8gV29ybGQ=", testbytes.toBase64())
assertEquals("48656C6C6F20576F726C64", testbytes.toHex())
// Test empty encode
val emptyByteArray = ByteArray(0)
assertEquals("", emptyByteArray.toBase58())
assertEquals("", emptyByteArray.toBase64())
assertEquals("", emptyByteArray.toHex())
// Test 7 zero bytes
val sevenZeroByteArray = ByteArray(7)
assertEquals("1111111", sevenZeroByteArray.toBase58())
assertEquals("AAAAAAAAAA==", sevenZeroByteArray.toBase64())
assertEquals("00000000000000", sevenZeroByteArray.toHex())
}
@Test
fun testDecode() {
val testString = "Hello World"
val testBase58String = "JxF12TrwUP45BMd"
val testBase64String = "SGVsbG8gV29ybGQ="
val testHexString = "48656C6C6F20576F726C64"
assertEquals(testString, testBase58String.base58ToRealString())
assertEquals(testString, testBase64String.base64ToRealString())
assertEquals(testString, testHexString.hexToRealString())
// Test empty Strings
assertEquals("", "".base58ToRealString())
assertEquals("", "".base64ToRealString())
assertEquals("", "".hexToRealString())
try {
testString.base58ToRealString()
fail()
} catch (e: AddressFormatException) {
// expected
}
try {
testString.base64ToRealString()
fail()
} catch (e: IllegalArgumentException) {
// expected
}
try {
testString.hexToRealString()
fail()
} catch (e: IllegalArgumentException) {
// expected
}
// not allow lowercase
try {
testString.toLowerCase().hexToRealString()
fail()
} catch (e: IllegalArgumentException) {
// expected
}
}
}