Pyjwt ,python jwt ,jwt

pip install Pyjwt

 

不錯的jwt 文章:html

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

 


Sample
import 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 = int(time.time()+3600) encode_str=jwt.encode( { "id":123123123, "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-simplepython

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(2048)
priv_pem = key.exportKey()
pub_pem = key.publickey().exportKey()
payload = { 'foo': 'bar', 'wup': 90 };
priv_key = RSA.importKey(priv_pem)
pub_key = RSA.importKey(pub_pem)
token = jwt.generate_jwt(payload, priv_key, 'RS256', datetime.timedelta(minutes=5))
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/
另外一種
相關文章
相關標籤/搜索