Example usage for javax.crypto Mac update

List of usage examples for javax.crypto Mac update

Introduction

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

Prototype

public final void update(ByteBuffer input) 

Source Link

Document

Processes input.remaining() bytes in the ByteBuffer input , starting at input.position() .

Usage

From source file:ch.cyberduck.core.sftp.openssh.OpenSSHHostKeyVerifier.java

private static byte[] hmacSha1Hash(byte[] salt, String hostname) throws IOException {
    try {/*w ww  . ja v a  2 s  . c o  m*/
        final Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(new SecretKeySpec(salt, 0, salt.length, mac.getAlgorithm()));
        mac.update(hostname.getBytes());
        return mac.doFinal();
    } catch (GeneralSecurityException e) {
        throw new IOException(e);
    }
}

From source file:org.linkdroid.PostJob.java

private static Mac initHmacSha1(String secretString, String nonce)
        throws NoSuchAlgorithmException, IOException, InvalidKeyException {
    SecretKey key = new SecretKeySpec(secretString.getBytes(), "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(key);/*from w w  w  .ja v  a2  s. c  o m*/
    if (nonce != null) {
        mac.update(nonce.getBytes(UTF8));
    }
    return mac;
}

From source file:org.opensaml.security.crypto.SigningUtil.java

/**
 * Compute the Message Authentication Code (MAC) value over the supplied input.
 * /* w w  w .ja v a2  s.c o  m*/
 * It is up to the caller to ensure that the specified algorithm ID is consistent with the type of signing key
 * supplied.
 * 
 * @param signingKey the key with which to compute the MAC
 * @param jcaAlgorithmID the Java JCA algorithm ID to use
 * @param input the input over which to compute the MAC
 * @return the computed MAC value
 * @throws SecurityException thrown if the MAC computation results in an error
 */
@Nonnull
public static byte[] signMAC(@Nonnull final Key signingKey, @Nonnull final String jcaAlgorithmID,
        @Nonnull final byte[] input) throws SecurityException {
    Constraint.isNotNull(signingKey, "Secret key cannot be null");
    Constraint.isNotNull(jcaAlgorithmID, "JCA algorithm ID cannot be null");
    Constraint.isNotNull(input, "Input data to sign cannot be null");

    Logger log = getLogger();
    log.debug("Computing MAC over input using key of type {} and JCA algorithm ID {}",
            signingKey.getAlgorithm(), jcaAlgorithmID);

    try {
        Mac mac = Mac.getInstance(jcaAlgorithmID);
        mac.init(signingKey);
        mac.update(input);
        byte[] rawMAC = mac.doFinal();
        log.debug("Computed MAC: {}", Hex.encodeHexString(rawMAC));
        return rawMAC;
    } catch (GeneralSecurityException e) {
        log.error("Error during MAC generation", e);
        throw new SecurityException("Error during MAC generation", e);
    }
}

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

/**
 * HMAC sha1/*from www  . j av a2s  .c om*/
 * 
 * @param key the key must be at least 8 bytes in length.
 * @param in byte array to HMAC.
 * @return the hash
 * 
 * @throws GeneralSecurityException
 */
public static byte[] hmacSha1(byte[] key, byte[] in) throws GeneralSecurityException {
    if (key.length < MIN_HMAC_KEY_LEN) {
        throw new GeneralSecurityException("HMAC key should be at least " + MIN_HMAC_KEY_LEN + " bytes.");
    }
    Mac hmac = Mac.getInstance(HMAC_TYPE);
    Key hmacKey = new SecretKeySpec(key, HMAC_TYPE);
    hmac.init(hmacKey);
    hmac.update(in);
    return hmac.doFinal();
}

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

/**
 * Verifies an HMAC SHA1 hash.  Throws if the verification fails.
 * //from   ww  w.  ja  v a2s .  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:com.cloud.test.utils.UtilsForTest.java

public static String signRequest(String request, String key) {
    try {/*from  w ww  . jav  a 2 s .  c om*/
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        mac.init(keySpec);
        mac.update(request.getBytes());
        byte[] encryptedBytes = mac.doFinal();
        //System.out.println("HmacSHA1 hash: " + encryptedBytes);
        return Base64.encodeBase64String(encryptedBytes);
    } catch (Exception ex) {
        System.out.println("unable to sign request");
        ex.printStackTrace();
    }
    return null;
}

From source file:org.linkdroid.PostJob.java

private static final String calculateBundleExtrasHmac(Bundle data, String secretString)
        throws NoSuchAlgorithmException, IOException, InvalidKeyException {
    Mac mac = initHmacSha1(secretString, null);
    SortedSet<String> keys = new TreeSet<String>(data.keySet());
    for (String key : keys) {
        mac.update(key.getBytes(UTF8));
        Object value = data.get(key);
        // We only add the value to the hmac digest if it exists; the key is still
        // part of the digest calculation.
        if (value != null) {
            mac.update(value.toString().getBytes(UTF8));
        }/* w w  w  .  j a  v  a  2  s.co  m*/
    }
    return hmacDigestToHexString(mac.doFinal());
}

From source file:com.alibaba.openapi.client.util.SignatureUtil.java

public static byte[] hmacSha1(String path, List<NameValuePair> parameters, SecretKeySpec signingKey) {
    Mac mac;
    try {//w  w  w .  j a v a2 s .c o m
        mac = Mac.getInstance(HMAC_SHA1);
        mac.init(signingKey);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e.getMessage(), e);
    } catch (InvalidKeyException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
    mac.update(path.getBytes(CHARSET_UTF8));
    Collections.sort(parameters, new NameValuePairComparator<NameValuePair>());
    for (NameValuePair parameter : parameters) {
        mac.update(parameter.getName().getBytes(CHARSET_UTF8));
        mac.update(parameter.getValue().getBytes(CHARSET_UTF8));
    }
    return mac.doFinal();
}

From source file:org.apache.cloudstack.region.RegionsApiUtil.java

/**
 * 1. Signs a string with a secret key using SHA-1 2. Base64 encode the result 3. URL encode the final result
 *
 * @param request/*from  ww  w . j ava  2 s  . com*/
 * @param key
 * @return
 */
private static String signRequest(String request, String key) {
    try {
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        mac.init(keySpec);
        mac.update(request.getBytes());
        byte[] encryptedBytes = mac.doFinal();
        return URLEncoder.encode(Base64.encodeBase64String(encryptedBytes), "UTF-8");
    } catch (Exception ex) {
        s_logger.error(ex.getMessage());
        return null;
    }
}

From source file:com.alibaba.openapi.client.util.SignatureUtil.java

public static byte[] hmacSha1(String[] datas, SecretKeySpec signingKey) {
    Mac mac;
    try {/*from   w w  w.j  a v a2  s. com*/
        mac = Mac.getInstance(HMAC_SHA1);
        mac.init(signingKey);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e.getMessage(), e);
    } catch (InvalidKeyException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
    for (String data : datas) {
        mac.update(data.getBytes(CHARSET_UTF8));
    }
    return mac.doFinal();
}