-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from aiven/hash-encoding
Encodes hashes hexadecimally instead of Base64
- Loading branch information
Showing
5 changed files
with
892 additions
and
25 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
35 changes: 35 additions & 0 deletions
35
src/main/java/io/aiven/kafka/connect/transforms/utils/Hex.java
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,35 @@ | ||
/* | ||
* Copyright 2020 Aiven Oy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.aiven.kafka.connect.transforms.utils; | ||
|
||
public class Hex { | ||
private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray(); | ||
|
||
/** | ||
* Encodes a byte array as a hexadecimal string. | ||
* @implNote https://stackoverflow.com/a/9855338/1781549 | ||
*/ | ||
public static String encode(final byte[] bytes) { | ||
final char[] hexChars = new char[bytes.length * 2]; | ||
for (int j = 0; j < bytes.length; j++) { | ||
final int v = bytes[j] & 0xFF; | ||
hexChars[j * 2] = HEX_ARRAY[v >>> 4]; // hi nibble | ||
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; // lo nibble | ||
} | ||
return new String(hexChars); | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
src/test/java/io/aiven/kafka/connect/transforms/utils/HexTest.java
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,68 @@ | ||
/* | ||
* Copyright 2020 Aiven Oy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.aiven.kafka.connect.transforms.utils; | ||
|
||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class HexTest { | ||
@Test | ||
void testEncodeEmpty() { | ||
final byte[] bytes = new byte[0]; | ||
assertEquals("", Hex.encode(bytes)); | ||
} | ||
|
||
@Test | ||
void testEncodeSingleByte() { | ||
final byte[] bytes = new byte[1]; | ||
for (int i = 0; i < 256; i++) { | ||
final byte b = (byte) i; | ||
bytes[0] = b; | ||
assertEquals(String.format("%02x", b), Hex.encode(bytes)); | ||
} | ||
} | ||
|
||
@Test | ||
void testEncodeFromStrings() throws IOException, URISyntaxException { | ||
final URL resource = getClass().getClassLoader().getResource("blns.txt"); | ||
final List<String> strings = Files.readAllLines(Paths.get(resource.toURI())); | ||
for (final String s : strings) { | ||
// Use the string as a byte array and hex-encode it. | ||
final byte[] bytes = s.getBytes(Charset.defaultCharset()); | ||
final String encoded = Hex.encode(bytes); | ||
assertEquals(bytes.length * 2, encoded.length()); | ||
|
||
// Decode the string back and compare to the original. | ||
final char[] encodedChars = encoded.toCharArray(); | ||
final byte[] decodedBytes = new byte[bytes.length]; | ||
for (int i = 0; i < encoded.length(); i += 2) { | ||
final String s1 = new String(encodedChars, i, 2); | ||
decodedBytes[i / 2] = (byte) Integer.parseInt(s1, 16); | ||
} | ||
assertEquals(new String(decodedBytes, Charset.defaultCharset()), s); | ||
} | ||
} | ||
} |
Oops, something went wrong.