Example usage for javax.crypto Mac getInstance

List of usage examples for javax.crypto Mac getInstance

Introduction

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

Prototype

public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a Mac object that implements the specified MAC algorithm.

Usage

From source file:com.amazon.advertising.api.common.HmacSecurityHandler.java

/**
 * Initializes the handler. Will throw RuntimeException if
 *//*from  w w w.  jav  a  2  s. c  o m*/
public void init() {
    super.init();

    this.awsAccessKeyId = (String) getOption(OPTION_AWS_ACCESS_KEY_ID);
    this.awsSecretKey = (String) getOption(OPTION_AWS_SECRETY_KEY);

    if (null == this.awsAccessKeyId || null == this.awsSecretKey || this.awsAccessKeyId.length() == 0
            || this.awsSecretKey.length() == 0) {
        throw new RuntimeException("Missing configuration for handler!");
    }

    try {
        byte[] bytes = awsSecretKey.getBytes("UTF-8");
        this.keySpec = new SecretKeySpec(bytes, SIGNATURE_ALGORITHM);
        this.mac = Mac.getInstance(SIGNATURE_ALGORITHM);
        this.mac.init(keySpec);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    }

}

From source file:fr.Axeldu18.PterodactylAPI.PterodactylAPI.java

public String hmac(String url) throws Exception {
    try {//from w w w .  j  av a  2  s.  c om
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(url.getBytes()));
        return hash;
    } catch (Exception e) {
        log(Level.SEVERE, " HMAC Error");
        return null;
    }
}

From source file:crow.weibo.util.WeiboUtil.java

/**
 * Sina tencent  HmacSHA1 ???/*from ww w.  ja  va2  s . co  m*/
 * 
 * @param base
 *            ???
 * @param consumerSecret
 *            ??API Secret
 * @param accessTokenSecret
 *            ?? Access Secret
 * @return
 */
public static String hmacSHA1Signature(String base, String consumerSecret, String accessTokenSecret) {
    String HMAC_SHA1 = "HmacSHA1";
    try {
        Mac mac = Mac.getInstance(HMAC_SHA1);
        String oauthSignature = encode(consumerSecret) + "&"
                + ((accessTokenSecret == null) ? "" : encode(accessTokenSecret));
        SecretKeySpec spec = new SecretKeySpec(oauthSignature.getBytes(), HMAC_SHA1);
        mac.init(spec);
        byte[] bytes = mac.doFinal(base.getBytes());
        return new String(BASE64Encoder.encode(bytes));
    } catch (Exception e) {
    }
    return null;
}

From source file:com.otaupdater.utils.Utils.java

public static String hmac(String str, String key) {
    try {//from   w ww .  j a v  a  2 s  .  c  o m
        Mac mac = Mac.getInstance(Config.HMAC_ALGORITHM);
        String salt = randomSaltString(mac.getMacLength());
        mac.init(new SecretKeySpec(key.getBytes(), mac.getAlgorithm()));
        return byteArrToStr(mac.doFinal((salt + str + salt).getBytes("UTF-8"))) + salt;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:be.fedict.eid.idp.model.CryptoUtil.java

public static Mac getMac(byte[] hmacSecret) throws InvalidKeyException {

    SecretKey macKey = new SecretKeySpec(hmacSecret, "HmacSHA1");
    Mac mac;//from www  . j  ava2s  . c o  m
    try {
        mac = Mac.getInstance(macKey.getAlgorithm());
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("HMAC algo not available: " + e.getMessage());
    }
    mac.init(macKey);
    return mac;
}

From source file:com.zimbra.common.auth.twofactor.TOTPAuthenticator.java

private byte[] calculateHash(byte[] K, byte[] C) throws ServiceException {
    try {//from w ww. j a va 2  s  .co m
        Mac mac = Mac.getInstance(config.getHashAlgorithm().getLabel());
        mac.init(new SecretKeySpec(K, config.getHashAlgorithm().getLabel()));
        byte[] hash = mac.doFinal(C);
        return hash;
    } catch (NoSuchAlgorithmException e) {
        throw ServiceException.FAILURE("no such algorithm", e);
    } catch (InvalidKeyException e) {
        throw ServiceException.FAILURE("invalid key", e);
    }
}

From source file:com.sina.auth.AbstractAWSSigner.java

protected byte[] sign(byte[] data, byte[] key, SigningAlgorithm algorithm) throws SCSClientException {
    try {/*w ww.  java  2  s .c om*/
        Mac mac = Mac.getInstance(algorithm.toString());
        mac.init(new SecretKeySpec(key, algorithm.toString()));
        return mac.doFinal(data);
    } catch (Exception e) {
        throw new SCSClientException("Unable to calculate a request signature: " + e.getMessage(), e);
    }
}

From source file:org.apache.abdera2.common.security.HashHelper.java

public static String hmac(Key key, String alg, byte[] mat) {
    try {/*from   w  ww. j  a va2  s.  com*/
        Mac mac = Mac.getInstance(alg);
        mac.init(key);
        mac.update(mat, 0, mat.length);
        byte[] sig = mac.doFinal();
        return Base64.encodeBase64URLSafeString(sig);
    } catch (Throwable t) {
        throw ExceptionHelper.propogate(t);
    }
}

From source file:co.edu.uniandes.csw.Arquidalgos.usuario.service.UsuarioService.java

@POST
@Path("/darAmigos")
public List<UsuarioDTO> darAmigos(UsuarioDTO usuario) throws Exception {
    System.out.println("Dar amigos service de: " + usuario.getFacebookId());
    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    String key = "123";
    SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
    sha256_HMAC.init(secret_key);/*from   w w w.ja v a  2s. com*/

    System.out.println("TO String: " + usuario.toString());

    String hash = Hex.encodeHexString(sha256_HMAC.doFinal(usuario.toString().getBytes()));
    System.out.println("CODIGO HASH: " + hash);
    System.out.println("CODIGO HASH JSON " + usuario.getHash());

    boolean alterado = !(hash.equalsIgnoreCase(usuario.getHash()));
    System.out.println("Alterado: " + alterado);

    if (alterado) {
        throw new Exception("Se han alterado los datos");
    }

    return this.usuarioLogicService.darAmigosUsuario(usuario.getFacebookId());
}

From source file:cn.ctyun.amazonaws.auth.AbstractAWSSigner.java

protected byte[] sign(byte[] data, byte[] key, SigningAlgorithm algorithm) throws AmazonClientException {
    try {/*from w  ww .  j  av  a 2  s . c  o m*/
        Mac mac = Mac.getInstance(algorithm.toString());
        mac.init(new SecretKeySpec(key, algorithm.toString()));
        return mac.doFinal(data);
    } catch (Exception e) {
        throw new AmazonClientException("Unable to calculate a request signature: " + e.getMessage(), e);
    }
}