Skip to content

Commit

Permalink
Fixed hex to base64
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardo-doyensec committed Apr 19, 2024
1 parent c8f6a44 commit 76b2fa2
Showing 1 changed file with 3 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import java.util.List;
import java.util.Base64;
import java.util.Optional;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
Expand All @@ -63,10 +64,6 @@ public final class RStudioCredentialTester extends CredentialTester {
private static final String RSTUDIO_UNSUPPORTED_BROWSER_P =
"Your web browser is not supported by RStudio.";

private static final String B64MAP =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static final char B64PAD = '=';

@Inject
RStudioCredentialTester(HttpClient httpClient) {
this.httpClient = checkNotNull(httpClient).modify().setFollowRedirects(false).build();
Expand Down Expand Up @@ -180,6 +177,7 @@ private boolean isRStudioAccessible(NetworkService networkService, TestCredentia
if (response.headers().get("Set-Cookie").isPresent()) {
for (String s : response.headers().getAll("Set-Cookie")) {
if (s.contains("user-id=" + credential.username())) {
logger.atInfo().log("Found valid credentials");
return true;
}
}
Expand All @@ -201,24 +199,7 @@ private boolean isRStudioAccessible(NetworkService networkService, TestCredentia

// This function base64 encodes provided cipertext string in hex.
private String hexToBase64(String hex) {
StringBuilder ret = new StringBuilder();

for (int i = 0; i + 3 <= hex.length(); i += 3) {
int c = Integer.parseInt(hex.substring(i, i + 3), 16);
ret.append(B64MAP.charAt(c >> 6)).append(B64MAP.charAt(c & 63));
}

int remaining = hex.length() % 3;

if (remaining == 1) {
int c = Integer.parseInt(hex.substring(hex.length() - 1), 16);
ret.append(B64MAP.charAt(c << 2)).append(B64MAP);
} else if (remaining == 2) {
int c = Integer.parseInt(hex.substring(hex.length() - 2), 16);
ret.append(B64MAP.charAt(c >> 2)).append(B64MAP.charAt((c & 3) << 4)).append(B64PAD);
}
ret.append(B64PAD);
return ret.toString();
return Base64.getEncoder().encodeToString(new BigInteger(hex, 16).toByteArray());
}

private HttpResponse sendRequestWithCredentials(
Expand Down

0 comments on commit 76b2fa2

Please sign in to comment.