2
2
3
3
# SPDX-License-Identifier: Apache-2.0
4
4
5
+ import binascii
6
+ import io
7
+ import os
5
8
import sys
6
9
from cryptography .hazmat .primitives .hashes import Hash , SHA256
7
10
8
11
AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */"
9
12
10
13
14
+ class FileHandler (object ):
15
+ def __init__ (self , file , * args , ** kwargs ):
16
+ self .file_in = file
17
+ self .args = args
18
+ self .kwargs = kwargs
19
+
20
+ def __enter__ (self ):
21
+ if isinstance (self .file_in , (str , bytes , os .PathLike )):
22
+ self .file = open (self .file_in , * self .args , ** self .kwargs )
23
+ else :
24
+ self .file = self .file_in
25
+ return self .file
26
+
27
+ def __exit__ (self , * args ):
28
+ if self .file != self .file_in :
29
+ self .file .close ()
30
+
31
+
11
32
class KeyClass (object ):
12
33
def _emit (self , header , trailer , encoded_bytes , indent , file = sys .stdout ,
13
34
len_format = None ):
14
- if file and file is not sys .stdout :
15
- with open (file , 'w' ) as file :
16
- self ._emit_to_output (header , trailer , encoded_bytes , indent ,
17
- file , len_format )
18
- else :
35
+ with FileHandler (file , 'w' ) as file :
19
36
self ._emit_to_output (header , trailer , encoded_bytes , indent ,
20
- sys . stdout , len_format )
37
+ file , len_format )
21
38
22
39
def _emit_to_output (self , header , trailer , encoded_bytes , indent , file ,
23
40
len_format ):
@@ -33,6 +50,16 @@ def _emit_to_output(self, header, trailer, encoded_bytes, indent, file,
33
50
if len_format is not None :
34
51
print (len_format .format (len (encoded_bytes )), file = file )
35
52
53
+ def _emit_raw (self , encoded_bytes , file ):
54
+ with FileHandler (file , 'wb' ) as file :
55
+ try :
56
+ # file.buffer is not part of the TextIOBase API
57
+ # and may not exist in some implementations.
58
+ file .buffer .write (encoded_bytes )
59
+ except AttributeError :
60
+ # raw binary data, can be for example io.BytesIO
61
+ file .write (encoded_bytes )
62
+
36
63
def emit_c_public (self , file = sys .stdout ):
37
64
self ._emit (
38
65
header = "const unsigned char {}_pub_key[] = {{"
@@ -58,20 +85,12 @@ def emit_c_public_hash(self, file=sys.stdout):
58
85
file = file )
59
86
60
87
def emit_raw_public (self , file = sys .stdout ):
61
- if file and file is not sys .stdout :
62
- with open (file , 'wb' ) as file :
63
- file .write (self .get_public_bytes ())
64
- else :
65
- sys .stdout .buffer .write (self .get_public_bytes ())
88
+ self ._emit_raw (self .get_public_bytes (), file = file )
66
89
67
90
def emit_raw_public_hash (self , file = sys .stdout ):
68
91
digest = Hash (SHA256 ())
69
92
digest .update (self .get_public_bytes ())
70
- if file and file is not sys .stdout :
71
- with open (file , 'wb' ) as file :
72
- file .write (digest .finalize ())
73
- else :
74
- sys .stdout .buffer .write (digest .finalize ())
93
+ self ._emit_raw (digest .finalize (), file = file )
75
94
76
95
def emit_rust_public (self , file = sys .stdout ):
77
96
self ._emit (
@@ -83,11 +102,8 @@ def emit_rust_public(self, file=sys.stdout):
83
102
file = file )
84
103
85
104
def emit_public_pem (self , file = sys .stdout ):
86
- if file and file is not sys .stdout :
87
- with open (file , 'w' ) as file :
88
- print (str (self .get_public_pem (), 'utf-8' ), file = file , end = '' )
89
- else :
90
- print (str (self .get_public_pem (), 'utf-8' ), file = sys .stdout , end = '' )
105
+ with FileHandler (file , 'w' ) as file :
106
+ print (str (self .get_public_pem (), 'utf-8' ), file = file , end = '' )
91
107
92
108
def emit_private (self , minimal , format , file = sys .stdout ):
93
109
self ._emit (
0 commit comments