-
Notifications
You must be signed in to change notification settings - Fork 4
BITMAG-1133: Reference client understands salt input as a Hex String. This is NOT clear from the docs #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
149b057
53494e3
a5488d3
4c017df
82524cf
e28b561
f02111c
568d6ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,8 @@ | |
| */ | ||
| package org.bitrepository.common.utils; | ||
|
|
||
| import org.apache.commons.codec.DecoderException; | ||
| import org.apache.commons.codec.binary.Hex; | ||
| import org.bitrepository.common.ArgumentValidator; | ||
|
|
||
| /** | ||
|
|
@@ -40,35 +42,23 @@ public static String decodeBase16(byte[] data) { | |
| if (data == null) { | ||
| return null; | ||
| } | ||
|
|
||
| StringBuilder sb = new StringBuilder(data.length * 2); | ||
| for (byte datum : data) { | ||
| int v = datum & 0xff; | ||
| if (v < 16) { | ||
| sb.append('0'); | ||
| } | ||
| sb.append(Integer.toHexString(v)); | ||
| } | ||
| return sb.toString(); | ||
| // TODO Java 17 has HexFormat.of().formatHex(bytes) - consider using instead | ||
| return Hex.encodeHexString(data); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly to below one may consider adding a comment along the lines of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually did write this, but somehow I deleted it again. Will add. :) |
||
| } | ||
|
|
||
| /** | ||
| * Encoding a hex string to base16. | ||
| * Encodes a hex string to a set of base16 bytes. | ||
| * | ||
| * @param hexString The string to encode to base16. | ||
| * @return The string encoded to base16. | ||
| */ | ||
| public static byte[] encodeBase16(String hexString) { | ||
| ArgumentValidator.checkNotNullOrEmpty(hexString, "String hexString"); | ||
| ArgumentValidator.checkTrue((hexString.length() % 2) == 0, "String hexString, '" + hexString | ||
| + "', must be an even number of characters."); | ||
|
|
||
| int len = hexString.length(); | ||
| byte[] data = new byte[len / 2]; | ||
| for (int i = 0; i < len; i += 2) { | ||
| data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) | ||
| + Character.digit(hexString.charAt(i + 1), 16)); | ||
| // TODO Java 17 has HexFormat.of().parseHex(s) - consider using instead | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this comment. |
||
| try { | ||
| return Hex.decodeHex(hexString); | ||
| } catch (DecoderException e) { | ||
| throw new IllegalArgumentException("Bad hex-string '" + hexString + "': " + e.getMessage()); | ||
| } | ||
| return data; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicking now we're at it: remove the second comma, the one between "server" and "when".