2 * Copyright (c) 2010-2020 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.opensprinkler.internal.util;
15 import java.security.MessageDigest;
18 * The {@link Hash} class contains static methods for creating hashes
19 * of strings. Usually for password hashing.
21 * @author Chris Graham - Initial contribution
24 private static final String MD5_HASH_ALGORITHM = "MD5";
25 private static final String UTF8_CHAR_SET = "UTF-8";
28 * Given a string, return the MD5 hash of the String.
30 * @param unhashed The string contents to be hashed.
31 * @return MD5 Hashed value of the String. Null if there is a problem hashing the String.
33 public static String getMD5Hash(String unhashed) {
35 byte[] bytesOfMessage = unhashed.getBytes(UTF8_CHAR_SET);
37 MessageDigest md5 = MessageDigest.getInstance(MD5_HASH_ALGORITHM);
39 byte[] hash = md5.digest(bytesOfMessage);
41 StringBuilder sb = new StringBuilder(2 * hash.length);
44 sb.append(String.format("%02x", b & 0xff));
47 String digest = sb.toString();
50 } catch (Exception exp) {