Pyjwt ,python jwt ,jwt

时间:2024-10-09 13:36:09
pip install Pyjwt

不错的jwt 文章:

https://www.cnblogs.com/wayneiscoming/p/7513487.html

Sampleimport jwt
import time
secret="b'\x7d\xef\x87\xd5\xf8\xbb\xff\xfc\x80\x91\x06\x91\xfd\xfc\xed\x69'"
print(secret.encode('utf-8'))
expire_time = )

encode_str=jwt.encode(
    {
        ,
        "exp":expire_time
    }
    ,
    secret,#秘钥
    algorithm='HS256'
)
str_encode=str(encode_str,encoding='ascii')
print(str_encode)
info=jwt.decode(encode_str,secret,algorithms='HS256')
print(info)
JSON WEB TOKENS

http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html

json web tokens

地址:https://www.npmjs.com/package/jwt-simple

jwt-simple
JWT(JSON Web Token) encode and decode module for node.js.

Install
$ npm install jwt-simple
Usage
var jwt = require('jwt-simple');
var payload = { foo: 'bar' };
var secret = 'xxx';

// HS256 secrets are typically 128-bit random strings, for example hex-encoded:
// var secret = Buffer.from('fe1a1915a379f3be5394b64d14794932', 'hex')

// encode
var token = jwt.encode(payload, secret);

// decode
var decoded = jwt.decode(token, secret);
console.log(decoded); //=> { foo: 'bar' }
decode params
/*
 * jwt.decode(token, key, noVerify, algorithm)
 */

// decode, by default the signature of the token is verified
var decoded = jwt.decode(token, secret);
console.log(decoded); //=> { foo: 'bar' }

// decode without verify the signature of the token,
// be sure to KNOW WHAT ARE YOU DOING because not verify the signature
// means you can't be sure that someone hasn't modified the token payload
var decoded = jwt.decode(token, secret, true);
console.log(decoded); //=> { foo: 'bar' }

// decode with a specific algorithm (not using the algorithm described in the token payload)
var decoded = jwt.decode(token, secret, false, 'HS256');
console.log(decoded); //=> { foo: 'bar' }
Algorithms
By default the algorithm to encode is HS256.

The supported algorithms for encoding and decoding are HS256, HS384, HS512 and RS256.

// encode using HS512
jwt.encode(payload, secret, 'HS512')

jwt-simple

import python_jwt as jwt, Crypto.PublicKey.RSA as RSA, datetime
key = RSA.generate()
priv_pem = key.exportKey()
pub_pem = key.publickey().exportKey()
payload = {  };
priv_key = RSA.importKey(priv_pem)
pub_key = RSA.importKey(pub_pem)
token = jwt.generate_jwt(payload, priv_key, ))
header, claims = jwt.verify_jwt(token, pub_key, ['RS256'])
for k in payload: assert claims[k] == payload[k]

更多:https://pypi.org/project/python_jwt/2.0.1/

另一种