Esempio n. 1
0
class ECPrivateKeyCompressedTest(unittest.TestCase):
    ref = _compressed_info

    def setUp(self):
        self.private_key = ECPrivateKey(
            self.ref['hex_private_key'], compressed=True)

    def tearDown(self):
        pass

    def test_random_private_key(self):
        private_key = ECPrivateKey()
        self.assertTrue(isinstance(private_key, ECPrivateKey))

    def test_hex_private_key(self):
        self.assertEqual(self.private_key.to_hex(), self.ref['hex_private_key'])

    def test_wif_private_key(self):
        self.assertEqual(self.private_key.to_wif(), self.ref['wif_private_key'])

    def test_pem_private_key(self):
        self.assertEqual(self.private_key.to_pem(), self.ref['pem_private_key'])

    def test_der_private_key(self):
        self.assertEqual(self.private_key.to_der(), self.ref['der_private_key'])

    def test_to_public_key_conversion(self):
        public_key = self.private_key.public_key()
        self.assertEqual(public_key.to_hex(), self.ref['hex_public_key'])
        self.assertEqual(public_key.address(), self.ref['address'])
def sign_token(claim, signing_private_key, subject, issuer=None,
               signing_algorithm="ES256K"):
    if signing_algorithm != 'ES256K':
        raise ValueError("Signing algorithm not supported")

    private_key_object = ECPrivateKey(signing_private_key)
    public_key_hex = private_key_object.public_key().to_hex()

    if not issuer:
        issuer = {
            "publicKey": public_key_hex
        }

    current_time = datetime.datetime.now()

    payload = {
        "claim": claim,
        "subject": subject,
        "issuer": issuer,
        "issuedAt": current_time.isoformat(),
        "expiresAt": current_time.replace(current_time.year + 1).isoformat()
    }

    token_signer = TokenSigner()
    token = token_signer.sign(payload, private_key_object.to_pem())

    return token
Esempio n. 3
0
class ECPrivateKeyTest(unittest.TestCase):
    reference = _reference_info

    def setUp(self):
        self.private_key = ECPrivateKey(self.reference['hex_private_key'],
                                        compressed=False)

    def tearDown(self):
        pass

    def test_random_private_key(self):
        private_key = ECPrivateKey()
        self.assertTrue(isinstance(private_key, ECPrivateKey))

    def test_private_key_from_wif(self):
        self.private_key_from_wif = ECPrivateKey(
            self.reference['wif_private_key'], compressed=False)
        self.assertEqual(self.private_key.to_hex(),
                         self.private_key_from_wif.to_hex())

    def test_hex_private_key(self):
        self.assertEqual(self.private_key.to_hex(),
                         self.reference['hex_private_key'])

    def test_wif_private_key(self):
        self.assertEqual(self.private_key.to_wif(),
                         self.reference['wif_private_key'])

    def test_pem_private_key(self):
        self.assertEqual(self.private_key.to_pem(),
                         self.reference['pem_private_key'])

    def test_der_private_key(self):
        self.assertEqual(self.private_key.to_der(),
                         self.reference['der_private_key'])
Esempio n. 4
0
class ECPrivateKeyTest(unittest.TestCase):
    reference = _reference_info

    def setUp(self):
        self.private_key = ECPrivateKey(self.reference['hex_private_key'], compressed=False)

    def tearDown(self):
        pass

    def test_random_private_key(self):
        private_key = ECPrivateKey()
        self.assertTrue(isinstance(private_key, ECPrivateKey))

    def test_private_key_from_wif(self):
        self.private_key_from_wif = ECPrivateKey(
            self.reference['wif_private_key'], compressed=False)
        self.assertEqual(
            self.private_key.to_hex(), self.private_key_from_wif.to_hex())

    def test_hex_private_key(self):
        self.assertEqual(
            self.private_key.to_hex(), self.reference['hex_private_key'])

    def test_wif_private_key(self):
        self.assertEqual(
            self.private_key.to_wif(), self.reference['wif_private_key'])

    def test_pem_private_key(self):
        self.assertEqual(
            self.private_key.to_pem(), self.reference['pem_private_key'])

    def test_der_private_key(self):
        self.assertEqual(
            self.private_key.to_der(), self.reference['der_private_key'])
Esempio n. 5
0
def sign_token(claim,
               signing_private_key,
               subject,
               issuer=None,
               signing_algorithm="ES256K"):
    if signing_algorithm != 'ES256K':
        raise ValueError("Signing algorithm not supported")

    private_key_object = ECPrivateKey(signing_private_key)
    public_key_hex = private_key_object.public_key().to_hex()

    if not issuer:
        issuer = {"publicKey": public_key_hex}

    current_time = datetime.datetime.now()

    payload = {
        "claim": claim,
        "subject": subject,
        "issuer": issuer,
        "issuedAt": current_time.isoformat(),
        "expiresAt": current_time.replace(current_time.year + 1).isoformat()
    }

    token_signer = TokenSigner()
    token = token_signer.sign(payload, private_key_object.to_pem())

    return token