]> git.basschouten.com Git - openhab-addons.git/blob
0514c8b2222e54c82521aa6167ef27c033dd8b4c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.ecovacs.internal.api.util;
14
15 import java.security.MessageDigest;
16 import java.security.NoSuchAlgorithmException;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * @author Johannes Ptaszyk - Initial contribution
24  */
25 @NonNullByDefault
26 public class MD5Util {
27     private static final Logger LOGGER = LoggerFactory.getLogger(MD5Util.class);
28
29     private MD5Util() {
30         // Prevent instantiation of util class
31     }
32
33     public static String getMD5Hash(String input) {
34         MessageDigest md;
35         try {
36             md = MessageDigest.getInstance("MD5");
37         } catch (NoSuchAlgorithmException e) {
38             LOGGER.error("Could not get MD5 MessageDigest instance", e);
39             return "";
40         }
41         md.update(input.getBytes());
42         byte[] hash = md.digest();
43         StringBuilder hexString = new StringBuilder();
44         for (byte b : hash) {
45             if ((0xff & b) < 0x10) {
46                 hexString.append("0").append(Integer.toHexString((0xFF & b)));
47             } else {
48                 hexString.append(Integer.toHexString(0xFF & b));
49             }
50         }
51         return hexString.toString();
52     }
53 }