View Javadoc
1   package org.jastacry.layer;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.io.OutputStream;
6   import java.util.Objects;
7   
8   import org.jastacry.JastacryException;
9   
10  /**
11   * Mask every byte with some random data. The random stream is initialized by the init seed. Must be used the same on both sides
12   * (encryption and decryption).
13   *
14   * <p>SPDX-License-Identifier: MIT
15   *
16   * @author Kai Kretschmann
17   */
18  public class RandomLayer extends AbstractBasicLayer
19  {
20      /**
21       * static name of the layer.
22       */
23      public static final String LAYERNAME = "Random Layer";
24  
25      /**
26       * Random number generator.
27       */
28      private final java.util.Random rand = new java.util.Random();
29  
30      /**
31       * Random number seed.
32       */
33      private long seed;
34  
35      /**
36       * Constructor of RandomLayer.
37       */
38      public RandomLayer()
39      {
40          super(RandomLayer.class, LAYERNAME);
41      }
42  
43      /**
44       * Getter method.
45       * @return long seed value
46       */
47      public final long getSeed()
48      {
49          return seed;
50      }
51  
52      /**
53       * Setter method.
54       * @param newSeed long value
55       */
56      public final void setSeed(final long newSeed)
57      {
58          this.seed = newSeed;
59      }
60  
61      /**
62       * Init function.
63       *
64       * @param data to initialise the random seed value.
65       */
66      @Override
67      public final void init(final String data)
68      {
69          setSeed(Long.parseLong(data));
70          rand.setSeed(seed);
71      }
72  
73      /**
74       * Local encode Stream function which does the real thing for Random Layer.
75       *
76       * @param inputStream incoming data
77       * @param outputStream outgoing data
78       * @throws JastacryException thrown on error
79       */
80      public void encodeAndDecode(final InputStream inputStream, final OutputStream outputStream) throws JastacryException
81      {
82          try
83          {
84              int iChar;
85              byte bChar;
86              final byte[] bRand = new byte[1];
87              while ((iChar = inputStream.read()) != -1)
88              {
89                  bChar = (byte) iChar;
90                  this.rand.nextBytes(bRand);
91                  bChar = (byte) (bChar ^ bRand[0]);
92                  outputStream.write(bChar);
93              }
94              logger.info("close pipe");
95              outputStream.close();
96          }
97          catch (IOException e)
98          {
99              throw (JastacryExceptionl#JastacryException">JastacryException) new JastacryException("encodeAndDecode failed").initCause(e);
100         }
101     }
102 
103     /**
104      * Override equals method from object class.
105      * @param o object to compare with
106      * @return true or false
107      */
108     @Override
109     public boolean equals(final Object o)
110     {
111         return o == this || o instanceof RandomLayer/org/jastacry/layer/RandomLayer.html#RandomLayer">RandomLayer && ((RandomLayer) o).getSeed() == this.getSeed();
112      }
113 
114     /**
115      * Override equals method from object class.
116      * @return hash of properties
117      */
118     @Override
119     public int hashCode()
120     {
121         return Objects.hash(seed);
122     }
123 }