David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 1 | """General key class.""" |
| 2 | |
| 3 | import sys |
| 4 | |
| 5 | AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */" |
| 6 | |
| 7 | class KeyClass(object): |
| 8 | def _public_emit(self, header, trailer, indent, file=sys.stdout, len_format=None): |
| 9 | print(AUTOGEN_MESSAGE, file=file) |
| 10 | print(header, end='', file=file) |
| 11 | encoded = self.get_public_bytes() |
| 12 | for count, b in enumerate(encoded): |
| 13 | if count % 8 == 0: |
| 14 | print("\n" + indent, end='', file=file) |
| 15 | else: |
| 16 | print(" ", end='', file=file) |
| 17 | print("0x{:02x},".format(b), end='', file=file) |
| 18 | print("\n" + trailer, file=file) |
| 19 | if len_format is not None: |
| 20 | print(len_format.format(len(encoded)), file=file) |
| 21 | |
| 22 | def emit_c(self, file=sys.stdout): |
| 23 | self._public_emit( |
| 24 | header="const unsigned char {}_pub_key[] = {{".format(self.shortname()), |
| 25 | trailer="};", |
| 26 | indent=" ", |
| 27 | len_format="const unsigned int {}_pub_key_len = {{}};".format(self.shortname()), |
| 28 | file=file) |
| 29 | |
| 30 | def emit_rust(self, file=sys.stdout): |
| 31 | self._public_emit( |
| 32 | header="static {}_PUB_KEY: &'static [u8] = &[".format(self.shortname().upper()), |
| 33 | trailer="];", |
| 34 | indent=" ", |
| 35 | file=file) |