-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
265 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2023 Richard Hughes <[email protected]> | ||
# | ||
# SPDX-License-Identifier: LGPL-2.1+ | ||
# | ||
# pylint: disable=too-few-public-methods | ||
|
||
from enum import IntEnum | ||
|
||
from typing import Optional | ||
|
||
|
||
class uSwidHashAlg(IntEnum): | ||
SHA1 = 0 | ||
SHA256 = 1 | ||
SHA384 = 7 | ||
SHA512 = 8 | ||
|
||
@classmethod | ||
def from_string(cls, alg_id: str) -> "uSwidHashAlg": | ||
return cls( | ||
{ | ||
"SHA1": uSwidHashAlg.SHA1, | ||
"SHA256": uSwidHashAlg.SHA256, | ||
"SHA384": uSwidHashAlg.SHA384, | ||
"SHA512": uSwidHashAlg.SHA512, | ||
}[alg_id] | ||
) | ||
|
||
|
||
class uSwidHash: | ||
"""represents a SWID link""" | ||
|
||
def __init__( | ||
self, | ||
alg_id: Optional[uSwidHashAlg] = None, | ||
value: Optional[str] = None, | ||
): | ||
self.alg_id: Optional[uSwidHashAlg] = alg_id | ||
self.value: Optional[str] = value | ||
|
||
@property | ||
def value(self) -> Optional[str]: | ||
return self._value | ||
|
||
@value.setter | ||
def value(self, value: Optional[str]) -> None: | ||
if self.alg_id is None and value: | ||
if len(value) == 40: | ||
self.alg_id = uSwidHashAlg.SHA1 | ||
elif len(value) == 64: | ||
self.alg_id = uSwidHashAlg.SHA256 | ||
elif len(value) == 96: | ||
self.alg_id = uSwidHashAlg.SHA384 | ||
elif len(value) == 128: | ||
self.alg_id = uSwidHashAlg.SHA512 | ||
self._value = value | ||
|
||
def __repr__(self) -> str: | ||
return "uSwidHash({},{})".format( | ||
self.alg_id.name if self.alg_id else uSwidHashAlg.SHA1.name, self.value | ||
) |
Oops, something went wrong.