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.skplanet.syruppay.token.tav.TransactionAuthenticationValue.java

public boolean isValidBy(final String key, final String checksum)
        throws NoSuchAlgorithmException, IOException, InvalidKeyException {
    final Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(new SecretKeySpec(key.getBytes("UTF-8"), mac.getAlgorithm()));
    mac.update((cardToken + mctTransAuthId + ocTransAuthId
            + new ObjectMapper().writeValueAsString(paymentAuthenticationDetail)).getBytes("UTF-8"));
    return Base64.encodeBase64URLSafeString(mac.doFinal()).equals(checksum);
}

From source file:com.zns.comicdroid.amazon.SignedRequestsHelper.java

/**
 * You must provide the three values below to initialize the helper.
 *  /*w ww. ja va 2  s  . com*/
 * @param endpoint          Destination for the requests.
 * @param awsAccessKeyId    Your AWS Access Key ID
 * @param awsSecretKey      Your AWS Secret Key
 */
@SuppressLint("DefaultLocale")
public static SignedRequestsHelper getInstance(String endpoint, String awsAccessKeyId, String awsSecretKey)
        throws IllegalArgumentException, UnsupportedEncodingException, NoSuchAlgorithmException,
        InvalidKeyException {
    if (null == endpoint || endpoint.length() == 0) {
        throw new IllegalArgumentException("endpoint is null or empty");
    }
    if (null == awsAccessKeyId || awsAccessKeyId.length() == 0) {
        throw new IllegalArgumentException("awsAccessKeyId is null or empty");
    }
    if (null == awsSecretKey || awsSecretKey.length() == 0) {
        throw new IllegalArgumentException("awsSecretKey is null or empty");
    }

    SignedRequestsHelper instance = new SignedRequestsHelper();
    instance.endpoint = endpoint.toLowerCase(Locale.ENGLISH);
    instance.awsAccessKeyId = awsAccessKeyId;
    instance.awsSecretKey = awsSecretKey;

    byte[] secretyKeyBytes = instance.awsSecretKey.getBytes(UTF8_CHARSET);
    instance.secretKeySpec = new SecretKeySpec(secretyKeyBytes, HMAC_SHA256_ALGORITHM);
    instance.mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);
    instance.mac.init(instance.secretKeySpec);

    return instance;
}

From source file:API.amazon.advertising.SignedRequestsHelper.java

/**
 * You must provide the three values below to initialize the helper.
 *  /*from  w  w  w.j a va  2 s  .  c  o  m*/
 * @param endpoint          Destination for the requests.
 * @param awsAccessKeyId    Your AWS Access Key ID
 * @param awsSecretKey      Your AWS Secret Key
 */
public static SignedRequestsHelper getInstance(String endpoint, String awsAccessKeyId, String awsSecretKey,
        String AssociateTag) throws IllegalArgumentException, UnsupportedEncodingException,
        NoSuchAlgorithmException, InvalidKeyException {
    if (null == endpoint || endpoint.length() == 0) {
        throw new IllegalArgumentException("endpoint is null or empty");
    }
    if (null == awsAccessKeyId || awsAccessKeyId.length() == 0) {
        throw new IllegalArgumentException("awsAccessKeyId is null or empty");
    }
    if (null == awsSecretKey || awsSecretKey.length() == 0) {
        throw new IllegalArgumentException("awsSecretKey is null or empty");
    }

    SignedRequestsHelper instance = new SignedRequestsHelper();
    instance.endpoint = endpoint.toLowerCase();
    instance.awsAccessKeyId = awsAccessKeyId;
    instance.awsSecretKey = awsSecretKey;
    instance.AssociateTag = AssociateTag;

    byte[] secretyKeyBytes = instance.awsSecretKey.getBytes(UTF8_CHARSET);
    instance.secretKeySpec = new SecretKeySpec(secretyKeyBytes, HMAC_SHA256_ALGORITHM);
    instance.mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);
    instance.mac.init(instance.secretKeySpec);

    return instance;
}

From source file:com.janrain.backplane.common.HmacHashUtils.java

private static String hmacSign(SecretKey key, String password)
        throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
    Mac mac = Mac.getInstance(key.getAlgorithm());
    mac.init(key);// w ww .ja  va  2s.  c  om
    return new String(Base64.encodeBase64(mac.doFinal(password.getBytes())), UTF8_STRING_ENCODING);
}

From source file:com.emc.atmos.api.RestUtil.java

public static String sign(String string, byte[] hashKey) {
    try {/*from  w  w  w.jav  a  2  s .  c o m*/
        // Compute the signature hash
        l4j.debug("Hashing: \n" + string);

        byte[] input = string.getBytes("UTF-8");

        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec key = new SecretKeySpec(hashKey, "HmacSHA1");
        mac.init(key);

        byte[] hashBytes = mac.doFinal(input);

        // Encode the hash in Base64.
        String hash = new String(Base64.encodeBase64(hashBytes), "UTF-8");

        l4j.debug("Hash: " + hash);

        return hash;
    } catch (Exception e) {
        throw new RuntimeException("Error signing string:\n" + string + "\n", e);
    }
}

From source file:model.connection.amazon.SignedRequestsHelper.java

/**
 * You must provide the three values below to initialize the helper.
 *  /*from   w ww .  j  a v  a2 s .c  o  m*/
 * @param endpoint          Destination for the requests.
 * @param awsAccessKeyId    Your AWS Access Key ID
 * @param awsSecretKey      Your AWS Secret Key
 */
public static SignedRequestsHelper getInstance(String protocol, String endpoint, String awsAccessKeyId,
        String awsSecretKey) throws IllegalArgumentException, UnsupportedEncodingException,
        NoSuchAlgorithmException, InvalidKeyException {
    if (null == endpoint || endpoint.length() == 0) {
        throw new IllegalArgumentException("endpoint is null or empty");
    }
    if (null == protocol || protocol.length() == 0) {
        throw new IllegalArgumentException("protocol is null or empty");
    }
    if (null == awsAccessKeyId || awsAccessKeyId.length() == 0) {
        throw new IllegalArgumentException("awsAccessKeyId is null or empty");
    }
    if (null == awsSecretKey || awsSecretKey.length() == 0) {
        throw new IllegalArgumentException("awsSecretKey is null or empty");
    }

    SignedRequestsHelper instance = new SignedRequestsHelper();
    instance.protocol = protocol.toLowerCase();
    instance.endpoint = endpoint.toLowerCase();
    instance.awsAccessKeyId = awsAccessKeyId;
    instance.awsSecretKey = awsSecretKey;

    byte[] secretyKeyBytes = instance.awsSecretKey.getBytes(UTF8_CHARSET);
    instance.secretKeySpec = new SecretKeySpec(secretyKeyBytes, HMAC_SHA256_ALGORITHM);
    instance.mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);
    instance.mac.init(instance.secretKeySpec);

    return instance;
}

From source file:org.apache.shindig.common.crypto.Crypto.java

/**
 * Verifies an HMAC SHA1 hash.  Throws if the verification fails.
 * //w  ww . j  a va2 s  .  c  o  m
 * @param key
 * @param in
 * @param expected
 * @throws GeneralSecurityException
 */
public static void hmacSha1Verify(byte[] key, byte[] in, byte[] expected) throws GeneralSecurityException {
    Mac hmac = Mac.getInstance(HMAC_TYPE);
    Key hmacKey = new SecretKeySpec(key, HMAC_TYPE);
    hmac.init(hmacKey);
    hmac.update(in);
    byte actual[] = hmac.doFinal();
    if (actual.length != expected.length) {
        throw new GeneralSecurityException("HMAC verification failure");
    }
    for (int i = 0; i < actual.length; i++) {
        if (actual[i] != expected[i]) {
            throw new GeneralSecurityException("HMAC verification failure");
        }
    }
}

From source file:apaapi4j.SignedRequestsHelper.java

private SignedRequestsHelper(String endpoint, String awsAccessKeyId, String awsSecretKey) {
    try {/*from   w  w w .j a  va 2  s . com*/
        this.endpoint = endpoint.toLowerCase();
        this.awsAccessKeyId = awsAccessKeyId;

        byte[] secretyKeyBytes = awsSecretKey.getBytes(UTF8_CHARSET);
        this.secretKeySpec = new SecretKeySpec(secretyKeyBytes, HMAC_SHA256_ALGORITHM);
        this.mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);
        this.mac.init(this.secretKeySpec);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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);/*ww w  .  j  a v a2s. com*/
    byte[] rawHmac = mac.doFinal(message.getBytes(Charsets.UTF_8));
    String result = new String(Hex.encodeHexString(rawHmac));
    return result;
}

From source file:com.example.amazon.SignedRequestsHelper.java

/**
 * You must provide the three values below to initialize the helper.
 * /*from  www .  j  ava 2  s .c  om*/
 * @param endpoint
 *            Destination for the requests.
 * @param awsAccessKeyId
 *            Your AWS Access Key ID
 * @param awsSecretKey
 *            Your AWS Secret Key
 */
public static SignedRequestsHelper getInstance(String endpoint, String awsAccessKeyId, String awsSecretKey,
        String awsAssociateTag) throws IllegalArgumentException, UnsupportedEncodingException,
        NoSuchAlgorithmException, InvalidKeyException {
    if (null == endpoint || endpoint.length() == 0) {
        throw new IllegalArgumentException("endpoint is null or empty");
    }
    if (null == awsAccessKeyId || awsAccessKeyId.length() == 0) {
        throw new IllegalArgumentException("awsAccessKeyId is null or empty");
    }
    if (null == awsSecretKey || awsSecretKey.length() == 0) {
        throw new IllegalArgumentException("awsSecretKey is null or empty");
    }
    if (null == awsAssociateTag || awsAssociateTag.length() == 0) {
        throw new IllegalArgumentException("awsAssociateTag is null or empty");
    }

    SignedRequestsHelper instance = new SignedRequestsHelper();
    instance.endpoint = endpoint.toLowerCase();
    instance.awsAccessKeyId = awsAccessKeyId;
    instance.awsSecretKey = awsSecretKey;
    instance.awsAssociateTag = awsAssociateTag;

    byte[] secretyKeyBytes = instance.awsSecretKey.getBytes(UTF8_CHARSET);
    instance.secretKeySpec = new SecretKeySpec(secretyKeyBytes, HMAC_SHA256_ALGORITHM);
    instance.mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);
    instance.mac.init(instance.secretKeySpec);

    return instance;
}