Java Digest digestHmacToBase64(String algorithm, String msg, byte[] privateKey)

Here you can find the source of digestHmacToBase64(String algorithm, String msg, byte[] privateKey)

Description

Firma un mensaje y devuelve la firma en formato string base 64

License

Open Source License

Parameter

Parameter Description
algorithm tipo de algoritmo.(HmacSHA1,HmacSHA256,HmacSHA384, HmacSHA512,HmacMD5)
msg mensaje a firmar
privateKey clave privada

Exception

Parameter Description
NoSuchAlgorithmException an exception
UnsupportedEncodingException an exception
InvalidKeyException an exception

Return

firma en formato string base 64.

Declaration

public static String digestHmacToBase64(String algorithm, String msg, byte[] privateKey)
        throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException 

Method Source Code


//package com.java2s;
/*//from w  w  w.j ava  2  s . c o m
* JavaBeanStack FrameWork
*
* Copyright (C) 2017 - 2018 Jorge Enciso
* Email: jorge.enciso.r@gmail.com
*        jenciso@javabeanstack.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301  USA
 */

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    /**
     * Firma un mensaje y devuelve la firma en formato string base 64
     * @param algorithm tipo de algoritmo.(HmacSHA1,HmacSHA256,HmacSHA384, HmacSHA512,HmacMD5)
     * @param msg mensaje a firmar
     * @param privateKey clave privada
     * @return firma en formato string base 64.
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     * @throws InvalidKeyException 
     */
    public static String digestHmacToBase64(String algorithm, String msg, byte[] privateKey)
            throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
        Mac mac = Mac.getInstance(algorithm);
        SecretKeySpec key = new SecretKeySpec(privateKey, algorithm);
        mac.init(key);
        byte[] macBytes = mac.doFinal(msg.getBytes("UTF-8"));
        return Base64.getEncoder().encodeToString(macBytes);
    }
}

Related

  1. digestBased(String text)
  2. digestBySHA256(byte[] source)
  3. digestBytes(String type, byte[]... data)
  4. digestEncrypte(byte[] plainText, String algorithm)
  5. digestHex(String txt)
  6. digestInit()
  7. digestOperation(String algo, byte[]... content)
  8. digestStream(MessageDigest digest, InputStream is)
  9. digestString(MessageDigest md, String s)