예제 #1
1
def test_crypto():
    pts = ((0x50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352,
            0x2cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6),
           (0xa83b8de893467d3a88d959c0eb4032d9ce3bf80f175d4d9e75892a3ebb8ab7e5,
            0x370f723328c24b7a97fe34063ba68f253fb08f8645d7c8b9a4ff98e3c29e7f0d),
           (0xf680556678e25084a82fa39e1b1dfd0944f7e69fddaa4e03ce934bd6b291dca0,
            0x52c10b721d34447e173721fb0151c68de1106badb089fb661523b8302a9097f5),
           (0x241febb8e23cbd77d664a18f66ad6240aaec6ecdc813b088d5b901b2e285131f,
            0x513378d9ff94f8d3d6c420bd13981df8cd50fd0fbd0cb5afabb3e66f2750026d))

    for pt in pts:
        b = bytes([(pt[1] & 0x1) + 0x2]) + pt[0].to_bytes(32, 'big')
        b_full = bytes([0x04]) + pt[0].to_bytes(32, 'big') + pt[1].to_bytes(32, 'big')
        pk = PublicKey.from_bytes(b)
        assert pk.point.y == pt[1]
        assert b == pk.compressed_bytes
        assert b_full == bytes(pk)

        assert bytes(PublicKey.from_hex(pk.to_hex())) == b_full

    for i in range(10):
        pk = PrivateKey.from_random()
        assert PrivateKey.from_hex(pk.to_hex()).key == pk.key

    hd_priv = HDPrivateKey.master_key_from_entropy()[0]
    hd_priv2 = HDKey.from_hex(hd_priv.to_hex())
    hd_pub = hd_priv.public_key
    hd_pub2 = HDKey.from_hex(hd_pub.to_hex())

    assert isinstance(hd_priv2, HDPrivateKey)
    assert hd_priv2._key.key == hd_priv._key.key
    assert hd_priv2.chain_code == hd_priv.chain_code

    assert isinstance(hd_pub2, HDPublicKey)
    assert hd_pub2._key.point.x == hd_pub._key.point.x
    assert hd_pub2._key.point.y == hd_pub._key.point.y
    assert hd_pub2.chain_code == hd_pub.chain_code
예제 #2
0
    def from_random():
        """ Initializes the object using a random private_key.
            The private key is generated using the system entropy source.

        Returns:
            MachineAuth: Constructed MachineAuth object.
            Returns None if the auth key is not found in the keyring.
        """
        auth_key = PrivateKey.from_random()
        return MachineAuth(auth_key)
예제 #3
0
def test_crypto():
    pts = (
        (0x50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352,
         0x2cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6),
        (0xa83b8de893467d3a88d959c0eb4032d9ce3bf80f175d4d9e75892a3ebb8ab7e5,
         0x370f723328c24b7a97fe34063ba68f253fb08f8645d7c8b9a4ff98e3c29e7f0d),
        (0xf680556678e25084a82fa39e1b1dfd0944f7e69fddaa4e03ce934bd6b291dca0,
         0x52c10b721d34447e173721fb0151c68de1106badb089fb661523b8302a9097f5),
        (0x241febb8e23cbd77d664a18f66ad6240aaec6ecdc813b088d5b901b2e285131f,
         0x513378d9ff94f8d3d6c420bd13981df8cd50fd0fbd0cb5afabb3e66f2750026d))

    for pt in pts:
        b = bytes([(pt[1] & 0x1) + 0x2]) + pt[0].to_bytes(32, 'big')
        b_full = bytes([0x04]) + pt[0].to_bytes(32, 'big') + pt[1].to_bytes(
            32, 'big')
        pk = PublicKey.from_bytes(b)
        assert pk.point.y == pt[1]
        assert b == pk.compressed_bytes
        assert b_full == bytes(pk)

        assert bytes(PublicKey.from_hex(pk.to_hex())) == b_full

    for i in range(10):
        pk = PrivateKey.from_random()
        assert PrivateKey.from_hex(pk.to_hex()).key == pk.key

    hd_priv = HDPrivateKey.master_key_from_entropy()[0]
    hd_priv2 = HDKey.from_hex(hd_priv.to_hex())
    hd_pub = hd_priv.public_key
    hd_pub2 = HDKey.from_hex(hd_pub.to_hex())

    assert isinstance(hd_priv2, HDPrivateKey)
    assert hd_priv2._key.key == hd_priv._key.key
    assert hd_priv2.chain_code == hd_priv.chain_code

    assert isinstance(hd_pub2, HDPublicKey)
    assert hd_pub2._key.point.x == hd_pub._key.point.x
    assert hd_pub2._key.point.y == hd_pub._key.point.y
    assert hd_pub2.chain_code == hd_pub.chain_code
예제 #4
0
    def __init__(self, private_key):
        """ Initialize using the provided private_key.

        Args:
            private_key (PrivateKey/str): Private key to use for initializing \
            the object. This can be a PrivateKey object or a base58 \
            encoded string.

        Returns:
            None:
        """
        if private_key:
            if isinstance(private_key, str):  # base58 encoded string
                self.private_key = PrivateKey.from_b58check(private_key)
            else:
                self.private_key = private_key
            self.public_key = self.private_key.public_key
        else:
            self.private_key = None
            self.public_key = None