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 AesEcbLayer extends AbstractCipherLayer
20  {
21      /**
22       * static name of the layer.
23       */
24      public static final String LAYERNAME = "AES ECB Layer";
25  
26      /**
27       * Used algorithm name.
28       */
29      private static final String MYALG = "AES/ECB/PKCS5Padding";
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 = 0;
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.
58       */
59      private static final int KEYSIZE = 128;
60  
61      /**
62       * Constructor of AesLayer.
63       */
64      public AesEcbLayer()
65      {
66          super(AesEcbLayer.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              final byte[] keyPlaintext = new String(chPasswd).getBytes(StandardCharsets.UTF_8);
82              final byte[] keyDigest = sha.digest(keyPlaintext);
83              final byte[] keyDigestTrimmed = Arrays.copyOf(keyDigest, KEYSIZE / BITSPERBYTE);
84  
85              pbeSecretKeySpec = new SecretKeySpec(keyDigestTrimmed, MYKEYALG);
86          }
87          catch (NoSuchAlgorithmException e)
88          {
89              throw (JastacryExceptionl#JastacryException">JastacryException) new JastacryException("Setup PBE failed").initCause(e);
90          }
91  
92      }
93  
94      /**
95       * init function. Overrides base init but uses it for setting base values.
96       *
97       * @param data to initialize the crypt value.
98       */
99      @Override
100     public final void init(final String data)
101     {
102         super.init();
103 
104         this.chPasswd = data.toCharArray();
105     }
106 
107     @Override
108     protected final String getMyAlg()
109     {
110         return MYALG;
111     }
112 
113     @Override
114     protected final String getMyKeyAlg()
115     {
116         return MYKEYALG;
117     }
118 
119     @Override
120     protected int getMySaltLen()
121     {
122         return SALTLEN;
123     }
124 
125     @Override
126     protected int getMyIvLen()
127     {
128         return IVLEN;
129     }
130 
131     @Override
132     protected int getMyCount()
133     {
134         return COUNT;
135     }
136 
137     @Override
138     protected int getMyKeysize()
139     {
140         return KEYSIZE;
141     }
142 
143     /**
144      * Override equals method from object class.
145      * @param o object to compare with
146      * @return true or false
147      */
148     @Override
149     public boolean equals(final Object o)
150     {
151         if (o == this)
152         {
153             return true;
154         }
155         if (!(o instanceof AesEcbLayer))
156         {
157             return false;
158         }
159 
160         final AesEcbLayer./../org/jastacry/layer/AesEcbLayer.html#AesEcbLayer">AesEcbLayer layer = (AesEcbLayer) o;
161         return Arrays.equals(layer.chPasswd, this.chPasswd);
162     }
163 
164     /**
165      * Override equals method from object class.
166      * @return hash of properties
167      */
168     @Override
169     public int hashCode()
170     {
171         return Arrays.hashCode(chPasswd);
172     }
173 }