Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/main/java/core/net/Connector.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ public class Connector {
* Creates a new instance of Connector.
*/
private Connector() {
m_OAService = new ServiceBuilder(Helper.decryptString(CONSUMER_KEY))
.apiSecret(Helper.decryptString(CONSUMER_SECRET))
m_OAService = new ServiceBuilder(HOEncryption.decryptString(CONSUMER_KEY))
.apiSecret(HOEncryption.decryptString(CONSUMER_SECRET))
.build(HattrickAPI.instance());


m_OAAccessToken = createOAAccessToken();
}

private OAuth1AccessToken createOAAccessToken() {
return new OAuth1AccessToken(Helper.decryptString(UserParameter.instance().AccessToken),
Helper.decryptString(UserParameter.instance().TokenSecret));
return new OAuth1AccessToken(HOEncryption.decryptString(UserParameter.instance().AccessToken),
HOEncryption.decryptString(UserParameter.instance().TokenSecret));
}

/**
Expand Down Expand Up @@ -778,8 +778,8 @@ public InputStream postWebFileWithBodyParameters(String surl, Map<String, String
m_OAAccessToken = authDialog.getAccessToken();
if (m_OAAccessToken == null) {
m_OAAccessToken = new OAuth1AccessToken(
Helper.decryptString(UserParameter.instance().AccessToken),
Helper.decryptString(UserParameter.instance().TokenSecret));
HOEncryption.decryptString(UserParameter.instance().AccessToken),
HOEncryption.decryptString(UserParameter.instance().TokenSecret));
}
}
// Try again...
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/core/net/login/OAuthDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@
import core.model.TranslationFacility;
import core.model.UserParameter;
import core.util.BrowserLauncher;
import core.util.HOEncryption;
import core.util.HOLogger;
import core.util.Helper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.Serial;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class OAuthDialog extends JDialog {

Expand Down Expand Up @@ -52,7 +49,7 @@ public OAuthDialog(HOMainFrame mainFrame, OAuth10aService service, String scope)
// Hard code set_matchorder due to authorization woes.
// scope manage_youthplayers is needed to use action type unlockskills
scopes = "&scope=" + "set_matchorder,manage_youthplayers";

obtainUserURL();
initComponents();
addListeners();
Expand All @@ -74,8 +71,8 @@ private void obtainUserURL() {
private void doAuthorize() {
try {
m_AccessToken = m_service.getAccessToken(m_RequestToken, m_jtfAuthString.getText().trim());
UserParameter.instance().AccessToken = Helper.cryptString(m_AccessToken.getToken());
UserParameter.instance().TokenSecret = Helper.cryptString(m_AccessToken.getTokenSecret());
UserParameter.instance().AccessToken = HOEncryption.cryptString(m_AccessToken.getToken());
UserParameter.instance().TokenSecret = HOEncryption.cryptString(m_AccessToken.getTokenSecret());

} catch (Exception e) {
HOLogger.instance().error(getClass(),
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/core/util/HOEncryption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package core.util;

public final class HOEncryption {

private HOEncryption() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

/**
* Decrypt string
* encrypted by method crypt
*/
public static String decryptString(String text) {
byte[] encoded;

if (text == null) {
return "";
}

encoded = text.getBytes();

for (int i = 0; (i < encoded.length); ++i) {
//check ob Zeichen gleich ~ = 126 ?
if (encoded[i] == 126) {
//Dann mit tilde ersetzen slash = 92
encoded[i] = 92;
}

encoded[i] += 7;

if ((encoded[i] % 2) == 0) {
++encoded[i];
} else {
--encoded[i];
}
}

return new String(encoded);
}

/**
* Encrypt a string consisting on numbers and characters only
*/
public static String cryptString(String text) {
byte[] encoded;

if (text == null) {
return "";
}

for (int j = 0; j < text.length(); j++) {
if (!Character.isLetterOrDigit(text.charAt(j))) {
return null;
}
}

encoded = text.getBytes();

for (int i = 0; (i < encoded.length); ++i) {
if ((encoded[i] % 2) == 0) {
++encoded[i];
} else {
--encoded[i];
}

encoded[i] -= 7;

//check for slash character = 92 ?
if (encoded[i] == 92) {
// replace it by ~ = 126
encoded[i] = 126;
}
}

return new String(encoded);
}
}
69 changes: 0 additions & 69 deletions src/main/java/core/util/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,75 +265,6 @@ public static NumberFormat getNumberFormat(int nbDecimals) {
return numFormat;
}

/**
* Decrypt string
* encrypted by method crypt
*/
public static String decryptString(String text) {
byte[] encoded;

if (text == null) {
return "";
}

encoded = text.getBytes();

for (int i = 0; (i < encoded.length); ++i) {
//check ob Zeichen gleich ~ = 126 ?
if (encoded[i] == 126) {
//Dann mit tilde ersetzen slash = 92
encoded[i] = 92;
}

encoded[i] += 7;

if ((encoded[i] % 2) == 0) {
++encoded[i];
} else {
--encoded[i];
}
}

return new String(encoded);
}

/**
* Encrypt a string consisting on numbers and characters only
*/
public static String cryptString(String text) {
byte[] encoded;

if (text == null) {
return "";
}

for (int j = 0; j < text.length(); j++) {
if (!Character.isLetterOrDigit(text.charAt(j))) {
return null;
}
}

encoded = text.getBytes();

for (int i = 0; (i < encoded.length); ++i) {
if ((encoded[i] % 2) == 0) {
++encoded[i];
} else {
--encoded[i];
}

encoded[i] -= 7;

//check for slash character = 92 ?
if (encoded[i] == 92) {
// replace it by ~ = 126
encoded[i] = 126;
}
}

return new String(encoded);
}

/**
* Copy vector to array
* @param src vector
Expand Down
99 changes: 99 additions & 0 deletions src/test/java/core/util/HOEncryptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package core.util;

import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.of;

class HOEncryptionTest {

private static Stream<Arguments> data() {
return Stream.of(
of("", ""),
of("01234567890", "*),+.-0/21*"),
of("ABCDEFGHIJLKMNOPQRSTUVWXQZ", "9<;>=@?BADFCEHGJILKNMPORIT"),
of("abcdefghijlkmnopqrstuvwxqz", "Y~[^]`_badfcehgjilknmporit"),
of("HO", "BG"),
of("HattrickOrganizer", "BYnnla[cGl_Yhat]l")
);
}

private static Stream<Arguments> notEncryptableCharacters() {
return Stream.of(
of(","),
of("."),
of("-"),
of(";"),
of(":"),
of("_"),
of("!"),
of("\""),
of("§"),
of("$"),
of("%"),
of("&"),
of("/"),
of("("),
of(")"),
of("="),
of("?"),
of("`"),
of("²"),
of("³"),
of("{"),
of("["),
of("]"),
of("}"),
of("\\"),
of("¸"),
of("+"),
of("*"),
of("~"),
of("#"),
of("'"),
of("|"),
of("<"),
of(">"),
of("^"),
of("°"),
of("€"),
of("@"),
of(" ")
);
}

@ParameterizedTest
@MethodSource("data")
void cryptString(String cleartext, String ciphertext) {
assertThat(HOEncryption.cryptString(cleartext)).isEqualTo(ciphertext);
}

@Test
void cryptString_with_null_result_is_empty() {
assertThat(HOEncryption.cryptString(null)).isEqualTo(StringUtils.EMPTY);
}

@ParameterizedTest
@MethodSource("data")
void decryptString(String cleartext, String ciphertext) {
assertThat(HOEncryption.decryptString(ciphertext)).isEqualTo(cleartext);
}

@Test
void decryptString_with_null_result_is_empty() {
assertThat(HOEncryption.decryptString(null)).isEqualTo(StringUtils.EMPTY);
}

@ParameterizedTest
@MethodSource("notEncryptableCharacters")
void cryptString_not_allowed_characters_result_is_null(String notAllowedCharacter) {
assertThat(StringUtils.length(notAllowedCharacter)).isEqualTo(1);
assertThat(HOEncryption.cryptString(notAllowedCharacter)).isNull();
}
}