Java中的Base64和HMAC SHA-256编码/解码

时间:2017-09-18 18:55:42

标签: java base64 sha256 hmac

我正在努力获得GDAX余额,但我认为我无法在Java中正确处理编码/解码。 API参考位于:https://docs.gdax.com/#authentication,我尝试执行的部分位于“帐户” - >列出帐户(只需从上面的链接向下滚动一下)即可获得余额。

这是我的代码。尽管我在谷歌和这里进行了多少搜索,但我似乎无法让它发挥作用。我不断从服务器收到400错误,这意味着"错误的请求 - 无效的请求格式"。

    // Decode the secret key
    byte[] decodedSecret;
    try
    {
        decodedSecret = Base64.decode(SECRET_KEY);
    }
    catch (Base64DecodingException ex)
    {
        System.out.println("Failed to decode secret key.");
        return null;
    }

    // Make the header parameters
    long timestamp = (new GregorianCalendar()).getTimeInMillis() / 1000;
    String preSign = "" + timestamp + "GET" + BASE_URL + "/accounts";

    byte[] encodedhash;
    try
    {
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        try
        {
            sha256_HMAC.init(new SecretKeySpec(decodedSecret, "HmacSHA256"));
        }
        catch (InvalidKeyException ex)
        {
            System.out.println("Failed due to invalid key exception.");
            System.out.println(ex.getMessage());
            return null;
        }
        encodedhash = sha256_HMAC.doFinal(preSign.getBytes());
    }
    catch (NoSuchAlgorithmException ex)
    {
        System.out.println("Failed to make SHA-256 encode because of no such algorithm.");
        return null;
    }

    HashMap<String, String> parameters = new HashMap<>();
    parameters.put("CB-ACCESS-KEY", API_KEY);
    parameters.put("CB-ACCESS-SIGN", Base64.encode(encodedhash));
    parameters.put("CB-ACCESS-TIMESTAMP", "" + timestamp);
    parameters.put("CB-ACCESS-PASSPHRASE", PASSPHRASE);

    // Send the request
    String response = sendGet(BASE_URL + "/accounts", parameters);

这是我在sendGet()中的代码,以防问题出在哪里。我知道它可以在没有参数的情况下工作,但我不知道它是否正确使用了参数,因为我无法使其工作。

    // Set up the connection
    URL url = null;
    try
    {
        url = new URL(urlStr);
    }
    catch (MalformedURLException ex)
    {
        return null;
    }
    HttpURLConnection con;
    try
    {
        con = (HttpURLConnection) url.openConnection();
    }
    catch (IOException ex)
    {
        System.out.println("Returning null because of failure to open connection.");
        return null;
    }
    try
    {
        con.setRequestMethod("GET");
    }
    catch (ProtocolException ex) {}
    if (parameters != null) // if there are parameters to add to the connection
    {
        for (Map.Entry<String, String> pair : parameters.entrySet()) // for each pair in parameters
        {
            try
            {
                con.addRequestProperty(pair.getKey(), pair.getValue());
            }
            catch (Exception ex)
            {
                System.out.println("Failed to add " + pair.getKey() + ".");
            }
        }
    }

    // Get the response
    int responseCode;
    try
    {
        responseCode = con.getResponseCode();

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        StringBuilder responseBldr = new StringBuilder();
        String line;
        while ((line = in.readLine()) != null) // while we have read another line
        {
            responseBldr.append(line);
        }
        in.close();
        return responseBldr.toString();
    }
    catch (IOException ex)
    {
        System.out.println("Returning null from network IOException.");
        System.out.println(ex.getMessage());
        return null;
    }

1 个答案:

答案 0 :(得分:0)

我在本地工作了。您似乎对签名进行了双重编码。

创建签名的步骤如下:

  1. 创建构成签名基础的初始字符串
  2. 解密你的秘密以获得KeySpec(new SecretKeySpec())
  3. 使用keyspec(sha256Hmac.init())
  4. 初始化您的HMAC
  5. 使用密码对请求签名进行编码(sha256Hmac.doFinal())
  6. base64编码步骤4的结果。
  7. 您在上述代码段中唯一的错误就是在标题中再次对其进行base64编码。

    HTH