Skip to content

Commit 8286c8b

Browse files
committed
Pass tests
1 parent f6578c1 commit 8286c8b

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

pynfe/entidades/certificado.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,19 @@ def separar_arquivo(self, senha, caminho=False):
5656
"Falha ao abrir arquivo do certificado digital A1. Causa desconhecida."
5757
) from exc
5858

59+
if not isinstance(senha, bytes):
60+
senha = str.encode(senha)
61+
5962
# Carrega o arquivo .pfx, erro pode ocorrer se a senha estiver errada ou formato invalido.
6063
try:
6164
(
6265
chave,
6366
cert,
6467
) = pkcs12.load_key_and_certificates(
65-
cert_conteudo, str.encode(senha)
68+
cert_conteudo, senha
6669
)[:2]
67-
except ValueError as e:
68-
if "bad decrypt" in str(e).lower():
70+
except Exception as e:
71+
if "invalid password" in str(e).lower():
6972
raise Exception(
7073
"Falha ao carregar certificado digital A1. Verifique a senha do"
7174
" certificado."
@@ -82,7 +85,7 @@ def separar_arquivo(self, senha, caminho=False):
8285
with tempfile.NamedTemporaryFile(delete=False) as arqchave:
8386
arqchave.write(
8487
chave.private_bytes(
85-
Encoding.PEM, PrivateFormat.TraditionalOpenSSL, NoEncryption()
88+
Encoding.PEM, PrivateFormat.PKCS8, NoEncryption()
8689
)
8790
)
8891
self.arquivos_temp.append(arqchave.name)
@@ -97,7 +100,7 @@ def separar_arquivo(self, senha, caminho=False):
97100

98101
# Chave, string decodificada da chave privada
99102
chave = chave.private_bytes(
100-
Encoding.PEM, PrivateFormat.TraditionalOpenSSL, NoEncryption()
103+
Encoding.PEM, PrivateFormat.PKCS8, NoEncryption()
101104
)
102105

103106
return chave, cert

0 commit comments

Comments
 (0)