Created
April 13, 2018 00:34
-
-
Save GuGaobai1994/41d955e38c0a7236abe41246b8f64dc8 to your computer and use it in GitHub Desktop.
qiniu_access_token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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