Example usage for javax.crypto Mac doFinal

List of usage examples for javax.crypto Mac doFinal

Introduction

In this page you can find the example usage for javax.crypto Mac doFinal.

Prototype

public final byte[] doFinal(byte[] input) throws IllegalStateException 

Source Link

Document

Processes the given array of bytes and finishes the MAC operation.

Usage

From source file:org.esupportail.papercut.services.HashService.java

public String getHMac(String input) {
    try {//from  w w  w  .j  a  v a 2 s  . c om
        Mac mac = Mac.getInstance("HmacSHA512");
        mac.init(secretKey);
        final byte[] macData = mac.doFinal(input.getBytes());
        byte[] hex = new Hex().encode(macData);
        String hmac = new String(hex, "ISO-8859-1").toUpperCase();
        log.debug(input);
        log.debug(hmac);
        return hmac;
    } catch (Exception e) {
        log.error("Error during encoding data ...");
        throw new RuntimeException(e);
    }
}

From source file:io.pivotal.cla.security.GitHubSignature.java

private byte[] sign(String body, String token) throws NoSuchAlgorithmException, InvalidKeyException {
    SecretKeySpec signingKey = new SecretKeySpec(token.getBytes(), HMAC_SHA1_ALGORITHM);
    Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
    mac.init(signingKey);/*w w w . java 2s.c om*/
    byte[] expectedBytes = mac.doFinal(body.getBytes());
    return expectedBytes;
}

From source file:com.parworks.androidlibrary.utils.HMacShaPasswordEncoder.java

public String encodePassword(String rawDataToBeEncrypted, Object salt) {
    byte[] hmacData = null;
    if (rawDataToBeEncrypted != null) {
        try {/*w w  w  . j  a  v  a  2 s  . c  o m*/
            SecretKeySpec secretKey = new SecretKeySpec(rawDataToBeEncrypted.getBytes(ENCODING_FOR_ENCRYPTION),
                    this.algorithm);
            Mac mac = getMac();
            mac.init(secretKey);
            hmacData = mac.doFinal(salt.toString().getBytes(ENCODING_FOR_ENCRYPTION));

            if (isEncodeHashAsBas64()) {
                return new String(Base64.encode(hmacData), ENCODING_FOR_ENCRYPTION);
            } else {
                return new String(hmacData, ENCODING_FOR_ENCRYPTION);
            }

        } catch (InvalidKeyException ike) {
            throw new RuntimeException("Invalid Key while encrypting.", ike);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Unsupported Encoding while encrypting.", e);
        }
    }
    return "";

}

From source file:org.mayocat.webhooks.Webhooks.java

private String hmac(String secret, String message) throws GeneralSecurityException {
    SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), HMAC_SHA256);
    Mac mac = Mac.getInstance(HMAC_SHA256);
    mac.init(signingKey);//from w w  w .jav  a 2s  .  c  o  m
    byte[] rawHmac = mac.doFinal(message.getBytes(Charsets.UTF_8));
    String result = new String(Hex.encodeHexString(rawHmac));
    return result;
}

From source file:com.ait.tooling.server.core.security.SimpleKeyStringSigningProvider.java

private final String hmac(final String text) {
    try {// ww w.ja  va2s  . co m
        final Mac hmac = Mac.getInstance(HMAC_ALGORITHM);

        hmac.init(m_secret);

        return Hex.encodeHexString(hmac.doFinal(text.getBytes(IHTTPConstants.CHARSET_UTF_8)));
    } catch (Exception e) {
        logger.error("hmac error", e);

        throw new IllegalArgumentException(e);
    }
}

From source file:br.com.ingenieux.jenkins.plugins.codecommit.RequestSignerBase.java

protected byte[] hash(byte[] kSecret, String obj) {
    try {//from  w ww.  jav a2  s.  c  om
        SecretKeySpec keySpec = new SecretKeySpec(kSecret, "HmacSHA256");

        Mac mac = Mac.getInstance("HmacSHA256");

        mac.init(keySpec);

        return mac.doFinal(obj.getBytes(DEFAULT_CHARSET));
    } catch (Exception exc) {
        throw new RuntimeException(exc);
    }
}

From source file:com.github.tojo.session.cookies.SignatureStrategyDefaultImpl.java

@Override
public byte[] sign(byte[] sessionData) {
    assertNotNullAndEmpty(sessionData);//from   w  w w.j a  va 2 s  .c  o  m

    byte[] signature = null;
    try {
        Mac mac = Mac.getInstance(HMAC_SHA256);
        mac.init(key);
        signature = mac.doFinal(sessionData);
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new InitializationException(e);
    }

    byte[] signedSessionData = ArrayUtils.addAll(signature, sessionData);
    return signedSessionData;
}

From source file:com.amazonaws.services.ec2.util.S3UploadPolicy.java

private String signPolicy(String awsSecretKey, String base64EncodedPolicy)
        throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
    SecretKeySpec signingKey = new SecretKeySpec(awsSecretKey.getBytes(), HMAC_SHA1_ALGORITHM);
    Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
    mac.init(signingKey);/* w  w w  . j a v  a  2s.  co  m*/
    return base64Encode(mac.doFinal(base64EncodedPolicy.getBytes()));
}

From source file:org.mule.module.fws.api.PortProvider.java

private String sign(String data, String key) throws SignatureException {
    byte[] signature;
    try {//from w w w .j av  a  2  s  .c o m
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(new SecretKeySpec(key.getBytes(), "HmacSHA1"));
        signature = Base64.encodeBase64(mac.doFinal(data.getBytes("UTF-8")));
    } catch (Exception e) {
        throw new SignatureException("Failed to generate signature: " + e.getMessage(), e);
    }
    return new String(signature);
}

From source file:org.springframework.social.facebook.web.SignedRequestDecoder.java

private byte[] encrypt(String base, String key) {
    try {//from   www.j  a  va 2 s. c o m
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), HMAC_SHA256_MAC_NAME);
        Mac mac = Mac.getInstance(HMAC_SHA256_MAC_NAME);
        mac.init(secretKeySpec);
        return mac.doFinal(base.getBytes());
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeyException e) {
        throw new IllegalStateException(e);
    }
}