View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2018 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.qa.glnm.format;
21  
22  import java.nio.charset.StandardCharsets;
23  import java.security.MessageDigest;
24  import java.security.NoSuchAlgorithmException;
25  import java.util.Base64;
26  
27  import org.apache.commons.codec.digest.MessageDigestAlgorithms;
28  
29  import io.wcm.qa.glnm.exceptions.GaleniumException;
30  
31  final class Md5Util {
32  
33    private Md5Util() {
34      // do not instantiate
35    }
36  
37    /**
38     * <p>getMd5AsAscii.</p>
39     *
40     * @param input a byte array.
41     * @return a {@link java.lang.String} object.
42     * @since 3.0.0
43     */
44    public static String getMd5AsAscii(byte[] input) {
45      try {
46        MessageDigest digest = MessageDigest.getInstance(MessageDigestAlgorithms.MD5);
47        byte[] digestedBytes = digest.digest(input);
48        return getBytesAsAscii(digestedBytes);
49      }
50      catch (NoSuchAlgorithmException ex) {
51        throw new GaleniumException("Java not remembering MD5.", ex);
52      }
53    }
54  
55    private static String getBytesAsAscii(byte[] bytesToEncode) {
56      byte[] encodedBytes = Base64.getEncoder().encode(bytesToEncode);
57      return new String(encodedBytes, StandardCharsets.US_ASCII);
58    }
59  
60  }