Exemplo n.º 1
0
    def test_load_key_pair_from_private_key_and_verify_signature(self):

        private_key_file_path = join_with_root(private_key_file)
        password_path = join_with_root(private_key_password_file)

        with open(password_path) as f:
            password = f.read()

        print(password)
        pri_key, pub_key = ECCipher.load_key_pair(private_key_file_path,
                                                  password)

        # ---------- sign and verify ------------
        data = "testdata"
        new_signature = ECCipher.create_signature(pri_key, data)
        print("new_signature is:" + new_signature.hex())

        is_valid_sign = verify_geth_signature(pub_key, new_signature, data)
        print("is valid new_signature:" + str(is_valid_sign))
        self.assertTrue(is_valid_sign, "should be success")

        is_valid_sign = verify_geth_signature(pub_key, new_signature,
                                              data + "error")
        print("is valid new_signature:" + str(is_valid_sign))
        self.assertFalse(is_valid_sign, "should be failed")
Exemplo n.º 2
0
    def setUp(self):
        private_key_file_path1 = join_with_root(private_key_file1)
        private_key_file_path2 = join_with_root(private_key_file2)
        password_path = join_with_root(private_key_password_file)

        with open(password_path) as f:
            password = f.read()

        self.pri_key, self.pub_key = ECCipher.load_key_pair(private_key_file_path1, password)
        self.pri_key_2, self.pub_key_2 = ECCipher.load_key_pair(private_key_file_path2, password)

        self.pub_key_string = ECCipher.serialize_public_key(self.pub_key)
        self.pub_key_string2 = ECCipher.serialize_public_key(self.pub_key_2)

        self.address = ECCipher.get_address_from_public_key(self.pub_key)
        self.address_2 = ECCipher.get_address_from_public_key(self.pub_key_2)
Exemplo n.º 3
0
    def test_generate_key_pair_signature_and_verify(self):
        private_key_file_path = join_with_root(private_key_file)
        password_path = join_with_root(private_key_password_file)

        with open(password_path) as f:
            password = f.read()

        print(password)
        new_pri_key, pub_key = ECCipher.load_key_pair(private_key_file_path,
                                                      password)

        sample = b"qZaQ6S"
        print("gen prikey:", new_pri_key)

        self.assertIsNotNone(new_pri_key)

        new_signature = ECCipher.create_signature(new_pri_key, sample)
        print("new_signature is:", new_signature)
        self.assertIsNotNone(new_signature)

        # pub_key = ECCipher.create_public_key(new_pub_key)
        is_valid_sign = ECCipher.verify_sign(pub_key, new_signature, sample)

        print("is valid new_signature:", is_valid_sign)
        self.assertTrue(is_valid_sign)
Exemplo n.º 4
0
    def test_load_key(self):
        pri_k, pub_k = ECCipher.load_key_pair(self.private_key_file_path,
                                              self.password)
        self.assertIsNotNone(pri_k)
        self.assertIsNotNone(pub_k)

        pri_key = ECCipher.load_private_key(self.private_key_file_path,
                                            self.password)
        self.assertIsNotNone(pri_key)

        msg = 'test123'
        sig = ECCipher.create_signature(pri_key, msg)
        print('sig:', sig)
        self.assertIsNotNone(sig)

        create_pub_key = ECCipher.create_public_key_from_private_key(pri_key)
        self.assertIsNotNone(create_pub_key)

        load_pub_key = ECCipher.load_public_key(self.private_key_file_path,
                                                self.password)
        self.assertIsNotNone(load_pub_key)

        v1 = ECCipher.verify_sign(load_pub_key, sig, msg)
        self.assertTrue(v1)

        v2 = ECCipher.verify_sign(create_pub_key, sig, msg)
        self.assertTrue(v2)

        addr = ECCipher.get_address_from_public_key(load_pub_key)
        print(addr)
        self.assertIsNotNone(addr)