|
11 | 11 | from enum import Enum, unique |
12 | 12 |
|
13 | 13 | import jwt |
14 | | -from ecdsa import SigningKey, SECP256k1, VerifyingKey |
15 | 14 | from gmssl import func, sm2, sm3 |
16 | 15 | from google.protobuf.descriptor import FieldDescriptor |
17 | 16 | from jsoncomment import JsonComment |
@@ -326,57 +325,6 @@ def make_job_event_message(job_id, job_priority, initiator_agency_id, receiver_a |
326 | 325 | return message.encode('utf-8') |
327 | 326 |
|
328 | 327 |
|
329 | | -def sign_with_secp256k1(message, private_key_str): |
330 | | - if isinstance(private_key_str, str): |
331 | | - private_key = SigningKey.from_string( |
332 | | - bytes().fromhex(private_key_str), curve=SECP256k1) |
333 | | - else: |
334 | | - private_key = SigningKey.from_string(private_key_str, curve=SECP256k1) |
335 | | - signature_bytes = private_key.sign( |
336 | | - make_hash(message, CryptoType.ECDSA, HashType.BYTES)) |
337 | | - return str(encode(signature_bytes), 'utf-8') |
338 | | - |
339 | | - |
340 | | -def verify_with_secp256k1(message, signature_str, public_key_str): |
341 | | - if isinstance(public_key_str, str): |
342 | | - verify_key = VerifyingKey.from_string( |
343 | | - bytes().fromhex(public_key_str), curve=SECP256k1) |
344 | | - else: |
345 | | - verify_key = VerifyingKey.from_string( |
346 | | - decode(public_key_str), curve=SECP256k1) |
347 | | - return verify_key.verify(decode(signature_str), make_hash(message, CryptoType.ECDSA, HashType.BYTES)) |
348 | | - |
349 | | - |
350 | | -def sign_with_sm2(message, private_key_str): |
351 | | - sm2_crypt = sm2.CryptSM2(private_key_str, "") |
352 | | - random_hex_str = func.random_hex(sm2_crypt.para_len) |
353 | | - message_hash = make_hash(message, CryptoType.GM).encode(encoding='utf-8') |
354 | | - signature_str = sm2_crypt.sign(message_hash, random_hex_str) |
355 | | - return signature_str |
356 | | - |
357 | | - |
358 | | -def verify_with_sm2(message, signature_str, public_key_str): |
359 | | - sm2_crypt = sm2.CryptSM2("", public_key_str) |
360 | | - message_hash = make_hash(message, CryptoType.GM).encode(encoding='utf-8') |
361 | | - return sm2_crypt.verify(signature_str, message_hash) |
362 | | - |
363 | | - |
364 | | -def make_signature(message, private_key_str, crypto_type): |
365 | | - if crypto_type == CryptoType.ECDSA: |
366 | | - return sign_with_secp256k1(message, private_key_str) |
367 | | - |
368 | | - if crypto_type == CryptoType.GM: |
369 | | - return sign_with_sm2(message, private_key_str) |
370 | | - |
371 | | - |
372 | | -def verify_signature(message, signature, public_key_str, crypto_type): |
373 | | - if crypto_type == CryptoType.ECDSA: |
374 | | - return verify_with_secp256k1(message, signature, public_key_str) |
375 | | - |
376 | | - if crypto_type == CryptoType.GM: |
377 | | - return verify_with_sm2(message, signature, public_key_str) |
378 | | - |
379 | | - |
380 | 328 | def exec_bash_command(cmd): |
381 | 329 | """replace commands.get_status_output |
382 | 330 |
|
|
0 commit comments