Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java版本的HmacSHA256实现? #1

Open
ArvinC opened this issue Aug 1, 2017 · 1 comment
Open

Java版本的HmacSHA256实现? #1

ArvinC opened this issue Aug 1, 2017 · 1 comment

Comments

@ArvinC
Copy link

ArvinC commented Aug 1, 2017

能否给出HmacSHA256的实现,使用JDK自带的HmacSHA256实现,无法与文档中对应。

JSONObject jsonObject = new JSONObject();
    jsonObject.put("Bucket", "doc");
    jsonObject.put("Object", "anne.jpg");
    jsonObject.put("Expires", 1451491200);
    String encodedPutPolicy = new BASE64Encoder().encode(jsonObject.toJSONString().getBytes());
    System.out.println("encodedPutPolicy:" + encodedPutPolicy);
    SecretKeySpec signingKey = new SecretKeySpec("ae0208eea57c4bc9bc5754368c06a542".getBytes(), "HmacSHA256");
    Mac mac = Mac.getInstance(signingKey.getAlgorithm());
    mac.init(signingKey);
    byte[] digest = mac.doFinal(encodedPutPolicy.getBytes("UTF-8"));
    String sign = new String(new Hex().encode(digest));
    String encodedSign = new BASE64Encoder().encode(sign.getBytes());
    System.out.println("encodedSign:" + encodedSign);
@zhenghuabin
Copy link

HmacSHA256实现是统一标准的,另外代码也是开放的,你可以查看源码。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants