Example usage for javax.crypto.spec SecretKeySpec getAlgorithm

List of usage examples for javax.crypto.spec SecretKeySpec getAlgorithm

Introduction

In this page you can find the example usage for javax.crypto.spec SecretKeySpec getAlgorithm.

Prototype

public String getAlgorithm() 

Source Link

Document

Returns the name of the algorithm associated with this secret key.

Usage

From source file:lucee.commons.io.res.type.s3.S3.java

private static byte[] HMAC_SHA1(String key, String message, String charset)
        throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {

    SecretKeySpec sks = new SecretKeySpec(key.getBytes(charset), "HmacSHA1");
    Mac mac = Mac.getInstance(sks.getAlgorithm());
    mac.init(sks);// ww w  .  j a v  a2 s . c o  m
    mac.update(message.getBytes(charset));
    return mac.doFinal();

}

From source file:lti.oauth.OAuthMessageSigner.java

/**
 * This method double encodes the parameter keys and values.
 * Thus, it expects the keys and values contained in the 'parameters' SortedMap
 * NOT to be encoded.//w w w.j ava 2s  .co  m
 * 
 * @param secret
 * @param algorithm
 * @param method
 * @param url
 * @param parameters
 * @return oauth signature
 * @throws Exception
 */
public String sign(String secret, String algorithm, String method, String url,
        SortedMap<String, String> parameters) throws Exception {
    SecretKeySpec secretKeySpec = new SecretKeySpec((secret.concat(OAuthUtil.AMPERSAND)).getBytes(), algorithm);
    Mac mac = Mac.getInstance(secretKeySpec.getAlgorithm());
    mac.init(secretKeySpec);

    StringBuilder signatureBase = new StringBuilder(OAuthUtil.percentEncode(method));
    signatureBase.append(OAuthUtil.AMPERSAND);

    signatureBase.append(OAuthUtil.percentEncode(url));
    signatureBase.append(OAuthUtil.AMPERSAND);

    int count = 0;
    for (String key : parameters.keySet()) {
        count++;
        signatureBase.append(OAuthUtil.percentEncode(OAuthUtil.percentEncode(key)));
        signatureBase.append(URLEncoder.encode(OAuthUtil.EQUAL, OAuthUtil.ENCODING));
        signatureBase.append(OAuthUtil.percentEncode(OAuthUtil.percentEncode(parameters.get(key))));

        if (count < parameters.size()) {
            signatureBase.append(URLEncoder.encode(OAuthUtil.AMPERSAND, OAuthUtil.ENCODING));
        }
    }

    if (log.isDebugEnabled()) {
        log.debug(signatureBase.toString());
    }

    byte[] bytes = mac.doFinal(signatureBase.toString().getBytes());
    byte[] encodedMacBytes = Base64.encodeBase64(bytes);

    return new String(encodedMacBytes);
}

From source file:hudson.plugins.sauce_ondemand.PluginImpl.java

/**
 * Creates a HMAC token which is used as part of the Javascript inclusion that embeds the Sauce results
 *
 * @param username  the Sauce user id/*from  w ww .ja v a 2s  .co  m*/
 * @param accessKey the Sauce access key
 * @param jobId     the Sauce job id
 * @return the HMAC token
 * @throws java.security.NoSuchAlgorithmException
 *
 * @throws java.security.InvalidKeyException
 *
 * @throws java.io.UnsupportedEncodingException
 *
 */
public String calcHMAC(String username, String accessKey, String jobId)
        throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
    Calendar calendar = Calendar.getInstance();

    SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    String key = username + ":" + accessKey + ":" + format.format(calendar.getTime());
    byte[] keyBytes = key.getBytes();
    SecretKeySpec sks = new SecretKeySpec(keyBytes, HMAC_KEY);
    Mac mac = Mac.getInstance(sks.getAlgorithm());
    mac.init(sks);
    byte[] hmacBytes = mac.doFinal(jobId.getBytes());
    byte[] hexBytes = new Hex().encode(hmacBytes);
    return new String(hexBytes, "ISO-8859-1");
}

From source file:org.apereo.openlrs.utils.OAuthUtils.java

public static String sign(String secret, Map<String, String> oauthParameters, String algorithm, String method,
        String url) {//from  w w w .  jav a  2s  . c om

    StringBuilder signatureBase = new StringBuilder(OAuthUtils.percentEncode(method));
    signatureBase.append("&");
    signatureBase.append(OAuthUtils.percentEncode(url));
    signatureBase.append("&");

    Map<String, String> treeMap = new TreeMap<String, String>(oauthParameters);
    treeMap.remove("oauth_signature");
    treeMap.remove("realm");

    boolean first = true;
    for (Map.Entry<String, String> entry : treeMap.entrySet()) {
        if (!first)
            signatureBase.append(OAuthUtils.percentEncode("&"));
        else
            first = false;

        signatureBase.append(OAuthUtils.percentEncode(entry.getKey() + "=" + entry.getValue()));
    }

    Mac mac = null;
    try {
        SecretKeySpec secretKeySpec = new SecretKeySpec((OAuthUtils.percentEncode(secret) + "&").getBytes(),
                algorithm);

        mac = Mac.getInstance(secretKeySpec.getAlgorithm());
        mac.init(secretKeySpec);

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    if (log.isDebugEnabled()) {
        log.debug("signatureBaseString: " + signatureBase.toString());
    }

    byte[] bytes = mac.doFinal(signatureBase.toString().getBytes());
    byte[] encodedMacBytes = Base64.encodeBase64(bytes);

    return new String(encodedMacBytes);
}

From source file:org.jgrades.security.utils.KeyStoreContentExtractorTest.java

@Test
public void shouldExtractPrivateKeyForEncryption() throws Exception {
    // when//  w w  w  .  j a v a 2  s. com
    SecretKeySpec secretKeySpec = extractor.getPrivateKeyForEncryptionAndDecryption();

    // then
    assertThat(secretKeySpec).isNotNull();
    assertThat(secretKeySpec.getAlgorithm()).isEqualTo("AES");
    assertThat(secretKeySpec.getEncoded()).isEqualTo(FileUtils.readFileToByteArray(cryptoPrivateKey));
}