开发者社区> 问答> 正文

OSSPostObject的Policy签名(Python和C)示例

研究了一下OSS的Policy如何签名
以下是Python的Sample代码



#coding=utf8
import md5
import hashlib
import base64
import hmac


def convert_base64(input):
    return base64.b64encode(input)


def get_sign_policy(key, policy):
    return base64.b64encode(hmac.new(key, policy, hashlib.sha1).digest())


policy='''
{"expiration":"2015-01-27T10:56:19Z","conditions":[["content-length-range", 0, 1048576]]}
'''
policy="{\"expiration\":\"2015-01-27T10:56:19Z\",\"conditions\":[[\"content-length-range\", 0, 1048576]]}"


print "policy: ", policy
print "policy length: ", len(policy)


secret_access_key = "test"
print "secret_access_key: ", secret_access_key


base64policy = convert_base64(policy)
print "base64policy: ", base64policy
signature = get_sign_policy(secret_access_key, base64policy)
print "signature_of_policy: ", signature








运行的结果:

policy:  {"expiration":"2015-01-27T10:56:19Z","conditions":[["content-length-range", 0, 1048576]]}
policy length:  89
secret_access_key:  test
base64policy:  eyJleHBpcmF0aW9uIjoiMjAxNS0wMS0yN1QxMDo1NjoxOVoiLCJjb25kaXRpb25zIjpbWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsIDAsIDEwNDg1NzZdXX0=
signature_of_policy:  XRiWSZLSQPQGG+f3GPYLYMeFjr0=



展开
收起
wood23 2015-01-27 19:47:58 10523 0
3 条回答
写回答
取消 提交回答
  • ReOSSPostObject的Policy如何签名?
    最近我也在签名方面遇到了问题,看了楼主的帖子后很快便解决了,太及时了,非常感谢。
    2015-01-27 19:54:31
    赞同 展开评论 打赏
  • ReOSSPostObject的Policy如何签名?
    顶 正需要这样的东西.
    2015-01-27 19:52:16
    赞同 展开评论 打赏
  • ReOSSPostObject的Policy如何签名?
    C#的Sample代码



                string key = "test";
                string stringToSign =
                    "{\"expiration\":\"2015-01-27T10:56:19Z\",\"conditions\":[[\"content-length-range\", 0, 1048576]]}";
                string base64Policy = Convert.ToBase64String(Encoding.UTF8.GetBytes(stringToSign));
                Console.WriteLine("base64policy: {0}", base64Policy);
                string signature = CryptoUtilFactory.CryptoInstance.HMACSign(base64Policy, key, SigningAlgorithm.HmacSHA1);
                Console.WriteLine(signature);
    2015-01-27 19:49:12
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载