Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaAmazon移动分析签名V4。简单例子_Java_Android_Amazon Web Services - Fatal编程技术网

JavaAmazon移动分析签名V4。简单例子

JavaAmazon移动分析签名V4。简单例子,java,android,amazon-web-services,Java,Android,Amazon Web Services,有人能举一个amazon签名v4的java示例吗 AMA签名示例: private static byte[] HmacSHA256(String data, byte[] key) throws Exception { String algorithm="HmacSHA256"; Mac mac = Mac.getInstance(algorithm); mac.init(new SecretKeySpec(key, algorithm)); return m

有人能举一个amazon签名v4的java示例吗 AMA签名示例:

 private static byte[] HmacSHA256(String data, byte[] key) throws Exception {
    String algorithm="HmacSHA256";
    Mac mac = Mac.getInstance(algorithm);
    mac.init(new SecretKeySpec(key, algorithm));
    return mac.doFinal(data.getBytes("UTF8"));
}

private static byte[] getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception {
    byte[] kSecret = ("AWS4" + key).getBytes("UTF8");
    byte[] kDate = HmacSHA256(dateStamp, kSecret);
    byte[] kRegion = HmacSHA256(regionName, kDate);
    byte[] kService = HmacSHA256(serviceName, kRegion);
    byte[] kSigning = HmacSHA256("aws4_request", kService);
    return kSigning;
}

private String SHA256HEX(String content) throws Exception
{
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    byte[] hash = digest.digest(content.getBytes(Util.UTF_8));
    return Util.encodeToBase16(hash);
}

public String getSignature(String url,long date,String payLoad) throws Exception
{
    String signature = "";

    String amzDate = Util.convertDateTo(ISO8601_HUMAN_READABLE,date);
    String amzDateShort = Util.convertDateTo(ISO8601_HUMAN_READABLE,date).replace(":","").replace("-","").split("\\.")[0] + "Z";
    String dateStamp = Util.convertDateTo(SHORT_DATE,date);

    String separator = File.separator;
    separator += separator;
    String host = url.split(separator)[1];

    String canonicalHeaders = "accept:application/hal+json\nhost:" + host + "\nx-amz-date:" + amzDate + "\n";

    String signedHeaders = "accept;host;x-amz-date";

    String payLoadHash = SHA256HEX(payLoad);

    String canonicalRequest = "POST\n" + AMASender.CANONICAL_URI + "\n\n" + canonicalHeaders +"\n" + signedHeaders + "\n"+ payLoadHash;

    String algorithm = "AWS4-HMAC-SHA256";
    String credentialScope = dateStamp + "/" + AMASender.REGION + "/" + AMASender.SERVICE_NAME + "/aws4_request";
    String stringToSign = algorithm + "\n" +  amzDateShort + "\n" +  credentialScope + "\n" +  SHA256HEX(canonicalRequest);

    byte[] signingKey = getSignatureKey(mSecretKey,dateStamp,AMASender.REGION,AMASender.SERVICE_NAME);

    signature = Util.encodeToBase16(HmacSHA256(stringToSign,signingKey)) ;

    return signature;
} 

public class Util {

private static String TAG = Util.class.getSimpleName();

private static int LOG_PRIORITY = 3;

public final static String UTF_8 = "UTF-8";

public static int timeout = 10000;

public static DateFormat ISO8601_HUMAN_READABLE = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") ;
public static DateFormat ISO8601 = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'") ;
public static DateFormat SHORT_DATE_SEPARATOR = new SimpleDateFormat("yyyy-MM-dd") ;
public static DateFormat SHORT_DATE = new SimpleDateFormat("yyyyMMdd") ;

public static String getUTCDate(DateFormat dateFormat)
{
    return convertDateTo(dateFormat,System.currentTimeMillis());
}

/**
 * Get the ISO 8601 Format of the current UTC Time
 * @return current time
 */
public static String getUTCDate()
{
  return getUTCDate(ISO8601);
}

public static String getUTCDateLong()
{
    return getUTCDate(ISO8601_HUMAN_READABLE);
}

public static String convertDateTo(DateFormat dateFormat,long date)
{
    TimeZone tz = TimeZone.getTimeZone("UTC");
    dateFormat.setTimeZone(tz);
    return dateFormat.format(new Date(date));
}

public static String encodeToBase16(byte[] byteArray) {

    final char[] HEX = new char[]{
            '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    StringBuffer hexBuffer = new StringBuffer(byteArray.length * 2);
    for (int i = 0; i < byteArray.length; i++)
        for (int j = 1; j >= 0; j--)
            hexBuffer.append(HEX[(byteArray[i] >> (j * 4)) & 0xF]);
    return hexBuffer.toString();
}
私有静态字节[]HmacSHA256(字符串数据,字节[]键)引发异常{
字符串算法=“HmacSHA256”;
Mac Mac=Mac.getInstance(算法);
init(新的SecretKeySpec(key,algorithm));
返回mac.doFinal(data.getBytes(“UTF8”);
}
私有静态字节[]getSignatureKey(字符串键、字符串日期戳、字符串regionName、字符串serviceName)引发异常{
字节[]kSecret=(“AWS4”+键).getBytes(“UTF8”);
字节[]kDate=HmacSHA256(日期戳,kSecret);
字节[]kRegion=HmacSHA256(区域名称,kDate);
字节[]kService=HmacSHA256(serviceName,kRegion);
字节[]kSigning=HmacSHA256(“aws4_请求”,kService);
返回kSigning;
}
私有字符串SHA256HEX(字符串内容)引发异常
{
MessageDigest=MessageDigest.getInstance(“SHA-256”);
byte[]hash=digest.digest(content.getBytes(Util.UTF_8));
返回Util.encodeToBase16(散列);
}
公共字符串getSignature(字符串url、长日期、字符串负载)引发异常
{
字符串签名=”;
字符串amzDate=Util.convertDateTo(ISO8601人类可读,日期);
字符串amzDateShort=Util.convertDateTo(ISO8601可读,日期)。替换(“:”,“”)。替换(“-”,“”)。拆分(“\\”)[0]+“Z”;
字符串dateStamp=Util.convertDateTo(短日期,日期);
字符串分隔符=File.separator;
分离器+=分离器;
String host=url.split(分隔符)[1];
String canonicalHeaders=“接受:应用程序/hal+json\n主机:“+host+”\nx amz日期:“+amzDate+”\n”;
String signedHeaders=“接受;主机;x-amz-date”;
字符串payLoadHash=SHA256HEX(有效载荷);
字符串canonicalRequest=“POST\n”+AMASender.CANONICAL\u URI+”\n\n“+canonicalHeaders+”\n“+signedHeaders+”\n“+payLoadHash;
字符串算法=“AWS4-HMAC-SHA256”;
String credentialScope=dateStamp+“/”+AMASender.REGION+“/”+AMASender.SERVICE\u NAME+“/aws4\u request”;
字符串stringToSign=algorithm+“\n”+amzDateShort+“\n”+credentialScope+“\n”+SHA256HEX(规范请求);
字节[]signingKey=getSignatureKey(mSecretKey、日期戳、AMASender.REGION、AMASender.SERVICE\u名称);
签名=Util.encodeToBase16(HmacSHA256(stringToSign,signingKey));
返回签名;
} 
公共类Util{
私有静态字符串标记=Util.class.getSimpleName();
私有静态int LOG_优先级=3;
公共最终静态字符串UTF_8=“UTF-8”;
公共静态int超时=10000;
公共静态日期格式ISO8601_HUMAN_READABLE=新的简化格式(“yyyy-MM-dd'T'HH:MM:ss.SSS'Z'”;
公共静态日期格式ISO8601=新的简化格式(“yyyyMMdd'T'HHmmss'Z'”;
public static DateFormat SHORT_DATE_SEPARATOR=new SimpleDateFormat(“yyyy-MM-dd”);
public static DateFormat SHORT_DATE=new SimpleDateFormat(“yyyyMMdd”);
公共静态字符串getUTCDate(DateFormat DateFormat)
{
返回convertDateTo(日期格式,System.currentTimeMillis());
}
/**
*获取当前UTC时间的ISO 8601格式
*@返回当前时间
*/
公共静态字符串getUTCDate()
{
返回getUTCDate(ISO8601);
}
公共静态字符串getUTCDateLong()
{
返回getUTCDate(ISO8601可读);
}
公共静态字符串convertDateTo(日期格式DateFormat,长日期)
{
时区tz=时区。getTimeZone(“UTC”);
dateFormat.setTimeZone(tz);
返回日期格式。格式(新日期(日期));
}
公共静态字符串编码器SE16(字节[]byteArray){
最终字符[]十六进制=新字符[]{
'0', '1', '2', '3', '4', '5', '6', '7',
‘8’、‘9’、‘a’、‘b’、‘c’、‘d’、‘e’、‘f’};
StringBuffer hexBuffer=新的StringBuffer(byteArray.length*2);
for(int i=0;i=0;j--)
append(HEX[(byteArray[i]>>(j*4))&0xF]);
返回hexBuffer.toString();
}
}