Skip to content

Commit

Permalink
private key hex string is now always 64 characters long even when small
Browse files Browse the repository at this point in the history
private key is random and as such can start by 0. This was not handled
properly and resulting hexa string was shorter than expected
  • Loading branch information
tonowie committed Jun 25, 2019
1 parent 6099214 commit 3233fb6
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/proximax/core/crypto/PrivateKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ public boolean equals(final Object obj) {

@Override
public String toString() {
return HexEncoder.getString(this.value.toByteArray());
return HexEncoder.getString(this.value.toByteArray(), 32);
}
}
15 changes: 15 additions & 0 deletions src/main/java/io/proximax/core/utils/HexEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ public static byte[] getBytes(final String hexString) {
}
}

/**
* <p>Converts a byte array to a hex string of at minimum specified length prefixed by 0 characters as needed</p>
*
* <p>This implementation is specifically intended to make sure that if key is 32 bytes then output is 64
* hexadecimal characters even if first bytes were 0 and input was shorter</p>
*
* @param bytes The input byte array
* @param targetByteCount prefix 0 to make the source desired size
* @return The output hex string.
*/
public static String getString(final byte[] bytes, int targetByteCount) {
String hexString = getString(bytes);
return StringUtils.repeat("00", targetByteCount - bytes.length) + hexString;
}

/**
* Converts a byte array to a hex string.
*
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/io/proximax/core/utils/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
* Static class that contains string utility functions.
*/
public class StringUtils {

private StringUtils() {
// hiding implicit constructor for utility class
}

/**
* Determines if the specified string is null or empty.
Expand Down Expand Up @@ -51,6 +55,27 @@ public static boolean isNullOrWhitespace(final String str) {
return true;
}

/**
* <p>Repeat a String {@code repeat} times to form a new String.</p>
*
* <pre>
* StringUtils.repeat(null, 2) = null
* StringUtils.repeat("", 0) = ""
* StringUtils.repeat("", 2) = ""
* StringUtils.repeat("a", 3) = "aaa"
* StringUtils.repeat("ab", 2) = "abab"
* StringUtils.repeat("a", -2) = ""
* </pre>
*
* @param str the String to repeat, may be null
* @param repeat number of times to repeat str, negative treated as zero
* @return a new String consisting of the original String repeated,
* {@code null} if null String input
*/
public static String repeat(String str, int repeat) {
return org.apache.commons.lang3.StringUtils.repeat(str, repeat);
}

/**
* Replaces a variable contained in a string with a value. A variable is defined as ${variable}.
* This pattern is replaced by the given value.
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/io/proximax/core/crypto/PrivateKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.proximax.core.crypto;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.math.BigInteger;
Expand Down Expand Up @@ -134,9 +135,9 @@ public void hashCodesAreEqualForEquivalentObjects() {

@Test
public void toStringReturnsHexRepresentation() {
// Assert:
MatcherAssert.assertThat(PrivateKey.fromHexString("2275").toString(), IsEqual.equalTo("2275"));
MatcherAssert.assertThat(PrivateKey.fromDecimalString("2275").toString(), IsEqual.equalTo("08e3"));
// private key is 32 bytes => 64 hexadecimal characters
assertEquals("0000000000000000000000000000000000000000000000000000000000002275", PrivateKey.fromHexString("2275").toString());
assertEquals("00000000000000000000000000000000000000000000000000000000000008e3", PrivateKey.fromDecimalString("2275").toString());
}

//endregion
Expand Down
9 changes: 8 additions & 1 deletion src/test/java/io/proximax/core/utils/HexEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.proximax.core.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -84,5 +86,10 @@ public void getStringCanConvertBytesWithLeadingZerosToHexString() {
"00000d465457");
}

//endregion
@Test
void getStringOfGivenLength() {
assertEquals("01", HexEncoder.getString(new byte[] {1}, 1));
assertEquals("0001", HexEncoder.getString(new byte[] {1}, 2));
assertEquals("010101", HexEncoder.getString(new byte[] {1, 1, 1}, 2));
}
}
12 changes: 12 additions & 0 deletions src/test/java/io/proximax/core/utils/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.proximax.core.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -85,4 +87,14 @@ public void replaceVariableCanReplaceMultipleOccurrencesOfVariable() {
MatcherAssert.assertThat(StringUtils.replaceVariable("Buffalo ${} Buffalo ${} ${} ${} Buffalo ${}", "", "buffalo"),
IsEqual.equalTo("Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo"));
}

@Test
void repeat() {
assertEquals("", StringUtils.repeat("hello", 0));
assertEquals("", StringUtils.repeat("hello", -2));
assertEquals("", StringUtils.repeat("", 7));
assertEquals("aaa", StringUtils.repeat("a", 3));
assertEquals("abab", StringUtils.repeat("ab", 2));
assertEquals(null, StringUtils.repeat(null, 2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void generateNewAccountTest(){
Account account = Account.generateNewAccount(NetworkType.MIJIN_TEST);
assertNotEquals(account.getPrivateKey(),null);
assertNotEquals(account.getPublicKey(), null);
assertEquals(64, account.getPrivateKey().toString().length());
assertEquals(64, account.getPrivateKey().length());
}

@Test
Expand Down

0 comments on commit 3233fb6

Please sign in to comment.