001/*
002 * Copyright (c) 2011-2017 Nexmo Inc
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.nexmo.client.auth;
023
024
025import java.io.UnsupportedEncodingException;
026import java.security.MessageDigest;
027import java.security.NoSuchAlgorithmException;
028
029/**
030 * Contains utility methods that use MD5 hashing. The class uses STANDARD JVM MD5 algorithm.
031 *
032 * @author Paul Cook
033 */
034public class MD5Util {
035
036    /**
037     * Calculates MD5 hash for string. assume string is UTF-8 encoded
038     * @param input string which is going to be encoded into MD5 format
039     * @return  MD5 representation of the input string
040     * @throws NoSuchAlgorithmException if the MD5 algorithm is not available.
041     */
042    public static String calculateMd5(String input) throws NoSuchAlgorithmException {
043        try {
044            return calculateMd5(input, "UTF-8");
045        } catch (UnsupportedEncodingException e) {
046            return null; // -- impossible --
047        }
048    }
049
050    /**
051     * Calculates MD5 hash for string.
052     * @param input string which is going to be encoded into MD5 format
053     * @param encoding character encoding of the string which is going to be encoded into MD5 format
054     * @return  MD5 representation of the input string
055     * @throws NoSuchAlgorithmException if the MD5 algorithm is not available.
056     * @throws UnsupportedEncodingException if the specified encoding is unavailable.
057     */
058    public static String calculateMd5(String input, String encoding) throws NoSuchAlgorithmException, UnsupportedEncodingException {
059        MessageDigest md = MessageDigest.getInstance("MD5");
060        md.update(input.getBytes(encoding));
061        byte digest[] = md.digest();
062
063        final StringBuilder hexString = new StringBuilder();
064        for (byte element : digest) {
065            int z = 0xFF & element;
066            if (z < 16)
067                hexString.append("0");
068            hexString.append(Integer.toHexString(z));
069        }
070
071        return hexString.toString();
072    }
073
074}