RandomLayer.java

  1. package org.jastacry.layer;

  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.util.Objects;

  6. import org.jastacry.JastacryException;

  7. /**
  8.  * Mask every byte with some random data. The random stream is initialized by the init seed. Must be used the same on both sides
  9.  * (encryption and decryption).
  10.  *
  11.  * <p>SPDX-License-Identifier: MIT
  12.  *
  13.  * @author Kai Kretschmann
  14.  */
  15. public class RandomLayer extends AbstractBasicLayer
  16. {
  17.     /**
  18.      * static name of the layer.
  19.      */
  20.     static final String LAYERNAME = "Random Layer";

  21.     /**
  22.      * Random number generator.
  23.      */
  24.     private final java.util.Random rand = new java.util.Random();

  25.     /**
  26.      * Random number seed.
  27.      */
  28.     private long seed;

  29.     /**
  30.      * Constructor of RandomLayer.
  31.      */
  32.     public RandomLayer()
  33.     {
  34.         super(RandomLayer.class, LAYERNAME);
  35.     }

  36.     /**
  37.      * Getter method.
  38.      * @return long seed value
  39.      */
  40.     public final long getSeed()
  41.     {
  42.         return seed;
  43.     }

  44.     /**
  45.      * Setter method.
  46.      * @param newSeed long value
  47.      */
  48.     public final void setSeed(final long newSeed)
  49.     {
  50.         this.seed = newSeed;
  51.     }

  52.     /**
  53.      * Init function.
  54.      *
  55.      * @param data to initialise the random seed value.
  56.      */
  57.     @Override
  58.     public final void init(final String data)
  59.     {
  60.         setSeed(Long.parseLong(data));
  61.         rand.setSeed(seed);
  62.     }

  63.     /**
  64.      * Local encode Stream function which does the real thing for Random Layer.
  65.      *
  66.      * @param inputStream incoming data
  67.      * @param outputStream outgoing data
  68.      * @throws JastacryException thrown on error
  69.      */
  70.     public void encodeAndDecode(final InputStream inputStream, final OutputStream outputStream) throws JastacryException
  71.     {
  72.         try
  73.         {
  74.             int iChar;
  75.             byte bChar;
  76.             final byte[] bRand = new byte[1];
  77.             while ((iChar = inputStream.read()) != -1)
  78.             {
  79.                 bChar = (byte) iChar;
  80.                 this.rand.nextBytes(bRand);
  81.                 bChar = (byte) (bChar ^ bRand[0]);
  82.                 outputStream.write(bChar);
  83.                 progress(1);
  84.             }
  85.             logger.info("close pipe");
  86.             outputStream.close();
  87.         }
  88.         catch (IOException e)
  89.         {
  90.             throw (JastacryException) new JastacryException("encodeAndDecode failed").initCause(e);
  91.         }
  92.     }

  93.     /**
  94.      * Override equals method from object class.
  95.      * @param o object to compare with
  96.      * @return true or false
  97.      */
  98.     @Override
  99.     public boolean equals(final Object o)
  100.     {
  101.         return o == this || o instanceof RandomLayer && ((RandomLayer) o).getSeed() == this.getSeed();
  102.      }

  103.     /**
  104.      * Override equals method from object class.
  105.      * @return hash of properties
  106.      */
  107.     @Override
  108.     public int hashCode()
  109.     {
  110.         return Objects.hash(seed);
  111.     }
  112. }