zoukankan      html  css  js  c++  java
  • PYJWT

    Welcome to PyJWT

    PyJWT是一个Python库,允许您对JSON Web Tokens(JWT)进行编码和解码。JWT是一个开放的行业标准(RFC 7519),用于在双方之间安全地声明索赔。

    安装

    pip install pyjwt
    

    如需了解更多,请参阅安装了解更多信息

    用法示例

    >>> import jwt
    
    >>> encoded_jwt = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')
    >>> encoded_jwt
    'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.4twFt5NiznN84AWoo1d7KO1T_yoc0Z6XOpOVswacPZg'
    
    >>> jwt.decode(encoded_jwt, 'secret', algorithms=['HS256'])
    {'some': 'payload'}
    

    加密相依性(可选)

    如果您计划使用某些数字签名算法(如RSA或ECDSA)进行编码或解码令牌,则需要安装 加密库。

    pip install cryptography
    

    传统的依赖关系

    • 一些环境,比如最常用的Google App Engine,不允许安装需要编译C扩展名的Python包,因此无法安装加密。如果你能安装加密,你应该忽略这个部分。
    • 如果你正在部署一个应用程序到这些环境之一,你需要使用数字签名算法的传统实现。 
    pip install pycrypto ecdsa
    

    一旦你已经安装了 pycrypto 和 ecdcsa,你可以在PyJWT中使用jwt.register_algorithm()。下面的示例代码将展示怎样配置PyJWT,去使用具有SHA256和EC与SHA256签名的RSA的传统实现。 

    import jwt
    
    from jwt.contrib.algorithms.pycrypto import RSAAlgorithm
    
    from jwt.contrib.algorithms.py_ecdsa import ECAlgorithm
    
    jwt.register_algorithm('RS256', RSAAlgorithm(RSAAlgorithm.SHA256))
    
    jwt.register_algorithm('ES256', ECAlgorithm(ECAlgorithm.SHA256))

      

      

     

  • 相关阅读:
    几种常用的曲线
    0188. Best Time to Buy and Sell Stock IV (H)
    0074. Search a 2D Matrix (M)
    0189. Rotate Array (E)
    0148. Sort List (M)
    0859. Buddy Strings (E)
    0316. Remove Duplicate Letters (M)
    0452. Minimum Number of Arrows to Burst Balloons (M)
    0449. Serialize and Deserialize BST (M)
    0704. Binary Search (E)
  • 原文地址:https://www.cnblogs.com/wangyuxing/p/9560692.html
Copyright © 2011-2022 走看看