<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> import json <EOL> from . . util import urlsafe_b64decode , urlsafe_b64encode , native , binary <EOL> ed25519ll = None <EOL> ALG = "<STR_LIT>" <EOL> def get_ed25519ll ( ) : <EOL> """<STR_LIT>""" <EOL> global ed25519ll <EOL> if not ed25519ll : <EOL> try : <EOL> import ed25519ll <EOL> except ( ImportError , OSError ) : <EOL> from . import ed25519py as ed25519ll <EOL> test ( ) <EOL> return ed25519ll <EOL> def sign ( payload , keypair ) : <EOL> """<STR_LIT>""" <EOL> get_ed25519ll ( ) <EOL> header = { <EOL> "<STR_LIT>" : ALG , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : ALG , <EOL> "<STR_LIT>" : native ( urlsafe_b64encode ( keypair . vk ) ) <EOL> } <EOL> } <EOL> encoded_header = urlsafe_b64encode ( binary ( json . dumps ( header , sort_keys = True ) ) ) <EOL> encoded_payload = urlsafe_b64encode ( binary ( json . dumps ( payload , sort_keys = True ) ) ) <EOL> secured_input = b"<STR_LIT:.>" . join ( ( encoded_header , encoded_payload ) ) <EOL> sig_msg = ed25519ll . crypto_sign ( secured_input , keypair . sk ) <EOL> signature = sig_msg [ : ed25519ll . SIGNATUREBYTES ] <EOL> encoded_signature = urlsafe_b64encode ( signature ) <EOL> return { "<STR_LIT>" : <EOL> [ { "<STR_LIT>" : native ( encoded_header ) , <EOL> "<STR_LIT>" : native ( encoded_signature ) } ] , <EOL> "<STR_LIT>" : native ( encoded_payload ) } <EOL> def assertTrue ( condition , message = "<STR_LIT>" ) : <EOL> if not condition : <EOL> raise ValueError ( message ) <EOL> def verify ( jwsjs ) : <EOL> """<STR_LIT>""" <EOL> get_ed25519ll ( ) <EOL> recipients = jwsjs [ "<STR_LIT>" ] <EOL> encoded_payload = binary ( jwsjs [ "<STR_LIT>" ] ) <EOL> headers = [ ] <EOL> for recipient in recipients : <EOL> assertTrue ( len ( recipient ) == <NUM_LIT:2> , "<STR_LIT>" . format ( recipient ) ) <EOL> h = binary ( recipient [ "<STR_LIT>" ] ) <EOL> s = binary ( recipient [ "<STR_LIT>" ] ) <EOL> header = json . loads ( native ( urlsafe_b64decode ( h ) ) ) <EOL> assertTrue ( header [ "<STR_LIT>" ] == ALG , <EOL> "<STR_LIT>" . format ( header [ "<STR_LIT>" ] ) ) <EOL> if "<STR_LIT>" in header [ "<STR_LIT>" ] and not "<STR_LIT>" in header [ "<STR_LIT>" ] : <EOL> header [ "<STR_LIT>" ] [ "<STR_LIT>" ] = header [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> assertTrue ( header [ "<STR_LIT>" ] [ "<STR_LIT>" ] == ALG , <EOL> "<STR_LIT>" . format ( header [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) ) <EOL> vk = urlsafe_b64decode ( binary ( header [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) ) <EOL> secured_input = b"<STR_LIT:.>" . join ( ( h , encoded_payload ) ) <EOL> sig = urlsafe_b64decode ( s ) <EOL> sig_msg = sig + secured_input <EOL> verified_input = native ( ed25519ll . crypto_sign_open ( sig_msg , vk ) ) <EOL> verified_header , verified_payload = verified_input . split ( '<STR_LIT:.>' ) <EOL> verified_header = binary ( verified_header ) <EOL> decoded_header = native ( urlsafe_b64decode ( verified_header ) ) <EOL> headers . append ( json . loads ( decoded_header ) ) <EOL> verified_payload = binary ( verified_payload ) <EOL> payload = json . loads ( native ( urlsafe_b64decode ( verified_payload ) ) ) <EOL> return headers , payload <EOL> def test ( ) : <EOL> kp = ed25519ll . crypto_sign_keypair ( ) <EOL> payload = { '<STR_LIT:test>' : '<STR_LIT>' } <EOL> jwsjs = json . loads ( json . dumps ( sign ( payload , kp ) ) ) <EOL> verify ( jwsjs ) <EOL> jwsjs [ '<STR_LIT>' ] += '<STR_LIT:x>' <EOL> try : <EOL> verify ( jwsjs ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) </s>
