Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Paste
P7337
(An Untitled Masterwork)
Active
Public
Actions
Authored by
Vgutierrez
on Jul 9 2018, 10:22 AM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Award Token
Flag For Later
Tags
None
Referenced Files
F23503012:
Jul 9 2018, 10:22 AM
2018-07-09 10:22:14 (UTC+0)
Subscribers
None
class
PrivateKey
(
abc
.
ABC
):
def
__init__
(
self
):
self
.
key
=
None
@abc.abstractmethod
def
generate
(
self
,
**
kwargs
):
pass
@staticmethod
def
load
(
filename
):
with
open
(
filename
,
'rb'
)
as
key_file
:
private_key
=
serialization
.
load_pem_private_key
(
key_file
.
read
(),
password
=
None
,
backend
=
default_backend
(),
)
if
isinstance
(
private_key
,
rsa
.
RSAPrivateKey
):
return
RSAPrivateKey
(
private_key
=
private_key
)
elif
isinstance
(
private_key
,
ec
.
EllipticCurvePrivateKey
):
return
ECPrivateKey
(
private_key
=
private_key
)
else
:
raise
NotImplementedError
(
"Unsupported private key type"
)
def
save
(
self
,
filename
):
with
open
(
filename
,
'wb'
,
opener
=
secure_opener
)
as
key_file
:
key_file
.
write
(
self
.
key
.
private_bytes
(
encoding
=
serialization
.
Encoding
.
PEM
,
format
=
serialization
.
PrivateFormat
.
TraditionalOpenSSL
,
encryption_algorithm
=
serialization
.
NoEncryption
(),
))
class
RSAPrivateKey
(
PrivateKey
):
def
__init__
(
self
,
private_key
=
None
):
super
()
.
__init__
(
private_key
)
def
generate
(
self
,
size
=
2048
):
self
.
key
=
rsa
.
generate_private_key
(
public_exponent
=
65537
,
key_size
=
size
,
backend
=
default_backend
(),
)
class
ECPrivateKey
(
PrivateKey
):
def
__init__
(
self
,
private_key
=
None
):
super
()
.
__init__
(
private_key
)
def
generate
(
self
,
curve
=
ec
.
SECP256R1
):
self
.
key
=
ec
.
generate_private_key
(
curve
=
curve
,
backend
=
default_backend
(),
)
Event Timeline
Vgutierrez
created this paste.
Jul 9 2018, 10:22 AM
2018-07-09 10:22:14 (UTC+0)
Log In to Comment