Skip to content

Commit

Permalink
Make the identifier more unique
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim203 committed Jan 24, 2021
1 parent ce2734d commit ad7ffab
Showing 1 changed file with 23 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,24 @@
* Responsible for both encrypting and decrypting data
*/
public interface FloodgateCipher {
byte[] IDENTIFIER = "Floodgate".getBytes(StandardCharsets.UTF_8);
// use invalid username characters at the beginning and the end of the identifier,
// to make sure that it doesn't get messed up with usernames
byte[] IDENTIFIER = "^Floodgate^".getBytes(StandardCharsets.UTF_8);
int HEADER_LENGTH = IDENTIFIER.length;

static boolean hasHeader(String data) {
if (data.length() < IDENTIFIER.length) {
return false;
}

for (int i = 0; i < IDENTIFIER.length; i++) {
if (IDENTIFIER[i] != data.charAt(i)) {
return false;
}
}
return true;
}

/**
* Initializes the instance by giving it the key it needs to encrypt or decrypt data
*
Expand All @@ -57,8 +72,7 @@ public interface FloodgateCipher {
byte[] encrypt(byte[] data) throws Exception;

/**
* Encrypts data from a String.<br>
* This method internally calls {@link #encrypt(byte[])}
* Encrypts data from a String.<br> This method internally calls {@link #encrypt(byte[])}
*
* @param data the data to encrypt
* @return the encrypted data
Expand All @@ -78,9 +92,8 @@ default byte[] encryptFromString(String data) throws Exception {
byte[] decrypt(byte[] data) throws Exception;

/**
* Decrypts a byte[] and turn it into a String.<br>
* This method internally calls {@link #decrypt(byte[])}
* and converts the returned byte[] into a String.
* Decrypts a byte[] and turn it into a String.<br> This method internally calls {@link
* #decrypt(byte[])} and converts the returned byte[] into a String.
*
* @param data the data to encrypt
* @return the decrypted data in a UTF-8 String
Expand All @@ -95,9 +108,8 @@ default String decryptToString(byte[] data) throws Exception {
}

/**
* Decrypts a String.<br>
* This method internally calls {@link #decrypt(byte[])}
* by converting the UTF-8 String into a byte[]
* Decrypts a String.<br> This method internally calls {@link #decrypt(byte[])} by converting
* the UTF-8 String into a byte[]
*
* @param data the data to decrypt
* @return the decrypted data in a byte[]
Expand All @@ -108,8 +120,8 @@ default byte[] decryptFromString(String data) throws Exception {
}

/**
* Checks if the header is valid.
* This method will throw an InvalidFormatException when the header is invalid.
* Checks if the header is valid. This method will throw an InvalidFormatException when the
* header is invalid.
*
* @param data the data to check
* @throws InvalidFormatException when the header is invalid
Expand Down Expand Up @@ -142,19 +154,6 @@ default void checkHeader(byte[] data) throws InvalidFormatException {
}
}

static boolean hasHeader(String data) {
if (data.length() < IDENTIFIER.length) {
return false;
}

for (int i = 0; i < IDENTIFIER.length; i++) {
if (IDENTIFIER[i] != data.charAt(i)) {
return false;
}
}
return true;
}

@Data
@AllArgsConstructor
class HeaderResult {
Expand Down

0 comments on commit ad7ffab

Please sign in to comment.