| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2015 Google Inc. All rights reserved. |
| 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at |
| 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. |
| 14 """Helper functions for commonly used utilities.""" |
| 15 |
| 16 import base64 |
| 17 import json |
| 18 import six |
| 19 |
| 20 |
| 21 def _parse_pem_key(raw_key_input): |
| 22 """Identify and extract PEM keys. |
| 23 |
| 24 Determines whether the given key is in the format of PEM key, and extracts |
| 25 the relevant part of the key if it is. |
| 26 |
| 27 Args: |
| 28 raw_key_input: The contents of a private key file (either PEM or PKCS12). |
| 29 |
| 30 Returns: |
| 31 string, The actual key if the contents are from a PEM file, or else None. |
| 32 """ |
| 33 offset = raw_key_input.find(b'-----BEGIN ') |
| 34 if offset != -1: |
| 35 return raw_key_input[offset:] |
| 36 |
| 37 |
| 38 def _json_encode(data): |
| 39 return json.dumps(data, separators=(',', ':')) |
| 40 |
| 41 |
| 42 def _urlsafe_b64encode(raw_bytes): |
| 43 if isinstance(raw_bytes, six.text_type): |
| 44 raw_bytes = raw_bytes.encode('utf-8') |
| 45 return base64.urlsafe_b64encode(raw_bytes).decode('ascii').rstrip('=') |
| 46 |
| 47 |
| 48 def _urlsafe_b64decode(b64string): |
| 49 # Guard against unicode strings, which base64 can't handle. |
| 50 if isinstance(b64string, six.text_type): |
| 51 b64string = b64string.encode('ascii') |
| 52 padded = b64string + b'=' * (4 - len(b64string) % 4) |
| 53 return base64.urlsafe_b64decode(padded) |
| OLD | NEW |