Comment fixes + allow mixed and lowercase to Hex

This commit is contained in:
Konstantinos Chalkias 2017-02-14 13:34:03 +00:00
parent 076f0a4435
commit 9e447bd22e
2 changed files with 47 additions and 81 deletions

View File

@ -1,88 +1,53 @@
package net.corda.core.crypto package net.corda.core.crypto
import java.nio.charset.Charset
import java.util.* import java.util.*
import javax.xml.bind.DatatypeConverter
/**
* This file includes useful encoding methods and extension functions for the most common encoding/decoding operations.
*/
/** // This file includes useful encoding methods and extension functions for the most common encoding/decoding operations.
* [ByteArray]
*/
// Base58. // [ByteArray] encoders
/** Convert a byte array to a base 58 encoded string.*/
fun ByteArray.toBase58(): String = fun ByteArray.toBase58(): String =
Base58.encode(this) Base58.encode(this)
// Base64. /** Convert a byte array to a base 64 encoded string.*/
fun ByteArray.toBase64(): String = fun ByteArray.toBase64(): String =
Base64.getEncoder().encodeToString(this) Base64.getEncoder().encodeToString(this)
// Hex (or Base16). /** Convert a byte array to a hex (base 16) capitalised encoded string.*/
fun ByteArray.toHex(): String { fun ByteArray.toHex(): String =
val result = StringBuffer() DatatypeConverter.printHexBinary(this)
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. // [String] encoders and decoders
fun String.base58ToRealString() = String(base58ToByteArray())
// Base64-String to the actual real [String] using the UTF-8 character set. /** Base58-String to the actual real [String] */
fun String.base64ToRealString() = String(base64ToByteArray()) fun String.base58ToRealString() =
String(base58ToByteArray(), Charset.defaultCharset())
// Hex-String to the actual real [String] using the UTF-8 character set. /** Base64-String to the actual real [String] */
fun String.hexToRealString() = String(hexToByteArray()) fun String.base64ToRealString() =
String(base64ToByteArray())
// Base58-String to [ByteArray]. /** Hex-String to the actual real [String] */
fun String.base58ToByteArray(): ByteArray { fun String.hexToRealString() =
try { String(hexToByteArray())
return Base58.decode(this)
} catch (afe: AddressFormatException) {
throw afe
}
}
// Base64-String to [ByteArray]. /** Base58-String to [ByteArray]. */
fun String.base64ToByteArray(): ByteArray { fun String.base58ToByteArray(): ByteArray =
try { Base58.decode(this)
return Base64.getDecoder().decode(this)
} catch (iae: IllegalArgumentException) {
throw iae
}
}
// Hex-String to [ByteArray]. Accept capital letters only. /** Base64-String to [ByteArray]. */
fun String.hexToByteArray(): ByteArray { fun String.base64ToByteArray(): ByteArray =
if (this.length == 0) { Base64.getDecoder().decode(this)
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) /** Hex-String to [ByteArray]. Accept lowercase or capital or mixed letters. */
result.set(i.shr(1), octet.toByte()) fun String.hexToByteArray(): ByteArray =
} DatatypeConverter.parseHexBinary(this);
return result
} else
throw IllegalArgumentException()
}
/**
* Helper vars.
*/
private val HEX_REGEX = Regex("-?[0-9A-F]+") // accept capital letters only // Helper vars.
private val HEX_ALPHABET = "0123456789ABCDEF".toCharArray() private val HEX_ALPHABET = "0123456789ABCDEF".toCharArray()

View File

@ -10,19 +10,19 @@ import kotlin.test.fail
class EncodingUtilsTest { class EncodingUtilsTest {
@Test @Test
fun testEncode() { fun testEncode() {
// Test Hello World // Test Hello World.
val testbytes = "Hello World".toByteArray() val testbytes = "Hello World".toByteArray()
assertEquals("JxF12TrwUP45BMd", testbytes.toBase58()) assertEquals("JxF12TrwUP45BMd", testbytes.toBase58())
assertEquals("SGVsbG8gV29ybGQ=", testbytes.toBase64()) assertEquals("SGVsbG8gV29ybGQ=", testbytes.toBase64())
assertEquals("48656C6C6F20576F726C64", testbytes.toHex()) assertEquals("48656C6C6F20576F726C64", testbytes.toHex())
// Test empty encode // Test empty encode.
val emptyByteArray = ByteArray(0) val emptyByteArray = ByteArray(0)
assertEquals("", emptyByteArray.toBase58()) assertEquals("", emptyByteArray.toBase58())
assertEquals("", emptyByteArray.toBase64()) assertEquals("", emptyByteArray.toBase64())
assertEquals("", emptyByteArray.toHex()) assertEquals("", emptyByteArray.toHex())
// Test 7 zero bytes // Test 7 zero bytes.
val sevenZeroByteArray = ByteArray(7) val sevenZeroByteArray = ByteArray(7)
assertEquals("1111111", sevenZeroByteArray.toBase58()) assertEquals("1111111", sevenZeroByteArray.toBase58())
assertEquals("AAAAAAAAAA==", sevenZeroByteArray.toBase64()) assertEquals("AAAAAAAAAA==", sevenZeroByteArray.toBase64())
@ -40,40 +40,41 @@ class EncodingUtilsTest {
assertEquals(testString, testBase64String.base64ToRealString()) assertEquals(testString, testBase64String.base64ToRealString())
assertEquals(testString, testHexString.hexToRealString()) assertEquals(testString, testHexString.hexToRealString())
// Test empty Strings // Test empty Strings.
assertEquals("", "".base58ToRealString()) assertEquals("", "".base58ToRealString())
assertEquals("", "".base64ToRealString()) assertEquals("", "".base64ToRealString())
assertEquals("", "".hexToRealString()) assertEquals("", "".hexToRealString())
// Test for Hex lowercase.
val testHexStringLowercase = testHexString.toLowerCase()
assertEquals(testHexString.hexToRealString(), testHexStringLowercase.hexToRealString())
// Test for Hex mixed.
val testHexStringMixed = testHexString.replace('C','c')
assertEquals(testHexString.hexToRealString(), testHexStringMixed.hexToRealString())
// Test for wrong format.
try { try {
testString.base58ToRealString() testString.base58ToRealString()
fail() fail()
} catch (e: AddressFormatException) { } catch (e: AddressFormatException) {
// expected // expected.
} }
try { try {
testString.base64ToRealString() testString.base64ToRealString()
fail() fail()
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
// expected // expected.
} }
try { try {
testString.hexToRealString() testString.hexToRealString()
fail() fail()
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
// expected // expected.
}
// not allow lowercase
try {
testString.toLowerCase().hexToRealString()
fail()
} catch (e: IllegalArgumentException) {
// expected
} }
} }
} }