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 javax.crypto.Mac;
019import javax.crypto.spec.SecretKeySpec;
020import java.io.UnsupportedEncodingException;
021import java.security.InvalidKeyException;
022import java.security.NoSuchAlgorithmException;
023
024/**
025 * Contains utility methods that use HMAC SHA-1 hashing. The class uses STANDARD JVM crypto Hmac SHA-512 algorithm.
026 *
027 * @deprecated This class will be made package-private in the next major release.
028 */
029@Deprecated
030public class HmacSha1Hasher extends AbstractHasher {
031
032    /**
033     * Calculates HMAC SHA-1 hash for string.
034     *
035     * @param input string which is going to be encoded into HMAC SHA-1 format
036     * @param secretKey The key used for initialization of the algorithm
037     * @param encoding character encoding of the string which is going to be encoded into HMAC SHA-1 format
038     * @return  HMAC SHA-1 representation of the input string
039     * @throws NoSuchAlgorithmException if the HMAC SHA-1 algorithm is not available.
040     * @throws UnsupportedEncodingException if the specified encoding is unavailable.
041     */
042    @Override
043    public String calculate(String input, String secretKey, String encoding) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
044        Mac sha1HMAC = Mac.getInstance("HmacSHA1");
045        SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(encoding), "HmacSHA1");
046
047        sha1HMAC.init(keySpec);
048
049        byte[] digest = sha1HMAC.doFinal(input.getBytes(encoding));
050
051        return buildHexString(digest);
052    }
053
054    /**
055     * Calculates HMAC SHA-1 hash for string.
056     * Secret key that is supplied here is the input itself.
057     *
058     * @param input string which is going to be encoded into HMAC SHA-1 format
059     * @param encoding character encoding of the string which is going to be encoded into HMAC SHA-1 format
060     * @return  HMAC SHA-1 representation of the input string
061     * @throws NoSuchAlgorithmException if the HMAC SHA-1 algorithm is not available.
062     * @throws UnsupportedEncodingException if the specified encoding is unavailable.
063     * @throws InvalidKeyException if key is invalid
064     */
065    @Deprecated
066    @Override
067    public String calculate(String input, String encoding) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
068        return calculate(input, input, encoding);
069    }
070}