def test_publickey(self):
     seed = unhexlify(b"4ba96b0b5303328c7405220598a587c4"
                      b"acb06ed9a9601d149f85400195f1ec3d")
     sk = ed25519_blake2b.SigningKey(seed)
     self.failUnlessEqual(hexlify(sk.to_bytes()),
                          (b"4ba96b0b5303328c7405220598a587c4"
                           b"acb06ed9a9601d149f85400195f1ec3d"))
     self.failUnlessEqual(hexlify(sk.to_seed()),
                          (b"4ba96b0b5303328c7405220598a587c4"
                           b"acb06ed9a9601d149f85400195f1ec3d"))
     self.failUnlessRaises(ValueError, ed25519_blake2b.SigningKey,
                           b"wrong length")
     sk2 = ed25519_blake2b.SigningKey(seed)
     self.failUnlessEqual(sk, sk2)
    def test_OOP(self):
        # BLAKE2b fork doesn't include public key as a part of the secret key
        sk_s = unhexlify(b"4ba96b0b5303328c7405220598a587c4"
                         b"acb06ed9a9601d149f85400195f1ec3d")
        sk = ed25519_blake2b.SigningKey(sk_s)
        self.failUnlessEqual(len(sk.to_bytes()), 32)
        self.failUnlessEqual(sk.to_bytes(), sk_s)

        vk = sk.get_verifying_key()
        self.failUnlessEqual(len(vk.to_bytes()), 32)
        exp_vks = unhexlify(b"eb8d01ec6900d9536dc0574268a5bb10"
                            b"ae4ca3fa75d5dcf12dcc51c299adc032")
        self.failUnlessEqual(vk.to_bytes(), exp_vks)
        self.failUnlessEqual(ed25519_blake2b.VerifyingKey(vk.to_bytes()), vk)
        msg = b"hello world"
        sig = sk.sign(msg)
        self.failUnlessEqual(len(sig), 64)
        exp_sig = unhexlify(b"6f6371e5ca4270e27d130912ba346a17"
                            b"06c3a7b0fae93012013c4b39e360a248"
                            b"9e6a95843ad186f5f6305a6f08d07036"
                            b"53891d54872029aabc6dbf0a616a5c02")
        self.failUnlessEqual(sig, exp_sig)
        self.failUnlessEqual(vk.verify(sig, msg), None)  # also, don't throw
        self.failUnlessRaises(ed25519_blake2b.BadSignatureError, vk.verify,
                              sig, msg + b".. NOT!")
Beispiel #3
0
    def test_all(self):
        # kat-ed25519_blake2b-blake2b.txt has been derived from the original
        # kat-ed25519_blake2b.txt sample collection included with the library
        for i, line in enumerate(open("kat-ed25519-blake2b.txt")):
            x = line.split(":")
            A, B, C, D = [unhexlify(i.encode("ascii")) for i in x[:4]]
            # A is the 32 byte seed (the entropy input to H())
            # B is the public point (pubkey)
            # C is the message
            # D is 64 bytes of signature (R+S) prepended to the message

            seed = A
            vk_s = B
            # the NaCl signature is R+S, which happens to be the same as ours
            msg = C
            sig = D[:64]
            # note that R depends only upon the second half of H(seed). S
            # depends upon both the first half (the exponent) and the second
            # half

            #if len(msg) % 16 == 1:
            #    print "msg len = %d" % len(msg), time.time()

            sk = ed25519_blake2b.SigningKey(seed)
            vk = sk.get_verifying_key()
            self.failUnlessEqual(vk.to_bytes(), vk_s)
            vk2 = ed25519_blake2b.VerifyingKey(vk_s)
            self.failUnlessEqual(vk2, vk)  # objects should compare equal
            self.failUnlessEqual(vk2.to_bytes(), vk_s)
            newsig = sk.sign(msg)
            sig_R, sig_S = sig[:32], sig[32:]
            newsig_R, newsig_S = newsig[:32], newsig[32:]
            self.failUnlessEqual(hexlify(newsig),
                                 hexlify(sig))  # deterministic sigs
            self.failUnlessEqual(vk.verify(sig, msg), None)  # no exception
    def test_prefix(self):
        sk1, vk1 = ed25519_blake2b.create_keypair()
        PREFIX = b"private0-"
        p = sk1.to_bytes(PREFIX)
        # that gives us a binary string with a prefix
        self.failUnless(p[:len(PREFIX)] == PREFIX, repr(p))
        sk2 = ed25519_blake2b.SigningKey(p, prefix=PREFIX)
        self.failUnlessEqual(sk1, sk2)
        self.failUnlessEqual(repr(sk1.to_bytes()), repr(sk2.to_bytes()))
        self.failUnlessRaises(ed25519_blake2b.BadPrefixError,
                              ed25519_blake2b.SigningKey,
                              p,
                              prefix=b"WRONG-")
        # SigningKey.to_seed() can do a prefix too
        p = sk1.to_seed(PREFIX)
        self.failUnless(p[:len(PREFIX)] == PREFIX, repr(p))
        sk3 = ed25519_blake2b.SigningKey(p, prefix=PREFIX)
        self.failUnlessEqual(sk1, sk3)
        self.failUnlessEqual(repr(sk1.to_bytes()), repr(sk3.to_bytes()))
        self.failUnlessRaises(ed25519_blake2b.BadPrefixError,
                              ed25519_blake2b.SigningKey,
                              p,
                              prefix=b"WRONG-")

        # verifying keys can do this too
        PREFIX = b"public0-"
        p = vk1.to_bytes(PREFIX)
        self.failUnless(p.startswith(PREFIX), repr(p))
        vk2 = ed25519_blake2b.VerifyingKey(p, prefix=PREFIX)
        self.failUnlessEqual(vk1, vk2)
        self.failUnlessEqual(repr(vk1.to_bytes()), repr(vk2.to_bytes()))
        self.failUnlessRaises(ed25519_blake2b.BadPrefixError,
                              ed25519_blake2b.VerifyingKey,
                              p,
                              prefix=b"WRONG-")

        # and signatures
        PREFIX = b"sig0-"
        p = sk1.sign(b"msg", PREFIX)
        self.failUnless(p.startswith(PREFIX), repr(p))
        vk1.verify(p, b"msg", PREFIX)
        self.failUnlessRaises(ed25519_blake2b.BadPrefixError,
                              vk1.verify,
                              p,
                              b"msg",
                              prefix=b"WRONG-")
 def check1(encoding, expected):
     PREFIX = "private0-"
     p = sk1.to_ascii(PREFIX, encoding)
     self.failUnlessEqual(p, expected)
     sk2 = ed25519_blake2b.SigningKey(p,
                                      prefix=PREFIX,
                                      encoding=encoding)
     self.failUnlessEqual(repr(sk1.to_bytes()), repr(sk2.to_bytes()))
     self.failUnlessEqual(sk1, sk2)
    def test_object_identity(self):
        sk1_s = unhexlify(b"ef32972ae3f1252a5aa1395347ea008c"
                          b"bd2fed0773a4ea45e2d2d06c8cf8fbd4"
                          b"c024601a9c5b854fb100ff3116cf4f22"
                          b"a311565f027391cb49d3bbe11c44399d")
        sk2_s = unhexlify(b"3d550c158900b4c2922b6656d2f80572"
                          b"89de4ee65043745179685ae7d29b944d"
                          b"672b8a2cb23f9e75e1d46ce249cd9c04"
                          b"68f816f1c734a102822b60e18b41eacd")
        sk1a = ed25519_blake2b.SigningKey(sk1_s)
        sk1b = ed25519_blake2b.SigningKey(sk1_s)
        vk1a = sk1a.get_verifying_key()
        vk1b = sk1b.get_verifying_key()
        sk2 = ed25519_blake2b.SigningKey(sk2_s)
        vk2 = sk2.get_verifying_key()
        self.failUnlessEqual(sk1a, sk1b)
        self.failIfEqual(sk1a, sk2)
        self.failUnlessEqual(vk1a, vk1b)
        self.failIfEqual(vk1a, vk2)

        self.failIfEqual(sk2, b"not a SigningKey")
        self.failIfEqual(vk2, b"not a VerifyingKey")
    def FromBytes(cls,
                  key_bytes: bytes) -> IPrivateKey:
        """
        Construct class from key bytes.

        Args:
            key_bytes (bytes): Key bytes

        Returns:
            IPrivateKey: IPrivateKey object

        Raises:
            ValueError: If key bytes are not valid
        """
        try:
            return cls(ed25519_blake2b.SigningKey(key_bytes))
        except ValueError as ex:
            raise ValueError("Invalid private key bytes") from ex
    def test_encoding(self):
        sk_s = b"\x88" * 32  # usually urandom(32)
        sk1 = ed25519_blake2b.SigningKey(sk_s)
        vk1 = sk1.get_verifying_key()

        def check1(encoding, expected):
            PREFIX = "private0-"
            p = sk1.to_ascii(PREFIX, encoding)
            self.failUnlessEqual(p, expected)
            sk2 = ed25519_blake2b.SigningKey(p,
                                             prefix=PREFIX,
                                             encoding=encoding)
            self.failUnlessEqual(repr(sk1.to_bytes()), repr(sk2.to_bytes()))
            self.failUnlessEqual(sk1, sk2)

        check1("base64",
               b"private0-iIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIg")
        check1(
            "base32",
            b"private0-rceirceirceirceirceirceirceirceirceirceirceirceircea")
        check1(
            "hex",
            b"private0-8888888888888888888888888888888888888888888888888888888888888888"
        )

        def check2(encoding, expected):
            PREFIX = "public0-"
            p = vk1.to_ascii(PREFIX, encoding)
            self.failUnlessEqual(p, expected)
            vk2 = ed25519_blake2b.VerifyingKey(p,
                                               prefix=PREFIX,
                                               encoding=encoding)
            self.failUnlessEqual(repr(vk1.to_bytes()), repr(vk2.to_bytes()))
            self.failUnlessEqual(vk1, vk2)

        check2("base64",
               b"public0-LyjqpyjpsKe+gSWQ0qPqcLyc7r+yrZkO54E+RqDzpG8")
        check2(
            "base32",
            b"public0-f4uovjzi5gykppubewinfi7koc6jz3v7wkwzsdxhqe7enihturxq")
        check2(
            "hex",
            b"public0-2f28eaa728e9b0a7be812590d2a3ea70bc9ceebfb2ad990ee7813e46a0f3a46f"
        )

        def check3(encoding, expected):
            msg = b"msg"
            PREFIX = "sig0-"
            sig = sk1.sign(msg, PREFIX, encoding)
            self.failUnlessEqual(sig, expected)
            vk1.verify(sig, msg, PREFIX, encoding)

        check3(
            "base64",
            b"sig0-zaHq9S5BED37aeA0YH7Evw63mJUYyEWWP80EIEu1ERvQDwN3uyJodFqunYIL9/DMsK9RkEeuZdcc5BJtRZ15CA"
        )
        check3(
            "base32",
            b"sig0-zwq6v5joieid363j4a2ga7wex4hlpgevddeelfr7zuccas5vcen5adydo65se2dulkxj3aql67ymzmfpkgiepltf24ooietniwoxsca"
        )
        check3(
            "hex",
            b"sig0-cda1eaf52e41103dfb69e034607ec4bf0eb7989518c845963fcd04204bb5111bd00f0377bb2268745aae9d820bf7f0ccb0af519047ae65d71ce4126d459d7908"
        )
Beispiel #9
0
def get_pair_ed_key_from_seed(seed):
    sk = ed25519_blake2b.SigningKey(seed.encode('utf-8'))
    return sk, sk.get_verifying_key()
Beispiel #10
0
from pyblake2 import blake2b
import ed25519_blake2b
import os

with open('init_del.json') as f:
    data = json.load(f)

directory = 'accounts'
account = {}
if not os.path.exists(directory):
    os.makedirs(directory)

for y in data['accounts']:
    fw = open(directory + '/' + y['info'] + '.json', 'w')
    keydata = bytes.fromhex(y['private'])
    sk = ed25519_blake2b.SigningKey(keydata)

    ################################################################################
    ## GENERATE SEND INFORMATION
    ################################################################################
    h = blake2b(digest_size=32)
    h.update(qlmdb3.fromaccount(y['account']))  # destination
    h.update(binascii.unhexlify(qlmdb3.hexstr(int(y['amount']), 16)))  # amount

    send_hash = binascii.hexlify(h.digest()).decode('ascii')
    hashdata = bytes.fromhex(send_hash)
    sig = sk.sign(hashdata)
    hexSig = sig.hex().upper()

    account['funding_info'] = {
        'account':