openc2lib.types.base.binary

 1import base64
 2
 3from openc2lib.types.base.openc2_type import Openc2Type
 4
 5class Binary(Openc2Type):
 6	""" OpenC2 Binary data
 7
 8		Binary data that are expected to be encoded with base64 
 9		as defined in [RFC4648], Section 5 (Sec. 3.1.5).
10	"""
11
12	def __init__(self, b=None):
13		""" Initializes from bytes or null """
14		if b is None:
15			b = b''
16		self.set(b)			
17
18	def set(self, b):
19		self._data = bytes(b)
20	
21	def get(self):
22		return self._data
23	
24	def __str__(self):
25		""" Returns base64 encoding """
26		if self._data is not None:
27			return base64.b64encode(self._data).decode('ascii')
28		else:
29			return ""
30			
31	def todict(self, e):
32		""" Encodes with base64 """
33		return base64.b64encode(self._data).decode('ascii')	
34
35	@classmethod
36	def fromdict(cls, dic, e):
37		""" Builds from base64encoding """
38		try:
39			return cls( base64.b64decode(dic.encode('ascii')) )
40		except:		
41			raise TypeError("Unexpected b64 value: ", dic)
class Binary(openc2lib.types.base.openc2_type.Openc2Type):
 6class Binary(Openc2Type):
 7	""" OpenC2 Binary data
 8
 9		Binary data that are expected to be encoded with base64 
10		as defined in [RFC4648], Section 5 (Sec. 3.1.5).
11	"""
12
13	def __init__(self, b=None):
14		""" Initializes from bytes or null """
15		if b is None:
16			b = b''
17		self.set(b)			
18
19	def set(self, b):
20		self._data = bytes(b)
21	
22	def get(self):
23		return self._data
24	
25	def __str__(self):
26		""" Returns base64 encoding """
27		if self._data is not None:
28			return base64.b64encode(self._data).decode('ascii')
29		else:
30			return ""
31			
32	def todict(self, e):
33		""" Encodes with base64 """
34		return base64.b64encode(self._data).decode('ascii')	
35
36	@classmethod
37	def fromdict(cls, dic, e):
38		""" Builds from base64encoding """
39		try:
40			return cls( base64.b64decode(dic.encode('ascii')) )
41		except:		
42			raise TypeError("Unexpected b64 value: ", dic)

OpenC2 Binary data

Binary data that are expected to be encoded with base64 as defined in [RFC4648], Section 5 (Sec. 3.1.5).

Binary(b=None)
13	def __init__(self, b=None):
14		""" Initializes from bytes or null """
15		if b is None:
16			b = b''
17		self.set(b)			

Initializes from bytes or null

def set(self, b):
19	def set(self, b):
20		self._data = bytes(b)
def get(self):
22	def get(self):
23		return self._data
def todict(self, e):
32	def todict(self, e):
33		""" Encodes with base64 """
34		return base64.b64encode(self._data).decode('ascii')	

Encodes with base64

@classmethod
def fromdict(cls, dic, e):
36	@classmethod
37	def fromdict(cls, dic, e):
38		""" Builds from base64encoding """
39		try:
40			return cls( base64.b64decode(dic.encode('ascii')) )
41		except:		
42			raise TypeError("Unexpected b64 value: ", dic)

Builds from base64encoding