James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # 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, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import sys |
| 16 | import os |
| 17 | |
| 18 | from cryptography.hazmat.backends import default_backend |
| 19 | from cryptography.hazmat.primitives.asymmetric import padding |
| 20 | from cryptography.hazmat.primitives import serialization |
| 21 | from cryptography.hazmat.primitives import hashes |
| 22 | |
| 23 | FIXTURE_DIR = os.path.join(os.path.dirname(__file__), |
| 24 | 'fixtures') |
| 25 | |
| 26 | |
| 27 | def main(): |
| 28 | private_key_file = os.path.join(FIXTURE_DIR, 'private.pem') |
| 29 | with open(private_key_file, "rb") as f: |
| 30 | private_key = serialization.load_pem_private_key( |
| 31 | f.read(), |
| 32 | password=None, |
| 33 | backend=default_backend() |
| 34 | ) |
| 35 | |
| 36 | # Extract public key from private |
| 37 | public_key = private_key.public_key() |
| 38 | |
| 39 | # https://cryptography.io/en/stable/hazmat/primitives/asymmetric/rsa/#encryption |
| 40 | ciphertext = public_key.encrypt( |
| 41 | sys.argv[1], |
| 42 | padding.OAEP( |
| 43 | mgf=padding.MGF1(algorithm=hashes.SHA1()), |
| 44 | algorithm=hashes.SHA1(), |
| 45 | label=None |
| 46 | ) |
| 47 | ) |
| 48 | print(ciphertext.encode('base64')) |
| 49 | |
| 50 | if __name__ == '__main__': |
| 51 | main() |