View Javadoc
1   package org.jastacry.layer;
2   
3   import java.nio.charset.StandardCharsets;
4   import java.security.MessageDigest;
5   import java.security.NoSuchAlgorithmException;
6   import java.util.Arrays;
7   
8   import javax.crypto.spec.SecretKeySpec;
9   
10  import org.jastacry.JastacryException;
11  
12  /**
13   * AES Layer class.
14   *
15   * <p>SPDX-License-Identifier: MIT
16   *
17   * @author Kai Kretschmann
18   */
19  public class AesCtrLayer extends AbstractCipherLayer
20  {
21      /**
22       * static name of the layer.
23       */
24      public static final String LAYERNAME = "AES CTR Layer";
25  
26      /**
27       * Used algorithm name.
28       */
29      private static final String MYALG = "AES/CTR/NoPadding";
30  
31      /**
32       * Used algorithm name for the key.
33       */
34      private static final String MYKEYALG = "AES";
35  
36      /**
37       * Used algorithm name for the key hash.
38       */
39      private static final String MYHASHALG = "SHA-256";
40  
41      /**
42       * IV length.
43       */
44      private static final int IVLEN = 16;
45  
46      /**
47       * Salt length.
48       */
49      private static final int SALTLEN = 0;
50  
51      /**
52       * Iteration count.
53       */
54      private static final int COUNT = 0;
55  
56      /**
57       * Size of key. (16 bytes)
58       */
59      private static final int KEYSIZE = 128;
60  
61      /**
62       * Constructor of AesLayer.
63       */
64      public AesCtrLayer()
65      {
66          super(AesCtrLayer.class, LAYERNAME);
67      }
68  
69      /**
70       * Generate Keys from plain password.
71       *
72       * @throws JastacryException on error
73       */
74      @Override
75      protected final void setupPbe() throws JastacryException
76      {
77          MessageDigest sha;
78          try
79          {
80              sha = MessageDigest.getInstance(MYHASHALG);
81  
82              final byte[] keyPlaintext = new String(chPasswd).getBytes(StandardCharsets.UTF_8);
83              final byte[] keyDigest = sha.digest(keyPlaintext);
84              final byte[] keyDigestTrimmed = Arrays.copyOf(keyDigest, KEYSIZE / BITSPERBYTE);
85  
86              this.pbeSecretKeySpec = new SecretKeySpec(keyDigestTrimmed, MYKEYALG);
87          }
88          catch (NoSuchAlgorithmException e)
89          {
90              throw (JastacryExceptionl#JastacryException">JastacryException) new JastacryException("Setup PBE failed").initCause(e);
91          }
92  
93      }
94  
95      /**
96       * init function. Overrides base init but uses it for setting base values.
97       *
98       * @param data to initialize the crypt value.
99       */
100     @Override
101     public final void init(final String data)
102     {
103         super.init();
104 
105         this.chPasswd = data.toCharArray();
106     }
107 
108     @Override
109     protected final String getMyAlg()
110     {
111         return MYALG;
112     }
113 
114     @Override
115     protected final String getMyKeyAlg()
116     {
117         return MYKEYALG;
118     }
119 
120     @Override
121     protected int getMySaltLen()
122     {
123         return SALTLEN;
124     }
125 
126     @Override
127     protected int getMyIvLen()
128     {
129         return IVLEN;
130     }
131 
132     @Override
133     protected int getMyCount()
134     {
135         return COUNT;
136     }
137 
138     @Override
139     protected int getMyKeysize()
140     {
141         return KEYSIZE;
142     }
143 
144     /**
145      * Override equals method from object class.
146      * @param o object to compare with
147      * @return true or false
148      */
149     @Override
150     public boolean equals(final Object o)
151     {
152         if (o == this)
153         {
154             return true;
155         }
156         if (!(o instanceof AesCtrLayer))
157         {
158             return false;
159         }
160 
161         final AesCtrLayer./../org/jastacry/layer/AesCtrLayer.html#AesCtrLayer">AesCtrLayer layer = (AesCtrLayer) o;
162         return Arrays.equals(layer.chPasswd, this.chPasswd);
163     }
164 
165     /**
166      * Override equals method from object class.
167      * @return hash of properties
168      */
169     @Override
170     public int hashCode()
171     {
172         return Arrays.hashCode(chPasswd);
173     }
174 }