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   * Very simple algorithm just to infuse some more complex data rotation.
12   *
13   * <p>SPDX-License-Identifier: MIT
14   *
15   * @author Kai Kretschmann
16   */
17  public class XorLayer extends AbstractBasicLayer
18  {
19      /**
20       * static name of the layer.
21       */
22      public static final String LAYERNAME = "Xor Layer";
23      /**
24       * private byte mask to xor with.
25       */
26      private byte bitMask;
27  
28      /**
29       * Constructor of XorLayer.
30       */
31      public XorLayer()
32      {
33          super(XorLayer.class, LAYERNAME);
34      }
35  
36      /**
37       * init function.
38       *
39       * @param data to initialize the xor value.
40       */
41      @Override
42      public final void init(final String data)
43      {
44          this.bitMask = (byte) Integer.parseInt(data);
45      }
46  
47      /**
48       * Local encode Stream function which does the real thing for Xor Layer.
49       *
50       * @param inputStream incoming data
51       * @param outputStream outgoing data
52       * @throws JastacryException thrown on error
53       */
54      public final void encodeAndDecode(final InputStream inputStream, final OutputStream outputStream) throws JastacryException
55      {
56          try
57          {
58              int iChar;
59              byte bChar;
60              while ((iChar = inputStream.read()) != -1)
61              { // NOPMD by kai on 21.11.17 17:18
62                  bChar = (byte) iChar; // NOPMD by kai on 21.11.17 17:18
63                  bChar = (byte) (bChar ^ this.bitMask);
64                  outputStream.write(bChar);
65              }
66              logger.info("close pipe");
67              outputStream.close();
68          }
69          catch (IOException e)
70          {
71              throw (JastacryExceptionl#JastacryException">JastacryException) new JastacryException("encodeAndDecode failed").initCause(e);
72          }
73      }
74  
75      /**
76       * Override equals method from object class.
77       * @param o object to compare with
78       * @return true or false
79       */
80      @Override
81      public boolean equals(final Object o)
82      {
83          return o == this || o instanceof XorLayer/../org/jastacry/layer/XorLayer.html#XorLayer">XorLayer && ((XorLayer) o).bitMask == this.bitMask;
84      }
85  
86      /**
87       * Override equals method from object class.
88       * @return hash of properties
89       */
90      @Override
91      public int hashCode()
92      {
93          return Objects.hash(bitMask);
94      }
95  }