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;
021
022/**
023 *
024 * @deprecated This class will be made package-private in the next major release.
025 */
026@Deprecated
027public abstract class AbstractHasher {
028
029    /**
030     * Calculates hash for string. assume string is UTF-8 encoded
031     *
032     * @param input string which is going to be encoded into requested format
033     * @return  hashed representation of the input string
034     * @throws NoSuchAlgorithmException if the algorithm is not available.
035     * @throws InvalidKeyException Only applicable to HMAC encoding types, when a bad key is provided.
036     */
037    public String calculate(String input) throws NoSuchAlgorithmException, InvalidKeyException {
038        try {
039            return calculate(input, "UTF-8");
040        } catch (UnsupportedEncodingException e) {
041            return null; // -- impossible --
042        }
043    }
044
045    /**
046     * Calculates hash for string.
047     *
048     * @param input string which is going to be encoded into requested format
049     * @param secretKey The secret key for the input
050     * @param encoding The encoding type of the string
051     * @return  hashed representation of the input string
052     * @throws NoSuchAlgorithmException if the algorithm is not available.
053     * @throws UnsupportedEncodingException if the encoding type is invalid
054     * @throws InvalidKeyException Only applicable to HMAC encoding types, when a bad key is provided.
055     */
056    public String calculate(String input, String secretKey, String encoding) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
057        return calculate(input + secretKey, encoding);
058    }
059
060    /**
061     * Calculates hash for string.
062     *
063     * @param input string which is going to be encoded into requested format
064     * @param encoding The encoding type of the string
065     * @return  hashed representation of the input string
066     * @throws NoSuchAlgorithmException if the algorithm is not available.
067     * @throws UnsupportedEncodingException if the encoding type is invalid
068     * @throws InvalidKeyException Only applicable to HMAC encoding types, when a bad key is provided.
069     */
070    public abstract String calculate(String input, String encoding) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException;
071
072    protected String buildHexString(byte[] digest) {
073        final StringBuilder hexString = new StringBuilder();
074        for (byte element : digest) {
075            int z = 0xFF & element;
076            if (z < 16)
077                hexString.append("0");
078            hexString.append(Integer.toHexString(z));
079        }
080
081        return hexString.toString();
082    }
083}