001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.util;
018
019import org.apache.logging.log4j.util.Strings;
020
021import java.nio.charset.Charset;
022import java.security.MessageDigest;
023import java.security.NoSuchAlgorithmException;
024import java.util.Objects;
025
026/**
027 *
028 */
029public final class NameUtil {
030
031    private NameUtil() {}
032
033    public static String getSubName(final String name) {
034        if (Strings.isEmpty(name)) {
035            return null;
036        }
037        final int i = name.lastIndexOf('.');
038        return i > 0 ? name.substring(0, i) : Strings.EMPTY;
039    }
040
041    /**
042     * Calculates the <a href="https://en.wikipedia.org/wiki/MD5">MD5</a> hash
043     * of the given input string encoded using the default platform
044     * {@link Charset charset}.
045     * <p>
046     * <b>MD5 has severe vulnerabilities and should not be used for sharing any
047     * sensitive information.</b> This function should only be used to create
048     * unique identifiers, e.g., configuration element names.
049     *
050     * @param input string to be hashed
051     * @return string composed of 32 hexadecimal digits of the calculated hash
052     */
053    public static String md5(final String input) {
054        Objects.requireNonNull(input, "input");
055        try {
056            final byte[] inputBytes = input.getBytes();
057            final MessageDigest digest = MessageDigest.getInstance("MD5");
058            final byte[] bytes = digest.digest(inputBytes);
059            final StringBuilder md5 = new StringBuilder(bytes.length * 2);
060            for (final byte b : bytes) {
061                final String hex = Integer.toHexString(0xFF & b);
062                if (hex.length() == 1) {
063                    md5.append('0');
064                }
065                md5.append(hex);
066            }
067            return md5.toString();
068        }
069        // Every implementation of the Java platform is required to support MD5.
070        // Hence, this catch block should be unreachable.
071        // See https://docs.oracle.com/javase/8/docs/api/java/security/MessageDigest.html
072        // for details.
073        catch (final NoSuchAlgorithmException error) {
074            throw new RuntimeException(error);
075        }
076    }
077
078}