Moves more functionality to the encryption helper

This commit is contained in:
Kristian Knarvik 2021-08-29 19:17:41 +02:00
parent 3364337b14
commit 07ff93e7bf

View File

@ -1,5 +1,15 @@
package net.knarcraft.bookswithoutborders.utility;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.EncryptionStyle;
import net.knarcraft.bookswithoutborders.GenenCrypt;
import net.knarcraft.bookswithoutborders.SubstitutionCipher;
import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.BookMeta;
import java.util.ArrayList;
import java.util.List;
public class EncryptionHelper {
/**
@ -15,4 +25,35 @@ public class EncryptionHelper {
return integerKey.toString();
}
/**
* Encrypts the pages of a book
* @param book <p>The book to encrypt</p>
* @param style <p>The encryption style to use</p>
* @param integerKey <p>The encryption key to use</p>
* @param player <p>The player trying to encrypt a book</p>
* @return <p>The pages of the book in encrypted form</p>
*/
public static List<String> encryptBookPages(BookMeta book, EncryptionStyle style, String integerKey, Player player) {
List<String> encryptedPages = new ArrayList<>();
//Scramble the book's contents
if (style == EncryptionStyle.DNA) {
//Encrypt the pages using gene-based encryption
GenenCrypt gc = new GenenCrypt(integerKey);
for (int x = 0; x < book.getPages().size(); x++) {
encryptedPages.add(gc.encrypt(book.getPage(x + 1)));
}
return encryptedPages;
} else if (style == EncryptionStyle.SUBSTITUTION) {
//Encrypt the pages using a substitution cipher
SubstitutionCipher sc = new SubstitutionCipher();
for (int x = 0; x < book.getPages().size(); x++) {
encryptedPages.add(sc.encrypt(book.getPage(x + 1), integerKey));
}
return encryptedPages;
} else {
BooksWithoutBorders.sendErrorMessage(player, "Invalid encryption style encountered!");
return null;
}
}
}