Skip to content

Commit a6b5108

Browse files
committed
Test pem to x5c functions.
First pem_hash function.
1 parent 88542b7 commit a6b5108

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/cryptojwt/jwk/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,11 @@ def certificate_fingerprint(der, hash="sha256"):
290290
raise UnsupportedAlgorithm(hash)
291291

292292
return ':'.join([fp[i:i + 2] for i in range(0, len(fp), 2)]).upper()
293+
294+
295+
def pem_hash(pem_file):
296+
with open(pem_file, "r") as fp:
297+
pem = fp.read()
298+
299+
der = ssl.PEM_cert_to_DER_cert(pem)
300+
return hashlib.sha1(as_bytes(der)).hexdigest()

tests/test_02_jwk.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from cryptojwt.exception import UnsupportedAlgorithm
1717
from cryptojwt.exception import WrongUsage
1818
from cryptojwt.jwk import JWK
19+
from cryptojwt.jwk import pem_hash
20+
from cryptojwt.jwk import pems_to_x5c
1921
from cryptojwt.jwk.ec import NIST2SEC
2022
from cryptojwt.jwk.ec import ECKey
2123
from cryptojwt.jwk.hmac import SYMKey
@@ -639,3 +641,17 @@ def test_key_ops_and_use():
639641
key_ops=["sign", "verify"],
640642
use = "sig"
641643
)
644+
645+
646+
def test_pem_to_x5c():
647+
with open(full_path("cert.pem")) as fp:
648+
cert_chain = fp.read()
649+
650+
x5c = pems_to_x5c([cert_chain])
651+
assert len(x5c) == 1
652+
assert x5c[0] == 'MIIB2jCCAUOgAwIBAgIBATANBgkqhkiG9w0BAQUFADA0MRgwFgYDVQQDEw9UaGUgY29kZSB0ZXN0ZXIxGDAWBgNVBAoTD1VtZWEgVW5pdmVyc2l0eTAeFw0xMjEwMDQwMDIzMDNaFw0xMzEwMDQwMDIzMDNaMDIxCzAJBgNVBAYTAlNFMSMwIQYDVQQDExpPcGVuSUQgQ29ubmVjdCBUZXN0IFNlcnZlcjCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAwf+wiusGhA+gleZYQAOPQlNUIucPiqXdPVyieDqQbXXOPBe3nuggtVzeq7pVFH1dZz4dY2Q2LA5DaegvP8kRvoSB/87ds3dy3Rfym/GUSc5B0l1TgEobcyaep8jguRoHto6GWHfCfKqoUYZq4N8vh4LLMQwLR6zi6Jtu82nB5k8CAwEAATANBgkqhkiG9w0BAQUFAAOBgQCsTntG4dfW5kO/Qle6uBhIhZU+3IreIPmbwzpXoCbcgjRa01z6WiBLwDC1RLAL7ucaF/EVlUq4e0cNXKt4ESGNc1xHISOMLetwvS1SN5tKWA9HNua/SaqRtiShxLUjPjmrtpUgotLNDRvUYnTdTT1vhZar7TSPr1yObirjvz/qLw=='
653+
654+
655+
def test_pem_hash():
656+
_hash = pem_hash(full_path("cert.pem"))
657+
assert _hash

0 commit comments

Comments
 (0)