Skip to content

Instantly share code, notes, and snippets.

@GuGaobai1994
Created April 13, 2018 00:34
Show Gist options
  • Save GuGaobai1994/41d955e38c0a7236abe41246b8f64dc8 to your computer and use it in GitHub Desktop.
Save GuGaobai1994/41d955e38c0a7236abe41246b8f64dc8 to your computer and use it in GitHub Desktop.
qiniu_access_token
package com.example.AccessToken;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.Base64;
import java.util.Formatter;
//https://developer.qiniu.com/kodo/manual/1201/access-token
public class AccessTokenApplication {
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
public static String calculateRFC2104HMAC(String data, String key)
throws NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
return Base64.getUrlEncoder().encodeToString((mac.doFinal(data.getBytes())));
}
public static void main(String[] args) throws Exception{
SpringApplication.run(AccessTokenApplication.class, args);
String asB64 = calculateRFC2104HMAC("/move/bmV3ZG9jczpmaW5kX21hbi50eHQ=/bmV3ZG9jczpmaW5kLm1hbi50eHQ=\n", "MY_SECRET_KEY");
System.out.println(asB64); // 输出为: FXsYh0wKHYPEsIAgdPD9OfjkeEM
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment