Example usage for org.apache.commons.codec.digest HmacUtils hmacSha512Hex

List of usage examples for org.apache.commons.codec.digest HmacUtils hmacSha512Hex

Introduction

In this page you can find the example usage for org.apache.commons.codec.digest HmacUtils hmacSha512Hex.

Prototype

public static String hmacSha512Hex(final String key, final String valueToDigest) 

Source Link

Document

Returns a HmacSHA512 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.

Usage

From source file:com.searchcode.app.service.ApiService.java

/**
 * Validates a request made to the API against the public key supplied, the hmac supplied and the
 * query string itself.//from   w w w  . ja v a  2 s  .c  o  m
 * http://stackoverflow.com/questions/11830338/web-api-creating-api-keys
 * http://stackoverflow.com/questions/6312544/hmac-sha1-how-to-do-it-properly-in-java
 * http://stackoverflow.com/questions/3208160/how-to-generate-an-hmac-in-java-equivalent-to-a-python-example?rq=1
 *
 */
public boolean validateRequest(String publicKey, String hmac, String query, HmacType hmacType) {
    ApiResult apiResult = this.api.getApiByPublicKey(publicKey);

    if (apiResult == null) {
        return false;
    }

    String myHmac;

    switch (hmacType) {
    case SHA512:
        myHmac = HmacUtils.hmacSha512Hex(apiResult.getPrivateKey(), query);
        break;
    default:
        myHmac = HmacUtils.hmacSha1Hex(apiResult.getPrivateKey(), query);
        break;
    }
    return myHmac.equals(hmac);
}

From source file:com.mtnfog.idyl.e3.sdk.interceptors.HmacSha512v1SigningInterceptor.java

@Override
public Response intercept(Chain chain) throws IOException {

    Builder builder = chain.request().newBuilder();

    if (method == AuthenticationMethod.PLAIN) {

        builder.header("Authorization", apiKey);

    } else if (method == AuthenticationMethod.HMACSHA512v1) {

        StringBuilder sb = new StringBuilder();
        sb.append(chain.request().method().toUpperCase() + "\n");
        sb.append(chain.request().uri().getHost() + ":" + chain.request().uri().getPort() + "\n");
        sb.append(requestDate);/* w  w w  .  ja v  a2 s . c  o  m*/

        final String signature = HmacUtils.hmacSha512Hex(apiKey, sb.toString());

        builder.header("Authorization", signature);

    }

    return chain.proceed(builder.build());

}