|
| 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 | +} |
0 commit comments