Skip to content

Commit 9395612

Browse files
author
BastouP411
committed
Added cryptographic accelerator
1 parent b53428b commit 9395612

File tree

15 files changed

+320
-5
lines changed

15 files changed

+320
-5
lines changed

lib/sqlite-jdbc-CUSTOM.jar

-6.43 MB
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package fr.bastoup.bperipherals.blocks;
2+
3+
import fr.bastoup.bperipherals.init.ModTileTypes;
4+
import net.minecraft.block.BlockState;
5+
import net.minecraft.block.material.Material;
6+
import net.minecraft.tileentity.TileEntity;
7+
import net.minecraft.util.Direction;
8+
import net.minecraft.world.IBlockReader;
9+
10+
import javax.annotation.Nullable;
11+
12+
public class BlockCryptographicAccelerator extends BlockOrientable{
13+
14+
public BlockCryptographicAccelerator() {
15+
super(Properties.create(Material.ROCK).hardnessAndResistance(2.0F), "cryptographic_accelerator");
16+
17+
setDefaultState(this.getDefaultState().with(FACING, Direction.NORTH));
18+
}
19+
20+
@Override
21+
public boolean hasTileEntity(BlockState state) {
22+
return true;
23+
}
24+
25+
@Nullable
26+
@Override
27+
public TileEntity createTileEntity(BlockState state, IBlockReader world) {
28+
return ModTileTypes.CRYPTOGRAPHIC_ACCELERATOR.create();
29+
}
30+
31+
}

src/main/java/fr/bastoup/bperipherals/gui/GUIDatabase.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.minecraft.entity.player.PlayerInventory;
99
import net.minecraft.util.ResourceLocation;
1010
import net.minecraft.util.text.ITextComponent;
11+
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
1112

1213
import javax.annotation.Nonnull;
1314

@@ -35,5 +36,6 @@ protected void drawGuiContainerBackgroundLayer(MatrixStack matrixStack, float pa
3536
int i = (this.width - this.xSize) / 2;
3637
int j = (this.height - this.ySize) / 2;
3738
this.blit(matrixStack, i, j, 0, 0, this.xSize, this.ySize);
39+
3840
}
3941
}

src/main/java/fr/bastoup/bperipherals/init/ModBlocks.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.bastoup.bperipherals.init;
22

3+
import fr.bastoup.bperipherals.blocks.BlockCryptographicAccelerator;
34
import fr.bastoup.bperipherals.blocks.BlockDatabase;
45
import fr.bastoup.bperipherals.blocks.BlockFEMeter;
56
import net.minecraft.block.Block;
@@ -12,4 +13,5 @@ public class ModBlocks {
1213

1314
public static final Block FE_METER = new BlockFEMeter();
1415
public static final Block DATABASE = new BlockDatabase();
16+
public static final Block CRYPTOGRAPHIC_ACCELERATOR = new BlockCryptographicAccelerator();
1517
}

src/main/java/fr/bastoup/bperipherals/init/ModTileTypes.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package fr.bastoup.bperipherals.init;
22

3+
import dan200.computercraft.api.lua.ILuaAPIFactory;
4+
import fr.bastoup.bperipherals.tileentities.TileCryptographicAccelerator;
35
import fr.bastoup.bperipherals.tileentities.TileDatabase;
46
import fr.bastoup.bperipherals.tileentities.TileFEMeter;
57
import fr.bastoup.bperipherals.util.BPeripheralsProperties;
@@ -10,6 +12,8 @@ public class ModTileTypes {
1012

1113
public static final TileEntityType<TileFEMeter> FE_METER = (TileEntityType<TileFEMeter>) TileEntityType.Builder.create(TileFEMeter::new, ModBlocks.FE_METER)
1214
.build(null).setRegistryName(new ResourceLocation(BPeripheralsProperties.MODID, "fe_meter"));
13-
public static final TileEntityType<TileFEMeter> DATABASE = (TileEntityType<TileFEMeter>) TileEntityType.Builder.create(TileDatabase::new, ModBlocks.DATABASE)
15+
public static final TileEntityType<TileDatabase> DATABASE = (TileEntityType<TileDatabase>) TileEntityType.Builder.create(TileDatabase::new, ModBlocks.DATABASE)
1416
.build(null).setRegistryName(new ResourceLocation(BPeripheralsProperties.MODID, "database"));
17+
public static final TileEntityType<TileCryptographicAccelerator> CRYPTOGRAPHIC_ACCELERATOR = (TileEntityType<TileCryptographicAccelerator>) TileEntityType.Builder.create(TileCryptographicAccelerator::new, ModBlocks.CRYPTOGRAPHIC_ACCELERATOR)
18+
.build(null).setRegistryName(new ResourceLocation(BPeripheralsProperties.MODID, "cryptographic_accelerator"));
1519
}
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package fr.bastoup.bperipherals.peripherals;
2+
3+
import dan200.computercraft.api.lua.IArguments;
4+
import dan200.computercraft.api.lua.LuaException;
5+
import dan200.computercraft.api.lua.LuaFunction;
6+
import dan200.computercraft.api.peripheral.IComputerAccess;
7+
import dan200.computercraft.api.peripheral.IPeripheral;
8+
import fr.bastoup.bperipherals.tileentities.TileCryptographicAccelerator;
9+
import fr.bastoup.bperipherals.util.Util;
10+
import org.squiddev.cobalt.LuaString;
11+
12+
import javax.annotation.Nonnull;
13+
import javax.annotation.Nullable;
14+
import javax.crypto.BadPaddingException;
15+
import javax.crypto.Cipher;
16+
import javax.crypto.IllegalBlockSizeException;
17+
import javax.crypto.NoSuchPaddingException;
18+
import javax.crypto.spec.IvParameterSpec;
19+
import javax.crypto.spec.SecretKeySpec;
20+
import java.nio.ByteBuffer;
21+
import java.security.*;
22+
import java.security.spec.InvalidKeySpecException;
23+
import java.security.spec.PKCS8EncodedKeySpec;
24+
import java.security.spec.X509EncodedKeySpec;
25+
import java.util.Base64;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
public class PeripheralCryptographicAccelerator implements IPeripheral {
30+
31+
public static final String TYPE = "cryptographic_accelerator";
32+
33+
private final TileCryptographicAccelerator tile;
34+
35+
public PeripheralCryptographicAccelerator(TileCryptographicAccelerator tile) {
36+
this.tile = tile;
37+
}
38+
39+
@Nonnull
40+
@Override
41+
public String getType() {
42+
return TYPE;
43+
}
44+
45+
@Override
46+
public void attach(@Nonnull IComputerAccess computer) {
47+
48+
}
49+
50+
@Override
51+
public void detach(@Nonnull IComputerAccess computer) {
52+
53+
}
54+
55+
@Nullable
56+
@Override
57+
public Object getTarget() {
58+
return tile;
59+
}
60+
61+
@Override
62+
public boolean equals(@Nullable IPeripheral other) {
63+
return other instanceof PeripheralCryptographicAccelerator && ((TileCryptographicAccelerator) other.getTarget()).getWorld().equals(tile.getWorld()) &&
64+
((TileCryptographicAccelerator) other.getTarget()).getPos().equals(tile.getPos());
65+
}
66+
67+
@LuaFunction
68+
public final byte[] randomBytes(int length) throws LuaException {
69+
if(0 >= length || length > 1024)
70+
throw new LuaException("Length must be between 1 and 1024");
71+
byte[] res = new byte[length];
72+
new SecureRandom().nextBytes(res);
73+
return res;
74+
}
75+
76+
@LuaFunction
77+
public final byte[] decodeBase64(String base64) throws LuaException {
78+
try {
79+
return Base64.getDecoder().decode(base64);
80+
} catch (IllegalArgumentException e) {
81+
throw new LuaException("This is not a valid base64 string.");
82+
}
83+
}
84+
85+
@LuaFunction
86+
public final String encodeBase64(ByteBuffer str) throws LuaException {
87+
byte[] strArray = Util.getByteBufferArray(str);
88+
return new String(Base64.getEncoder().encode(strArray));
89+
}
90+
91+
@LuaFunction
92+
public final byte[] encryptAES(ByteBuffer data, ByteBuffer key, ByteBuffer iv) throws LuaException {
93+
byte[] keyArray = Util.getByteBufferArray(key);
94+
byte[] ivArray = Util.getByteBufferArray(iv);
95+
byte[] dataArray = Util.getByteBufferArray(data);
96+
SecretKeySpec secretKeySpec = new SecretKeySpec(keyArray, "AES");
97+
IvParameterSpec ivSpec = new IvParameterSpec(ivArray);
98+
try {
99+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
100+
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
101+
return cipher.doFinal(dataArray);
102+
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
103+
throw new LuaException(e.getMessage());
104+
} catch (NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e) {
105+
e.printStackTrace();
106+
throw new LuaException("Internal error, check the logs for more info.");
107+
}
108+
}
109+
110+
@LuaFunction
111+
public final byte[] decryptAES(ByteBuffer data, ByteBuffer key, ByteBuffer iv) throws LuaException {
112+
byte[] keyArray = Util.getByteBufferArray(key);
113+
byte[] ivArray = Util.getByteBufferArray(iv);
114+
byte[] dataArray = Util.getByteBufferArray(data);
115+
SecretKeySpec secretKeySpec = new SecretKeySpec(keyArray, "AES");
116+
IvParameterSpec ivSpec = new IvParameterSpec(ivArray);
117+
try {
118+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
119+
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);
120+
return cipher.doFinal(dataArray);
121+
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
122+
throw new LuaException(e.getMessage());
123+
} catch (NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e) {
124+
e.printStackTrace();
125+
throw new LuaException("Internal error, check the logs for more info.");
126+
}
127+
}
128+
129+
@LuaFunction
130+
public final Map<String, byte[]> generateRSAKeys(int keySize) throws LuaException {
131+
if(0 >= keySize || keySize > 1024)
132+
throw new LuaException("Key size must be between 1 and 1024");
133+
try {
134+
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA");
135+
kpGen.initialize(keySize);
136+
KeyPair kp = kpGen.generateKeyPair();
137+
Map<String, byte[]> res = new HashMap<>();
138+
res.put("public", kp.getPublic().getEncoded());
139+
res.put("private", kp.getPrivate().getEncoded());
140+
return res;
141+
} catch (NoSuchAlgorithmException e) {
142+
e.printStackTrace();
143+
throw new LuaException("Internal error, check the logs for more info.");
144+
}
145+
146+
}
147+
148+
@LuaFunction
149+
public final byte[] encryptRSA(ByteBuffer data, ByteBuffer publicKey) throws LuaException {
150+
byte[] publicKeyArray = Util.getByteBufferArray(publicKey);
151+
byte[] dataArray = Util.getByteBufferArray(data);
152+
try {
153+
PublicKey key = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKeyArray));
154+
Cipher cipher = Cipher.getInstance("RSA");
155+
cipher.init(Cipher.ENCRYPT_MODE, key);
156+
return cipher.doFinal(dataArray);
157+
} catch (InvalidKeyException e) {
158+
throw new LuaException(e.getMessage());
159+
} catch (NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException | InvalidKeySpecException e) {
160+
e.printStackTrace();
161+
throw new LuaException("Internal error, check the logs for more info.");
162+
}
163+
}
164+
165+
@LuaFunction
166+
public final byte[] decryptRSA(ByteBuffer data, ByteBuffer privateKey) throws LuaException {
167+
byte[] privateKeyArray = Util.getByteBufferArray(privateKey);
168+
byte[] dataArray = Util.getByteBufferArray(data);
169+
try {
170+
PrivateKey key = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateKeyArray));
171+
Cipher cipher = Cipher.getInstance("RSA");
172+
cipher.init(Cipher.DECRYPT_MODE, key);
173+
return cipher.doFinal(dataArray);
174+
} catch (InvalidKeyException e) {
175+
throw new LuaException(e.getMessage());
176+
} catch (NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException | InvalidKeySpecException e) {
177+
e.printStackTrace();
178+
throw new LuaException("Internal error, check the logs for more info.");
179+
}
180+
}
181+
182+
@LuaFunction
183+
public final byte[] hashMD5(ByteBuffer data) throws LuaException {
184+
byte[] dataArray = Util.getByteBufferArray(data);
185+
try {
186+
MessageDigest md = MessageDigest.getInstance("MD5");
187+
return md.digest(dataArray);
188+
} catch (NoSuchAlgorithmException e) {
189+
e.printStackTrace();
190+
throw new LuaException("Internal error, check the logs for more info.");
191+
}
192+
}
193+
194+
@LuaFunction
195+
public final byte[] hashSHA512(ByteBuffer data) throws LuaException {
196+
byte[] dataArray = Util.getByteBufferArray(data);
197+
try {
198+
MessageDigest md = MessageDigest.getInstance("SHA-512");
199+
return md.digest(dataArray);
200+
} catch (NoSuchAlgorithmException e) {
201+
e.printStackTrace();
202+
throw new LuaException("Internal error, check the logs for more info.");
203+
}
204+
}
205+
206+
207+
}

src/main/java/fr/bastoup/bperipherals/registry/SharedRegistry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public static void onBlockRegister(RegistryEvent.Register<Block> event) {
3434
public static void onTileRegister(RegistryEvent.Register<TileEntityType<?>> event) {
3535
event.getRegistry().registerAll(
3636
ModTileTypes.FE_METER,
37-
ModTileTypes.DATABASE
37+
ModTileTypes.DATABASE,
38+
ModTileTypes.CRYPTOGRAPHIC_ACCELERATOR
3839
);
3940
}
4041

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package fr.bastoup.bperipherals.tileentities;
2+
3+
import dan200.computercraft.api.peripheral.IPeripheral;
4+
import fr.bastoup.bperipherals.init.ModTileTypes;
5+
import fr.bastoup.bperipherals.peripherals.PeripheralCryptographicAccelerator;
6+
import net.minecraft.inventory.container.INamedContainerProvider;
7+
import net.minecraft.tileentity.TileEntityType;
8+
9+
public class TileCryptographicAccelerator extends TileOrientable implements TilePeripheral {
10+
public TileCryptographicAccelerator() {
11+
super(ModTileTypes.CRYPTOGRAPHIC_ACCELERATOR);
12+
}
13+
14+
@Override
15+
public IPeripheral getPeripheral() {
16+
return new PeripheralCryptographicAccelerator(this);
17+
}
18+
}

src/main/java/fr/bastoup/bperipherals/util/Util.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@
44
import net.minecraft.util.math.BlockPos;
55
import net.minecraft.world.server.ServerWorld;
66
import net.minecraft.world.storage.DimensionSavedDataManager;
7+
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
78

89
import java.io.File;
910
import java.lang.reflect.Field;
11+
import java.nio.ByteBuffer;
12+
import java.util.ArrayList;
13+
import java.util.List;
1014

1115
public class Util {
16+
17+
private static Field folderField;
18+
19+
static {
20+
folderField = ObfuscationReflectionHelper.findField(DimensionSavedDataManager.class, "folder");
21+
folderField.setAccessible(true);
22+
}
1223

1324
public static Direction getOppositeFacing(Direction facing) {
1425
switch(facing) {
@@ -121,9 +132,12 @@ public static BlockPos getNextPos(BlockPos pos, Direction facing) {
121132

122133
public static File getWorldFolder(ServerWorld world) throws NoSuchFieldException, IllegalAccessException {
123134
DimensionSavedDataManager savedData = world.getChunkProvider().getSavedData();
124-
125-
Field folderField = DimensionSavedDataManager.class.getDeclaredField("folder");
126-
folderField.setAccessible(true);
127135
return ((File) folderField.get(savedData)).getParentFile();
128136
}
137+
138+
public static byte[] getByteBufferArray(ByteBuffer buf) {
139+
byte[] res = new byte[buf.remaining()];
140+
buf.get(res, 0, res.length);
141+
return res;
142+
}
129143
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"variants": {
3+
"facing=north": {
4+
"model": "bperipherals:block/cryptographic_accelerator"
5+
},
6+
"facing=south": {
7+
"model": "bperipherals:block/cryptographic_accelerator",
8+
"y": 180
9+
},
10+
"facing=west": {
11+
"model": "bperipherals:block/cryptographic_accelerator",
12+
"y": 270
13+
},
14+
"facing=east": {
15+
"model": "bperipherals:block/cryptographic_accelerator",
16+
"y": 90
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)