1717import json
1818
1919import jwt
20- import Crypto
21- from Crypto .PublicKey import RSA
22- from Crypto .Cipher import PKCS1_OAEP
23- from Crypto .Signature import PKCS1_v1_5
24- from Crypto import Random
25- from Crypto .Cipher import AES
2620from urllib .parse import urlencode
2721
2822from cryptography .hazmat .primitives .asymmetric import ed25519
29- from cryptography .hazmat .primitives import serialization
3023
3124from .message_types import ButtonMessage
3225from . import mixin_api
@@ -46,14 +39,14 @@ def __init__(self, mixin_config):
4639 self .private_key_base64 = self .private_key
4740
4841 if self .private_key .find ('RSA PRIVATE KEY' ) >= 0 :
42+ raise Exception ("RSA private key supports has been deprecated, use ed25519 instead!" )
4943 self .algorithm = 'RS512'
5044 else :
5145 self .algorithm = 'EdDSA'
5246 self .private_key = self .decode_ed25519 (self .private_key )
5347
5448 self .client = httpx .AsyncClient ()
5549
56- self .keyForAES = ""
5750 # mixin api base url
5851 self .api_base_url = 'https://api.mixin.one'
5952 #self.api_base_url = 'https://mixin-api.zeromesh.net'
@@ -89,17 +82,6 @@ def gen_get_jwt_token(self, uristring, bodystring, jti):
8982
9083 return encoded
9184
92- def gen_get_listen_signed_token (self , uristring , bodystring , jti ):
93- jwtSig = self .gen_get_sig (uristring , bodystring )
94- iat = datetime .datetime .utcnow ()
95- exp = datetime .datetime .utcnow () + datetime .timedelta (seconds = 200 )
96- encoded = jwt .encode ({'uid' :self .client_id , 'sid' :self .pay_session_id ,'iat' :iat ,'exp' : exp , 'jti' :jti ,'sig' :jwtSig }, self .private_key , algorithm = self .algorithm )
97- privKeyObj = RSA .importKey (self .private_key )
98- signer = PKCS1_v1_5 .new (privKeyObj )
99- signature = signer .sign (encoded )
100- return signature
101-
102-
10385 def gen_post_jwt_token (self , uristring , bodystring , jti ):
10486 jwtSig = self .genPOSTSig (uristring , bodystring )
10587 iat = datetime .datetime .utcnow ()
@@ -108,38 +90,8 @@ def gen_post_jwt_token(self, uristring, bodystring, jti):
10890 return encoded
10991
11092 def gen_encrypted_pin (self , iterString = None ):
111- if self .algorithm == 'EdDSA' :
112- return mixin_api .encrypt_ed25519_pin (self .pay_pin , self .pin_token , self .pay_session_id , self .private_key_base64 , int (time .time ()* 1e9 ))
113-
114- if self .keyForAES == "" :
115- privKeyObj = RSA .importKey (self .private_key )
116- decoded_result = base64 .b64decode (self .pin_token )
117- cipher = PKCS1_OAEP .new (key = privKeyObj , hashAlgo = Crypto .Hash .SHA256 , label = self .pay_session_id .encode ("utf-8" ))
118- decrypted_msg = cipher .decrypt (decoded_result )
119- self .keyForAES = decrypted_msg
120-
121- tsstring = int (time .time ()) # unix time
122- tsstring = tsstring .to_bytes (8 , 'little' )
123-
124- if iterString is None :
125- iterator = int (time .time () * 1e9 ) # unix nano
126- iterator = iterator .to_bytes (8 , 'little' )
127- toEncryptContent = self .pay_pin .encode ('utf8' ) + tsstring + iterator
128- else :
129- toEncryptContent = self .pay_pin .encode ('utf8' ) + tsstring + iterString
130-
131- toPadCount = AES .block_size - len (toEncryptContent ) % AES .block_size
132- toEncryptContent = toEncryptContent + int .to_bytes (toPadCount , 1 , 'little' ) * toPadCount
133-
134- iv = Random .new ().read (AES .block_size )
135-
136- cipher = AES .new (self .keyForAES , AES .MODE_CBC ,iv )
137- encrypted_result = cipher .encrypt (toEncryptContent )
138-
139- msg = iv + encrypted_result
140- encrypted_pin = base64 .b64encode (msg )
141-
142- return encrypted_pin .decode ()
93+ assert self .algorithm == 'EdDSA' , "mixin bot only support ed25519 crypto now!"
94+ return mixin_api .encrypt_ed25519_pin (self .pay_pin , self .pin_token , self .pay_session_id , self .private_key_base64 , int (time .time ()* 1e9 ))
14395
14496 def __genUrl (self , path ):
14597 """
0 commit comments