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 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 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
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)
# -*- coding: utf-8 -*- # @Author : 河北雪域网络科技有限公司 A.Star # @contact: [email protected] # @site: www.snowland.ltd # @file: demo.py # @time: 2018/11/19 1:16 # @Software: PyCharm import time from pysmx.SM3 import SM3Type, digest, KDF from pysmx.crypto.hashlib import pbkdf2_hmac from collections import namedtuple if __name__ == '__main__': from pysmx.SM2 import generate_keypair KeyPair = namedtuple('KeyPair', ['publicKey', 'privateKey']) key = generate_keypair() pk = key.publicKey sk = key.privateKey print("sk:", sk) print("pk:", pk) a = bytes("abc"*10000, encoding='utf8') st = time.clock() y = digest(a) et = time.clock() print("sm3:", y) print("time:", et - st) klen = 19 print(KDF("57E7B63623FAE5F08CDA468E872A20AFA03DED41BF1403770E040DC83AF31A67991F2B01EBF9EFD8881F0A0493000603", klen)) sm = SM3Type() sm.update('abc') print(sm.digest())