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-512 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 HmacSha512Hasher extends AbstractHasher {
031
032    /**
033     * Calculates HMAC SHA-512 hash for string.
034     *
035     * @param input string which is going to be encoded into HMAC SHA-512 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-512 format
038     * @return  HMAC SHA-512 representation of the input string
039     * @throws NoSuchAlgorithmException if the HMAC SHA-512 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 sha512HMAC = Mac.getInstance("HmacSHA512");
045        SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(encoding), "HmacSHA512");
046
047        sha512HMAC.init(keySpec);
048
049        byte[] digest = sha512HMAC.doFinal(input.getBytes(encoding));
050
051        return buildHexString(digest);
052    }
053
054    /**
055     * Calculates HMAC SHA-512 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-512 format
059     * @param encoding character encoding of the string which is going to be encoded into HMAC SHA-512 format
060     * @return  HMAC SHA-512 representation of the input string
061     * @throws NoSuchAlgorithmException if the HMAC SHA-512 algorithm is not available.
062     * @throws UnsupportedEncodingException if the specified encoding is unavailable.
063     * @throws InvalidKeyException if key is invalid
064     */
065    @Override
066    public String calculate(String input, String encoding) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
067        return calculate(input, input, encoding);
068    }
069}