001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.auth.hashutils;
017
018import java.io.UnsupportedEncodingException;
019import java.security.InvalidKeyException;
020import java.security.NoSuchAlgorithmException;
021import java.util.HashMap;
022import java.util.Map;
023
024/**
025 * Utility methods for hashing strings.
026 */
027public class HashUtil {
028
029    private static final Map<HashType, AbstractHasher> HASH_TYPES;
030
031    static {
032        HASH_TYPES = new HashMap<>(8);
033        HASH_TYPES.put(HashType.MD5, new Md5Hasher());
034        HASH_TYPES.put(HashType.HMAC_SHA1, new HmacSha1Hasher());
035        HASH_TYPES.put(HashType.HMAC_MD5, new HmacMd5Hasher());
036        HASH_TYPES.put(HashType.HMAC_SHA256, new HmacSha256Hasher());
037        HASH_TYPES.put(HashType.HMAC_SHA512, new HmacSha512Hasher());
038    }
039
040    /**
041     * Calculates hash for string. assume string is UTF-8 encoded.
042     *
043     * @param input string which is going to be encoded into requested format
044     * @param hashType The type of hash to be applied to the input string
045     * @return representation of the input string with given hash type
046     * @throws NoSuchAlgorithmException if the algorithm is not available.
047     * @throws InvalidKeyException Only applicable to HMAC encoding types, when a bad key is provided.
048     *
049     * @deprecated This will be removed in the next major release.
050     */
051    @Deprecated
052    public static String calculate(String input, HashType hashType) throws NoSuchAlgorithmException, InvalidKeyException {
053        return HASH_TYPES.get(hashType).calculate(input);
054    }
055
056    /**
057     * Calculates hash for string.
058     *
059     * @param input string which is going to be encoded into requested format
060     * @param encoding encoding type of input
061     * @param hashType The type of hash to be applied to the input string
062     * @return representation of the input string with given hash type
063     * @throws NoSuchAlgorithmException if the algorithm is not available.
064     * @throws InvalidKeyException Only applicable to HMAC encoding types, when a bad key is provided.
065     * @throws UnsupportedEncodingException if the specified encoding is unavailable.
066     *
067     * @deprecated This will be removed in the next major release.
068     */
069    @Deprecated
070    public static String calculate(String input, String encoding, HashType hashType) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
071        return HASH_TYPES.get(hashType).calculate(input, encoding);
072    }
073
074    /**
075     * Calculates hash for string.
076     * @param input string which is going to be encoded into requested format
077     * @param secretKey the key to be used for encoding
078     * @param encoding character encoding of the string which is going to be encoded into requested format
079     * @param hashType The type of hash to be applied to the input string
080     * @return representation of the input string with given hash type
081     * @throws NoSuchAlgorithmException if the algorithm is not available.
082     * @throws UnsupportedEncodingException if the specified encoding is unavailable.
083     * @throws InvalidKeyException Only applicable to HMAC encoding types, when a bad key is provided.
084     */
085    public static String calculate(String input, String secretKey, String encoding, HashType hashType) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
086        return HASH_TYPES.get(hashType).calculate(input, secretKey, encoding);
087    }
088
089    public enum HashType {
090        MD5,
091        HMAC_SHA1,
092        HMAC_MD5,
093        HMAC_SHA256,
094        HMAC_SHA512
095    }
096}