七牛用JAVA生成各种凭证代码

用JAVA生成各种凭证代码

1 上传凭证:

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import com.qiniu.util.StringUtils;
import com.qiniu.util.UrlSafeBase64;

public class TokenUpload {  

    public static void tokenupload() throws NoSuchAlgorithmException, InvalidKeyException{
        String ACCESS_KEY = "AK";
        String SECRET_KEY = "SK";
//      String putpolicy = "{\"scope\":\"javademo\",\"deadline\":1457843866}";
        String putpolicy = "{\"scope\":\"thinkox-shang:sdfsdf.jpg\",\"deadline\":1441940419}";
        String t3 = UrlSafeBase64.encodeToString(putpolicy);
        Mac mac = javax.crypto.Mac.getInstance("HmacSHA1");
        mac.init(new SecretKeySpec(StringUtils.utf8Bytes(SECRET_KEY),"HmacSHA1"));
        String t2 = UrlSafeBase64.encodeToString(mac.doFinal(StringUtils.utf8Bytes(t3)));
        System.out.println(ACCESS_KEY+":"+t2+":"+t3);
    }

    public static void main(String args[]) throws InvalidKeyException, NoSuchAlgorithmException{
        tokenupload();
    }

}

2 下载凭证:

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import com.qiniu.util.StringUtils;
import com.qiniu.util.UrlSafeBase64;

public class TokenDownload {
    public static void tokendownload() throws NoSuchAlgorithmException, InvalidKeyException{

        String ACCESS_KEY = "AK";
        String SECRET_KEY = "SK"; 

        String DownloadUrl  = "http://7xl4ck.com1.z0.glb.clouddn.com/photo1.jpg?e=1637846514";
        Mac mac = javax.crypto.Mac.getInstance("HmacSHA1");
        mac.init(new SecretKeySpec(StringUtils.utf8Bytes(SECRET_KEY),"HmacSHA1"));
        String t2 = UrlSafeBase64.encodeToString(mac.doFinal(StringUtils.utf8Bytes(DownloadUrl)));

        System.out.println(DownloadUrl+"&token="+ACCESS_KEY+":"+t2); 
    }
    public static void main(String args[]) throws InvalidKeyException, NoSuchAlgorithmException{
        tokendownload();

    }

}

3 管理凭证:

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import com.qiniu.util.StringUtils;
import com.qiniu.util.UrlSafeBase64;


public class TokenManage {
    public static void tokenmanage() throws NoSuchAlgorithmException, InvalidKeyException{

        String ACCESS_KEY = "AK";
        String SECRET_KEY = "SK";
    //删除方法
        String signingStr  = "/delete/Y3BwZGVtbzpwaG90by0y\n";
    //fetch方法
    //  String signingStr  = "/fetch/aHR0cHM6Ly9zY29udGVudC5jZG5pbnN0YWdyYW0uY29tL2hwaG90b3MteGFmMS90NTEuMjg4NS0xNS9lMTUvMTEzODAyOTRfMTU5MDk3MDMwNzgyOTc1Nl8xNTQ5OTUzOTE5X24uanBn/to/cGhwZGVtbzpmZXRjaC5qcGc=\n"; 
        Mac mac = javax.crypto.Mac.getInstance("HmacSHA1");
        mac.init(new SecretKeySpec(StringUtils.utf8Bytes(SECRET_KEY),"HmacSHA1"));
        String t2 = UrlSafeBase64.encodeToString(mac.doFinal(StringUtils.utf8Bytes(signingStr)));

        System.out.println(ACCESS_KEY+":"+t2); 
    }
    public static void main(String args[]) throws InvalidKeyException, NoSuchAlgorithmException{
        tokenmanage(); 
    }

}

你可能感兴趣的:(java)