重力表单签名-从PHP到java

重力表单签名-从PHP到java,java,php,signature,gravity-forms-plugin,Java,Php,Signature,Gravity Forms Plugin,来自外部应用程序的所有请求都通过检查过期签名进行身份验证。这类似于Amazon用于保护对S3存储API的访问的方法。经过身份验证后,将使用标准WordPress和Gravity表单基于角色的授权来确保允许满足API请求 每个请求至少必须包括以下3个查询参数: api_key - The public API key defined on the settings page e.g. "1234" expires - Expiration date for the request expresse

来自外部应用程序的所有请求都通过检查过期签名进行身份验证。这类似于Amazon用于保护对S3存储API的访问的方法。经过身份验证后,将使用标准WordPress和Gravity表单基于角色的授权来确保允许满足API请求

每个请求至少必须包括以下3个查询参数:

api_key - The public API key defined on the settings page e.g. "1234"
expires - Expiration date for the request expressed as a UNIX timestamp in seconds e.g. 1369749344
signature - A URL-encoded, base64 HMAC-SHA1 hash of a colon separated string following this structure:
{api_key}:{http method}:{route}:{expires}
e.g. 1234:GET:forms/1/entries:1369749344
The signature for this request using the private key of "abcd" is uJEnk0EoQ4d3iinjFMBrBzZfH9w%3D
用于在PHP中生成签名但在JAVA中需要的示例代码

  <?php
function calculate_signature($string, $private_key) {
    $hash = hash_hmac("sha1", $string, $private_key, true);
    $sig = rawurlencode(base64_encode($hash));
    return $sig;
}

$api_key = "1234";
$private_key = "abcd";
$method  = "GET";
$route    = "forms/1/entries";
$expires = strtotime("+60 mins");
$string_to_sign = sprintf("%s:%s:%s:%s", $api_key, $method, $route, $expires);
$sig = calculate_signature($string_to_sign, $private_key);
var_dump($sig);
?>

请帮帮我

以下是一些Java解决方案

public class GravityManager {

public static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException {

    SecretKey secretKey = null;

    byte[] keyBytes = keyString.getBytes();
    secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");

    Mac mac = Mac.getInstance("HmacSHA1");

    mac.init(secretKey);

    byte[] text = baseString.getBytes();

    return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
}
}

public class GravityManager {

public static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException {

    SecretKey secretKey = null;

    byte[] keyBytes = keyString.getBytes();
    secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");

    Mac mac = Mac.getInstance("HmacSHA1");

    mac.init(secretKey);

    byte[] text = baseString.getBytes();

    return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
}