# Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu> # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
"""Functions that load and write PEM-encoded files."""
import base64 import typing
# Should either be ASCII strings or bytes.
FlexiText = typing.Union[str, bytes]
def _markers(pem_marker: FlexiText) -> typing.Tuple[bytes, bytes]: """
Returns the start and end PEM markers, as bytes. """
:param contents: the contents of the file to interpret
:param pem_marker: the marker of the PEM content, such as'RSA PRIVATE KEY'
when your file has '-----BEGIN RSA PRIVATE KEY-----'and '-----END RSA PRIVATE KEY-----' markers.
:return: the base64-decoded content between the start and end markers.
@raise ValueError: when the content is invalid, for example when the start
marker cannot be found.
"""
# We want bytes, not text. If it's text, it can be converted to ASCII bytes. ifnot isinstance(contents, bytes):
contents = contents.encode("ascii")
(pem_start, pem_end) = _markers(pem_marker)
pem_lines = [line for line in _pem_lines(contents, pem_start, pem_end)]
# Base64-decode the contents
pem = b"".join(pem_lines) return base64.standard_b64decode(pem)
:param contents: the contents to encode in PEM format
:param pem_marker: the marker of the PEM content, such as'RSA PRIVATE KEY'
when your file has '-----BEGIN RSA PRIVATE KEY-----'and '-----END RSA PRIVATE KEY-----' markers.
:return: the base64-encoded content between the start and end markers, as bytes.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.