0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ubuntu: jose を使う

Last updated at Posted at 2025-03-04

Ubuntu 24.10 のパッケージに python の jose がないので、仮想環境で pip でインストールします。

仮想環境の使い方はこちら
Ubuntu で Python の仮想環境を使う

仮想環境をアクティベイト

$ source myenv/bin/activate
(myenv) uchida@shimizu: ~$

jose のインストール

pip install jose

./myenv/lib/python3.12/site-packages にインストールされました。

jose のテスト

(myenv) uchida@shimizu: ~$ python
Python 3.12.7 (main, Feb  4 2025, 14:46:03) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from jose import jws
>>> signed = jws.sign({'aa': 'bb'}, 'secret', algorithm='HS256')
>>> print(signed)
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhYSI6ImJiIn0.lEijQJvrHVHycblxAUTYTFexPExaoXEbQo1VxzZcurM
>>> jws.verify(signed, 'secret', algorithms=['HS256'])
b'{"aa":"bb"}'
>>>

テストスクリプト

jose_test01.py
from jose import jws
#
signed = jws.sign({'aa': 'bb'}, 'secret', algorithm='HS256')
print(signed)
vv = jws.verify(signed, 'secret', algorithms=['HS256'])
print(vv)

実行結果

$ python jose_test01.py 
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhYSI6ImJiIn0.lEijQJvrHVHycblxAUTYTFexPExaoXEbQo1VxzZcurM
b'{"aa":"bb"}'

確認したバージョン

$ python
Python 3.12.7 (main, Feb  4 2025, 14:46:03) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import jose
>>> jose.__version__
'3.4.0'
>>> 

参考ページ

JSON Web Signature

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?