Skip to content
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

Add Base16 (Hex) and Base64 encoding and decoding capabilities #25

Closed
Glusk opened this issue Aug 20, 2020 · 3 comments · Fixed by #28
Closed

Add Base16 (Hex) and Base64 encoding and decoding capabilities #25

Glusk opened this issue Aug 20, 2020 · 3 comments · Fixed by #28

Comments

@Glusk
Copy link
Owner

Glusk commented Aug 20, 2020

At the moment, we're wired to an external dependency jakarta.xml.bind:jakarta.xml.bind-api just so that we can use DatatypeConverter. This dependency could be removed.

Java 8 introduced Base64 class and the hex encoding/decoding looks easy enough to implement.

The library currently only parses Base64 and Base16 strings. We should add default methods to Bytes that would encode byte sequences to Base64 and Base16 strings.

@Glusk
Copy link
Owner Author

Glusk commented Aug 21, 2020

Base64 encoding/decoding should be Basic.

If the user wants to read a Base64 MIME encoded file, he or she can do it this way in Java 9 or later:

byte[] decodedBytes = Base64.getMimeDecoder().wrap(/*InputStream*/).readAllBytes();

decodedBytes can then be further used by Caesar.

The procedure is analogous for encoding.

@Glusk
Copy link
Owner Author

Glusk commented Aug 21, 2020

Hex string need not be even length. As a consequence, different length strings may result in the same byte array.

This library should be able to parse all valid hex strings.

The simplest way to encode a byte array to an even length string:

// byte[] array = ...
StringBuilder sb = new StringBuilder();
for (byte b : array) {
    sb.append(String.format("%02X", b));
}
String result = sb.toString();

What to do with empty strings ("")?

@Glusk
Copy link
Owner Author

Glusk commented Aug 22, 2020

Base64 support will be dropped. It is easy enough for the user of this library to convert a Base64 string to a byte array (and vice versa) using the Java SE API version 8 or later.

Glusk added a commit that referenced this issue Aug 22, 2020
Glusk added a commit that referenced this issue Aug 22, 2020
@Glusk Glusk mentioned this issue Aug 22, 2020
@Glusk Glusk closed this as completed in #28 Aug 22, 2020
This was referenced Aug 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant