2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.haassohnpelletstove.internal;
15 import java.nio.charset.Charset;
16 import java.nio.charset.StandardCharsets;
17 import java.security.MessageDigest;
18 import java.security.NoSuchAlgorithmException;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
24 * The {@link MD5Utils} is responsible for generating the MD5 hash
27 * @author Christian Feininger - Initial contribution
30 public class MD5Utils {
32 private static final Charset UTF_8 = StandardCharsets.UTF_8;
34 private static byte[] digest(byte[] input) {
37 md = MessageDigest.getInstance("MD5");
38 } catch (NoSuchAlgorithmException e) {
39 throw new IllegalArgumentException(e);
41 return md.digest(input);
44 private static String bytesToHex(byte[] bytes) {
45 StringBuilder sb = new StringBuilder();
46 for (byte b : bytes) {
47 sb.append(String.format("%02x", b));
53 * Returns an encrypted MD5 string
55 * @param input nonce as input
56 * @return Encrypted String
58 public static String getMD5String(@Nullable String input) {
60 byte[] md5InBytes = MD5Utils.digest(input.getBytes(UTF_8));
61 return bytesToHex(md5InBytes);