def gen_sm2_keypair(work_dir, chain_name): pk, sk = generate_keypair() addr = '0x' + hash_msg(pk)[24:] path = os.path.join(work_dir, 'cita-cloud/{}/node_key'.format(chain_name)) with open(path, 'wt') as stream: stream.write('0x' + sk.hex()) path = os.path.join(work_dir, 'cita-cloud/{}/node_address'.format(chain_name)) with open(path, 'wt') as stream: stream.write(addr) return addr
def gen_sm2_authorities(work_dir, chain_name, peers_count): authorities = [] for i in range(peers_count): pk, sk = generate_keypair() addr = '0x' + hash_msg(pk)[24:] path = os.path.join( "{0}/cita-cloud/{1}/node{2}".format(work_dir, chain_name, i), 'node_key') with open(path, 'wt') as stream: stream.write('0x' + sk.hex()) path = os.path.join( "{0}/cita-cloud/{1}/node{2}".format(work_dir, chain_name, i), 'node_address') with open(path, 'wt') as stream: stream.write(addr) path = os.path.join( "{0}/cita-cloud/{1}/node{2}".format(work_dir, chain_name, i), 'key_id') with open(path, 'wt') as stream: stream.write(str(i)) authorities.append(addr) return authorities
def main(): pk, sk = generate_keypair() address = '0x' + hash_msg(pk)[24:] print("address:", address) current_dir = os.path.abspath(os.curdir) target_dir = os.path.join(current_dir, address) if not os.path.exists(target_dir): os.makedirs(target_dir) path = os.path.join(target_dir, 'node_key') with open(path, 'wt') as stream: stream.write('0x' + sk.hex()) path = os.path.join(target_dir, 'node_address') with open(path, 'wt') as stream: stream.write(address) path = os.path.join(target_dir, 'key_id') with open(path, 'wt') as stream: stream.write("1")
def do_validate(): msg = open("input.txt", 'rb').read() # hashed = bytes.fromhex(sm3.sm3_hash(func.bytes_to_list(msg))) Too Too Too slow! hashed = bytes.fromhex(hash_msg(msg)) # too too too slow! return hashed
from pysmx.SM3 import hash_msg from pysmx.SM2 import generate_keypair from gmssl import sm2, func if __name__ == "__main__": file = open("sm3.pdf", "rb") data = file.read() print(hash_msg(data) + "\n") with open("sm3pdf-hash.txt", "w", encoding="utf-8") as hash_file: hash_file.write(hash_msg(data)) pk, sk = generate_keypair() print(pk.hex() + "\n") print(sk.hex() + "\n") sm2_crypt = sm2.CryptSM2(public_key=pk.hex(), private_key=sk.hex()) with open("sm2.pem", "w", encoding="utf-8") as sm2: sm2.write(sk.hex()) with open("sm2Pub.pem", "w", encoding="utf-8") as sm2Pub: sm2Pub.write(pk.hex()) data = bytes.fromhex(hash_msg(data)) random_hex_str = func.random_hex(sm2_crypt.para_len) sign = sm2_crypt.sign(data, random_hex_str) print(sign + "\n") with open("sm3pdf-sign.txt", "w", encoding="utf-8") as sm3PdfSign: sm3PdfSign.write(sign)