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   * MD5 DES Layer class.
14   *
15   * <p>SPDX-License-Identifier: MIT
16   *
17   * @author Kai Kretschmann
18   */
19  public class Md5DesLayer extends AbstractCipherLayer
20  {
21      /**
22       * static name of the layer.
23       */
24      public static final String LAYERNAME = "MD5DES Layer";
25  
26      /**
27       * Used algorithm name.
28       */
29      private static final String MYALG = "DESede/CBC/PKCS5Padding";
30  
31      /**
32       * Used algorithm name for the key.
33       */
34      private static final String MYKEYALG = "DESede";
35  
36      /**
37       * Used algorithm name for the hash.
38       */
39      private static final String MYHASHALG = "md5";
40  
41      /**
42       * IV length.
43       */
44      private static final int IVLEN = 8;
45  
46      /**
47       * Salt length.
48       */
49      private static final int SALTLEN = 8;
50  
51      /**
52       * Iteration count.
53       */
54      private static final int COUNT = 20;
55  
56      /**
57       * Size of key.
58       */
59      private static final int KEYSIZE = 24;
60  
61      /**
62       * local key storage implementation.
63       */
64      private byte[] keyBytes; // NOPMD by kkretsch on 29.03.18 14:53
65  
66      /**
67       * Constructor of Md5DesLayer.
68       */
69      public Md5DesLayer()
70      {
71          super(Md5DesLayer.class, LAYERNAME);
72      }
73  
74      /**
75       * Generate Keys from plain password.
76       *
77       * @throws JastacryException on error
78       */
79      @Override
80      protected final void setupPbe() throws JastacryException
81      {
82          logger.debug("in child setupPBE");
83          pbeKey = new SecretKeySpec(keyBytes, MYKEYALG);
84          pbeSecretKeySpec = new SecretKeySpec(pbeKey.getEncoded(), strKeyAlg);
85      }
86  
87      /**
88       * init function.
89       *
90       * @param data to initialize the crypt value.
91       */
92      @Override
93      public final void init(final String data)
94      {
95          super.init();
96  
97          this.chPasswd = data.toCharArray();
98          try
99          {
100             final MessageDigest msgDigest = MessageDigest.getInstance(MYHASHALG);
101             final byte[] digestOfPassword = msgDigest.digest(data.getBytes(StandardCharsets.UTF_8));
102             keyBytes = Arrays.copyOf(digestOfPassword, KEYSIZE);
103         }
104         catch (final NoSuchAlgorithmException e)
105         {
106             logger.catching(e);
107         }
108     }
109 
110     @Override
111     protected final String getMyAlg()
112     {
113         return MYALG;
114     }
115 
116     @Override
117     protected final String getMyKeyAlg()
118     {
119         return MYKEYALG;
120     }
121 
122     @Override
123     protected int getMySaltLen()
124     {
125         return SALTLEN;
126     }
127 
128     @Override
129     protected int getMyIvLen()
130     {
131         return IVLEN;
132     }
133 
134     @Override
135     protected int getMyCount()
136     {
137         return COUNT;
138     }
139 
140     @Override
141     protected int getMyKeysize()
142     {
143         return KEYSIZE;
144     }
145 
146     /**
147      * Override equals method from object class.
148      * @param o object to compare with
149      * @return true or false
150      */
151     @Override
152     public boolean equals(final Object o)
153     {
154         if (o == this)
155         {
156             return true;
157         }
158         if (!(o instanceof Md5DesLayer))
159         {
160             return false;
161         }
162 
163         final Md5DesLayer./../org/jastacry/layer/Md5DesLayer.html#Md5DesLayer">Md5DesLayer layer = (Md5DesLayer) o;
164         return Arrays.equals(layer.keyBytes, this.keyBytes);
165     }
166 
167     /**
168      * Override equals method from object class.
169      * @return hash of properties
170      */
171     @Override
172     public int hashCode()
173     {
174         return Arrays.hashCode(keyBytes);
175     }
176 }