Example #1
0
File: Mac.java Project: dyglcc/mmwd
  /**
   * makes a upload token.
   *
   * @param data
   * @return
   * @throws AuthException
   */
  public String signWithData(byte[] data) throws AuthException {
    byte[] accessKey = this.accessKey.getBytes();
    byte[] secretKey = this.secretKey.getBytes();

    try {
      byte[] policyBase64 = EncodeUtils.urlsafeEncodeBytes(data);

      javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1");
      SecretKeySpec keySpec = new SecretKeySpec(secretKey, "HmacSHA1");
      mac.init(keySpec);

      byte[] digest = mac.doFinal(policyBase64);
      byte[] digestBase64 = EncodeUtils.urlsafeEncodeBytes(digest);
      byte[] token = new byte[accessKey.length + 30 + policyBase64.length];

      System.arraycopy(accessKey, 0, token, 0, accessKey.length);
      token[accessKey.length] = ':';
      System.arraycopy(digestBase64, 0, token, accessKey.length + 1, digestBase64.length);
      token[accessKey.length + 29] = ':';
      System.arraycopy(policyBase64, 0, token, accessKey.length + 30, policyBase64.length);

      return new String(token);
    } catch (Exception e) {
      throw new AuthException("Fail to sign with data!", e);
    }
  }
Example #2
0
File: Mac.java Project: dyglcc/mmwd
  /*
   * makes an access token.
   */
  public String signRequest(HttpPost post) throws AuthException {
    URI uri = post.getURI();
    String path = uri.getRawPath();
    String query = uri.getRawQuery();
    HttpEntity entity = post.getEntity();

    byte[] secretKey = this.secretKey.getBytes();
    javax.crypto.Mac mac = null;
    try {
      mac = javax.crypto.Mac.getInstance("HmacSHA1");
    } catch (NoSuchAlgorithmException e) {
      throw new AuthException("No algorithm called HmacSHA1!", e);
    }

    SecretKeySpec keySpec = new SecretKeySpec(secretKey, "HmacSHA1");
    try {
      mac.init(keySpec);
      mac.update(path.getBytes());
    } catch (InvalidKeyException e) {
      throw new AuthException("You've passed an invalid secret key!", e);
    } catch (IllegalStateException e) {
      throw new AuthException(e);
    }

    if (query != null && query.length() != 0) {
      mac.update((byte) ('?'));
      mac.update(query.getBytes());
    }
    mac.update((byte) '\n');
    if (entity != null) {
      org.apache.http.Header ct = entity.getContentType();
      if (ct != null && ct.getValue() == "application/x-www-form-urlencoded") {
        ByteArrayOutputStream w = new ByteArrayOutputStream();
        try {
          entity.writeTo(w);
        } catch (IOException e) {
          throw new AuthException(e);
        }
        mac.update(w.toByteArray());
      }
    }

    byte[] digest = mac.doFinal();
    byte[] digestBase64 = EncodeUtils.urlsafeEncodeBytes(digest);

    StringBuffer b = new StringBuffer();
    b.append(this.accessKey);
    b.append(':');
    b.append(new String(digestBase64));

    return b.toString();
  }
Example #3
0
File: Mac.java Project: dyglcc/mmwd
  /**
   * makes a download token.
   *
   * @param data
   * @return
   * @throws AuthException
   */
  public String sign(byte[] data) throws AuthException {
    System.out.println("data : " + new String(data));
    javax.crypto.Mac mac = null;
    try {
      mac = javax.crypto.Mac.getInstance("HmacSHA1");
      byte[] secretKey = this.secretKey.getBytes();
      SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, "HmacSHA1");
      mac.init(secretKeySpec);
    } catch (InvalidKeyException e) {
      throw new AuthException("invalid key!", e);
    } catch (NoSuchAlgorithmException e) {
      throw new AuthException("no algorithm called HmacSHA1!", e);
    }

    String encodedSign = EncodeUtils.urlsafeEncodeString(mac.doFinal(data));
    return this.accessKey + ":" + encodedSign;
  }
Example #4
0
 /** 基于Base62编码的SecureRandom随机生成bytes. */
 public static String randomBase62(int length) {
   byte[] randomBytes = new byte[length];
   random.nextBytes(randomBytes);
   return EncodeUtils.encodeBase62(randomBytes);
 }