七牛云存储 | 上传凭证

七牛云存储上凭证生成演示 author(s): Qiniu

HTML

<link rel="stylesheet" href="http://qtestbucket.qiniudn.com/demo/ext/resources/ext-theme-neptune/ext-theme-neptune-all.css">
<script src="http://qtestbucket.qiniudn.com/demo/CryptoJS.js"></script>
<script>
    /**
     * 上传凭证算法实现参考
     * 请注意External Resources项中引用的第三方CryptoJS库
     */
    var genUpToken = function(accessKey, secretKey, putPolicy) {

        //SETP 2
        var put_policy = JSON.stringify(putPolicy);
        console && console.log("put_policy = ", put_policy);

        //SETP 3
        var encoded = base64encode(utf16to8(put_policy));
        console && console.log("encoded = ", encoded);

        //SETP 4
        var hash = CryptoJS.HmacSHA1(encoded, secretKey);
        var encoded_signed = hash.toString(CryptoJS.enc.Base64);
        console && console.log("encoded_signed=", encoded_signed)

        //SETP 5
        var upload_token = accessKey + ":" + safe64(encoded_signed) + ":" + encoded;
        console && console.log("upload_token=", upload_token)
        return upload_token;
    };

    /*******************************************************************************
    *   贡献代码:
    *   1. git clone [email protected]:icattlecoder/jsfiddle
    *   2. push代码到您的github库
    *   2. 测试效果,访问 http://jsfiddle.net/gh/get/extjs/4.2/<Your GitHub Name>/jsfiddle/tree/master/uptoken
    *   3. 提pr
    ********************************************************************************/

</script>

JavaScript

Ext.onReady(function() {

    App = {
        Bucket: "qtestbucket",
        SignUrl: "token.php",
        //qiniu test account
        AK: "iN7NgwM31j4-BZacMjPrOQBs34UG1maYCAQmhdCV",
        SK: "6QTOr2Jg1gcZEWDQXKOGZh5PziC2MCV5KsntT70j"
    }

    /* utf.js - UTF-8 <=> UTF-16 convertion
     *
     * Copyright (C) 1999 Masanao Izumo <[email protected]>
     * Version: 1.0
     * LastModified: Dec 25 1999
     * This library is free. You can redistribute it and/or modify it.
     */
    /*
     * Interfaces:
     * utf8 = utf16to8(utf16);
     * utf16 = utf8to16(utf8);
     */

    function utf16to8(str) {
        var out, i, len, c;
        out = "";
        len = str.length;
        for (i = 0; i < len; i++) {
            c = str.charCodeAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                out += str.charAt(i);
            } else if (c > 0x07FF) {
                out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            } else {
                out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            }
        }
        return out;
    }

    function utf8to16(str) {
        var out, i, len, c;
        var char2, char3;
        out = "";
        len = str.length;
        i = 0;
        while (i < len) {
            c = str.charCodeAt(i++);
            switch (c >> 4) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                    // 0xxxxxxx
                    out += str.charAt(i - 1);
                    break;
                case 12:
                case 13:
                    // 110x xxxx 10xx xxxx
                    char2 = str.charCodeAt(i++);
                   ...