<!DOCTYPE html>
<html>
<head>
<title>七牛云配置token</title>
<meta charset="utf-8">
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!-- 资源引用 -->
<script type="text/javascript" src="js/jquery-1.7.2.min.js" charset="utf-8"></script>
<script type="text/javascript" src="js/CryptoJS.js"></script>
<script type="text/javascript" src="js/tokenTools.js"></script>
<style type="text/css">
input{
width: 350px;margin-bottom: 10px;height: 30px;line-height: 30px;
}
</style>
</head>
<body style="text-align:center">
<div> 请将AK/SK/七牛云空间名称 都填写完整</div>
<input type="text" name="" id="accessKey" placeholder="请输入AccessKey/AK" ><br>
<input type="text" name="" id="secretKey" placeholder="请输入SecretKey/SK"> <br>
<input type="text" name="" id="scope" placeholder="请输入七牛云空间名称"><br>
<button type="button" onclick="createToken()">生成Token</button><br>
<div id="token"></div>
</body>
<script>
/**
* 上传凭证算法实现参考
* 请注意External Resources项中引用的第三方CryptoJS库
*/
function createToken() {
var accessKey = $('#accessKey').val();//AccessKey/AK
var secretKey = $('#secretKey').val();//SecretKey/SK
var scope=$('#scope').val();
//scope:七牛云空间名称
//deadline unix的时间戳 让token到2021年失效 在线时间戳转换:https://tool.lu/timestamp/
var putPolicy = {"scope" :scope,"deadline" : 1640793600};
//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);
$('#token').html(upload_token);
// alert("________________" + upload_token);
}
</script>
</html>
CryptoJS.js: https://blog.csdn.net/weixin_42095170/article/details/103256125
tokenTools.js: https://blog.csdn.net/weixin_42095170/article/details/103256045