Java tutorial
/** * Copyright 2010-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved. * <p> * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * <p> * http://aws.amazon.com/apache2.0 * <p> * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.amazonaws.cognito.sync.demo.client.server; import com.amazonaws.AmazonClientException; import com.amazonaws.Request; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSSessionCredentials; import com.amazonaws.auth.AbstractAWSSigner; import com.amazonaws.util.DateUtils; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.util.Date; import org.apache.commons.codec.binary.Hex; public class Utilities { public static String getTimestamp() { return DateUtils.formatISO8601Date(new Date()); } public static String extractElement(String json, String element) { boolean hasElement = json.contains(element); if (hasElement) { int elementIndex = json.indexOf(element) + element.length() + 1; int startIndex = json.indexOf("\"", elementIndex); int endIndex = json.indexOf("\"", startIndex + 1); return json.substring(startIndex + 1, endIndex); } return null; } public static String getSignature(String dataToSign, String key) { return new Signer().getSignature(dataToSign, key); } private static class Signer extends AbstractAWSSigner { public String getSignature(String dataToSign, String key) { try { byte[] data = dataToSign.getBytes("UTF8"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(key.getBytes("UTF8"), "HmacSHA256")); char[] signature = Hex.encodeHex(mac.doFinal(data)); return new String(signature); } catch (Exception exception) { return null; } } @Override public void sign(Request<?> arg0, AWSCredentials arg1) throws AmazonClientException { } @Override protected void addSessionCredentials(Request<?> arg0, AWSSessionCredentials arg1) { } } }