示例#1
0
  public String signRequest(URL url, String method, byte[] body, String contentType)
      throws Exception {
    StringBuilder sb = new StringBuilder();

    sb.append(String.format("%s %s", method, url.getPath()));
    if (url.getQuery() != null) {
      sb.append(String.format("?%s", url.getQuery()));
    }

    sb.append(String.format("\nHost: %s", url.getHost()));
    if (url.getPort() > 0) {
      sb.append(String.format(":%d", url.getPort()));
    }

    if (contentType != null) {
      sb.append(String.format("\nContent-Type: %s", contentType));
    }

    // body
    sb.append("\n\n");
    if (incBody(body, contentType)) {
      sb.append(new String(body));
    }

    byte[] sum = HMac.HmacSHA1Encrypt(sb.toString(), this.secretKey);
    String sign = UrlSafeBase64.encodeToString(sum);
    return this.accessKey + ":" + sign;
  }
示例#2
0
 public String sign(String data) throws Exception {
   byte[] sum = HMac.HmacSHA1Encrypt(data, this.secretKey);
   String sign = UrlSafeBase64.encodeToString(sum);
   return this.accessKey + ":" + sign;
 }